Table of Contents |
---|
...
- Create a backup.sh file using command - vi backup.sh
On the vim, editor screen enter the command for database backup and to delete files older than 30 days
Code Block #! /bin/bash EMAIL_ID= #<EMAIL_ID_SEND_AN_EMAIL> EMAIL_PASS= OUTPUT#<EMAIL_ID_PASSWORD> DB_NAME= #<DATABASE_NAME> DB_USER= #<DATABASE_USER> DB_PASS= #<DATABASE_USER_PASSWORD> DB_BACKUP_PATH= #<ABSOLUTE_PATH_TO_BACKUP_DIR> RCPT_EMAIL_ID= #<EMAIL_ID_TO_RECIEVE_AN_EMAIL> CLIENT_NAME_AND_ENVIRONMENT= #<CLIENT_NAME_AND_ENVIRONMENT_NAME> EMAIL_FILE=backup.txt BACKUP_LOG=db_backup.log backupStartTime() { echo "-----------------------------------------------------" &>> $BACKUP_LOG echo "DB backup start time:" `date +%x-%r` &>> $BACKUP_LOG } backupEndTime() { echo "DB backup end time:" `date +%x-%r` &>> $BACKUP_LOG echo "-----------------------------------------------------" &>> $BACKUP_LOG } backupFailedTime() { echo "DB backup fail time:" `date +%x-%r` &>> $BACKUP_LOG echo "-----------------------------------------------------" &>> $BACKUP_LOG } backupStartTime cat > $OUTPUT$EMAIL_FILE << EOF Subject: [ IMPORTATNT ALERT ] : <CLIENT'$CLIENT_NAME_AND_ENVIRONMENT_NAME>' : DB Backup Script Failed! Hello Buid Team, Please check the below errors error ================================================================================================================== EOF set -o pipefail mysqldump -u<user>u$DB_USER -p<password>p$DB_PASS --single-transaction --skip-lock-tables --routines database name $DB_NAME --verbose &>> $EMAIL_FILE | gzip > /backup directory path$DB_BACKUP_PATH/OPENSPECIMEN_`date +\%d-\%m-\%Y`.SQL.gz DUMP_EXIT_CODE=$? cat >> $OUTPUT$EMAIL_FILE << EOF =================================================================================================================== Thanks, Backup Monitor EOF if [[ $DUMP_EXIT_CODE -ne 0 ]] then sed -n '6,14p' then$EMAIL_FILE &>> $BACKUP_LOG curl --ssl-reqd --url 'smtps://smtp.gmail.com:465' -u $EMAIL_ID:$EMAIL_PASS --mail-from $EMAIL_ID --mail-rcpt '<EMAIL$RCPT_EMAIL_ID_TO_RECIEVE_FAILED_EMAIL>' --upload-file backup.txt $EMAIL_FILE backupFailedTime else backupEndTime fi rm backup.txt find /backup directory path/$DB_BACKUP_PATH -mtime +30 -exec rm {} \;
Create a cron job with the command: crontab -e
This will open a crontab (cron configuration file) and the format of the job should be as follows.
minute hour day-of-month month day-of-week <path to the backup.sh file>
This is an example file to take database backup nightly every day at 11:59
Code Block 59 23 * * * /home/backup.sh
...