Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Create a backup.sh file using command - vi backup.sh
  2. 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_PASS=
    OUTPUT_FILE=backup.txt
    
    cat > $OUTPUT_FILE << EOF
    Subject: [ IMPORTATNT ALERT ] : <CLIENT_AND_ENVIRONMENT_NAME> DB Backup Script Failed!
    Hello Buid Team,
    Please check the below error
    ==================================================================================================================
    EOF
    
    mysqldump -u<user> -p<password> --single-transaction --skip-lock-tables --routines database name | gzip > /backup directory path/OPENSPECIMEN_`date +\%d-\%m-\%Y`.SQL.gz
    
    DUMP_EXIT_CODE=$?
    
    cat >> $OUTPUT_FILE << EOF
    ===================================================================================================================
    Thanks,
    Backup Monitor
    EOF
    
    if [[ $DUMP_EXIT_CODE -ne 0 ]] 
    then
      curl --ssl-reqd --url 'smtps://smtp.gmail.com:465' -u $EMAIL_ID:$EMAIL_PASS --mail-from $EMAIL_ID --mail-rcpt '<EMAIL_ID_TO_RECIEVE_FAILED_EMAIL>' --upload-file backup.txt
    fi
    
    rm backup.txt
    
    find /backup directory path/ -mtime +30 -exec rm {} \;


  3. Create a cron job with the command:  crontab -e

  4. 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


...