MySQL Backup Rotation Script


#Author: Rajesh
#Version: 1.1

#This script will do a backup rotation of your Mysql database files. This script assumes that you want to delete backup that are older than seven days. Run the script as a cronjob

TIMESTAMP="$(date +%Y-%m-%d)"                                   # Time stamp for today's date. You can use the format you want, also with time format. For this example we will use Year-Month-Day.

DESTINATION=/tmp                                             # The destination folder where you want to backup your database dump.

mysqldump database_name > $DESTINATION/$TIMESTAMP.sql        # command to backup mysql database. You can include multiple databases or all databases. Replace database_name with the name of the database you want to backup. This will save the backup file with today's date on it ans .sql extension. 

TO_DELETE="$(date -d "$TIMESTAMP - 7 days" +%Y-%m-%d)"          # this gets the difference of dates. In this case its 7 and has the same date format as the backup file format.

if [ -f $DESTINATION/$TO_DELETE ]                            # Check to see if the file we want to delete exists in the directory. If not it will just exit. 


   echo "File is present"

   sleep 2

   echo "Deleting file $TO_DELETE.sql ......"

   sleep 2



  echo "File Does Not Exist"



Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.