With this is script, we set up a backup system for the mySQL database with rotation. The rotation schedule is the following, let's say we launched the script 5 months ago, today we will find in our daily directory 7 backups, in weekly directory we see the backups from last 4 mondays, and in monthly directory there are the backup of first monday of the past 4 months
In that case we make use of rsync with symbolic links, which allows us to perform incremental backups while keeping the availability of a full backup.
#!/bin/bash #Copyright (C) 2013 Josep Manel Andrés Moscardó # #This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published #by the Free Software Foundation, either version 3 of the License, # or (at your option) any later version. # #This program is distributed in the hope that it will be useful, but #WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANT #ABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General #Public License for more details. # #You should have received a copy of the GNU General Public License #along with this program. If not, see http://www.gnu.org/licenses/. # CAL=/usr/bin/cal #Format= 2013-03-20 YESTERDAY=`date --date=yesterday +%Y-%m-%d` #Int (where Monday is 1 and Sunday is 7) TODAY=`date +%u` #Format= 2013-03-20 SEVENDAYSAGO=`date --date '7 days ago' +%Y-%m-%d` FOURWEEKSAGO=`date --date '28 days ago' +%Y-%m-%d` FIVEWEEKSAGO=`date --date '35 days ago' +%Y-%m-%d` #These variables will be used to find out which day is the backup from 5months ago that we have to remove FIVEMONTHSAGOMONTH=`date --date='5 month ago' +%m` FIVEMONTHSAGOYEAR=`date --date='5 month ago' +%Y` #This function will give us the date of the first Monday of 5 months ago month. function date5monthsago(){ #Exact day for the backup from 5months ago local aux=`$CAL $FIVEMONTHSAGOMONTH $FIVEMONTHSAGOYEAR | awk ' NR == 1 { next } NR == 2 { next } NF <= 5 { next } NF == 6 { print $1 ; exit } NF == 7 { print $2 ; exit } '` echo $aux } #AUX used for checking whether the backup from 28 days ago is the first Monday of the month backup, if so We'll keep it in monthly directory, if not, we'll remove it. AUX=`date --date '35 days ago' +%d` #Do daily backup and store it on /home/backup/daily #To log Starting time START=$(date +%s) ########################################################### echo "#########################Starting myLVMbackup for dbMaster on dbBackup#####################" >> /home/backup/daily/mylvm-dbmaster-`date +%Y-%m-%d`.log 2>>1 ########################################################### mylvmbackup --socket=/var/run/mysqld/mysqld1.sock --innodb_recover --vgname=vgpool --lvname=lvdata --backuptype=rsync --relpath=replic_dbmaster --prefix=dbmaster --backupdir=/home/backup/daily --datefmt=%Y-%m-%d --rsyncarg=" -a --link-dest=/home/backup/daily/dbmaster-${YESTERDAY}_mysql " >> /home/backup/daily/mylvm-dbmaster-`date +%Y-%m-%d`.log 2>&1 ########################################################### ########################################################### #To log Finishing time END=$(date +%s) TOTALTIME=$(( $END - $START )) echo "############################################################################################" >> /home/backup/daily/mylvm-dbmaster-`date +%Y-%m-%d`.log echo "Start time:$START, finish time:$END, seconds:$TOTALTIME" >> /home/backup/daily/mylvm-dbmaster-`date +%Y-%m-%d.log` #We will check if today is Monday, if so we'll move last monday's backup 1 directory up if [ "$TODAY" -eq "1" ]; then if [ -d "/home/backup/daily/dbmaster-${SEVENDAYSAGO}_mysql" ]; then mv /home/backup/daily/dbmaster-${SEVENDAYSAGO}_mysql /home/backup/weekly/ mv /home/backup/daily/mylvm-dbmaster-${SEVENDAYSAGO}.log /home/backup/weekly/ fi #Checking for a backup older than 28 days and also from first Monday of the month.If the backup from 28 days ago is not the firstMondayOfTheMonth we'll expire, or else keep it in monthly directory. if [ -d "/home/backup/weekly/dbmaster-${FIVEWEEKSAGO}_mysql" ]; then #Is it from first Monday of the month? if [ "$AUX" -le "7" ]; then mv /home/backup/weekly/dbmaster-${FIVEWEEKSAGO}_mysql /home/backup/monthly/ mv /home/backup/weekly/mylvm-dbmaster-${SEVENDAYSAGO}.log /home/backup/monthly/ #Execute function to find out exact date for 5 months ago FIVEMONTHSAGODAY=$(date5monthsago) #Expire backups older than 4 months ago. Note that FIVEMONTHSAGODAY has only 1 digit,that is why we add "0" before. if [ -d "/home/backup/monthly/dbmaster-${FIVEMONTHSAGOYEAR}-${FIVEMONTHSAGOMONTH}-${FIVEMONTHSAGODAY}_mysql" ]; then rm -rf /home/backup/monthly/dbmaster-${FIVEMONTHSAGOYEAR}-${FIVEMONTHSAGOMONTH}-0${FIVEMONTHSAGODAY}_mysql rm /home/backup/monthly/mylvm-dbmaster-${FIVEMONTHSAGOYEAR}-${FIVEMONTHSAGOMONTH}-0${FIVEMONTHSAGODAY}.log fi else rm -rf /home/backup/weekly/dbmaster-${FIVEWEEKSAGO}_mysql rm /home/backup/weekly/mylvm-dbmaster-${FIVEWEEKSAGO}.log fi fi #If today is NOT Monday we'll expire old daily backups else #Is there a backup from 1weekago? If so we'll exxpire it if [ -d "/home/backup/daily/dbmaster-${SEVENDAYSAGO}_mysql" ]; then rm -rf /home/backup/daily/dbmaster-${SEVENDAYSAGO}_mysql #Remove log file also rm /home/backup/daily/mylvm-dbmaster-${SEVENDAYSAGO}.log fi fi rm /root/debug-${SEVENDAYSAGO}.log ################################################################### ###Code for dbslave replication backup############################# #To log Starting time START2=$(date +%s) ################################################################### echo "#########################Starting myLVMbackup for dbSlave on dbBackup#####################" >> /home/backup/daily/mylvm-dbslave-`date +%Y-%m-%d`.log 2>>1 ################################################################### mylvmbackup --socket=/var/run/mysqld/mysqld2.sock --innodb_recover --vgname=vgpool --lvname=lvdata --backuptype=rsync --relpath=replic_dbslave --prefix=dbslave --backupdir=/home/backup/daily --datefmt=%Y-%m-%d --rsyncarg=" -a --link-dest=/home/backup/daily/dbslave-${YESTERDAY}_mysql " >> /home/backup/daily/mylvm-dbslave-`date +%Y-%m-%d`.log 2>>1 #To log Finishing time END2=$(date +%s) TOTALTIME2=$(( $END2 - $START2 )) echo "############################################################################################" >> /home/backup/daily/mylvm-dbslave-`date +%Y-%m-%d`.log echo "Start time:$START2, finish time:$END2, seconds:$TOTALTIME2" >> /home/backup/daily/mylvm-dbslave-`date +%Y-%m-%d.log` #We will check if today is Monday, if so we ll move last mondays backup 1 directory up if [ "$TODAY" -eq "1" ]; then if [ -d "/home/backup/daily/dbslave-${SEVENDAYSAGO}_mysql" ]; then mv /home/backup/daily/dbslave-${SEVENDAYSAGO}_mysql /home/backup/weekly/ mv /home/backup/daily/mylvm-dbslave-${SEVENDAYSAGO}.log /home/backup/weekly/ fi #Checking for a backup older than 28 days and also from first Monday of the month.If the backup from 28 days ago is not the firstMondayOfTheMonth we ll expire, or else keep it in monthly directory. if [ -d "/home/backup/weekly/dbslave-${FIVEWEEKSAGO}_mysql" ]; then #Is it from first Monday of the month? if [ "$AUX" -le "7" ]; then mv /home/backup/weekly/dbslave-${FIVEWEEKSAGO}_mysql /home/backup/monthly/ mv /home/backup/weekly/mylvm-dbslave-${SEVENDAYSAGO}.log /home/backup/monthly/ #Execute function to find out exact date for 5 months ago FIVEMONTHSAGODAY=$(date5monthsago) #Expire backups older than 4 months ago. Note that FIVEMONTHSAGODAY has only 1 digit,that is why we add "0" before. if [ -d "/home/backup/monthly/dbslave-${FIVEMONTHSAGOYEAR}-${FIVEMONTHSAGOMONTH}-${FIVEMONTHSAGODAY}_mysql" ]; then rm -rf /home/backup/monthly/dbslave-${FIVEMONTHSAGOYEAR}-${FIVEMONTHSAGOMONTH}-0${FIVEMONTHSAGODAY}_mysql rm /home/backup/monthly/mylvm-dbslave-${FIVEMONTHSAGOYEAR}-${FIVEMONTHSAGOMONTH}-0${FIVEMONTHSAGODAY}.log fi else rm -rf /home/backup/weekly/dbslave-${FIVEWEEKSAGO}_mysql rm /home/backup/weekly/mylvm-dbslave-${FIVEWEEKSAGO}.log fi fi #If today is NOT Monday we ll expire old daily backups else #Is there a backup from 1weekago? If so we will expire it if [ -d "/home/backup/daily/dbslave-${SEVENDAYSAGO}_mysql" ]; then rm -rf /home/backup/daily/dbslave-${SEVENDAYSAGO}_mysql #Remove log file also rm /home/backup/daily/mylvm-dbslave-${SEVENDAYSAGO}.log fi fi rm /root/debug-slave${SEVENDAYSAGO}.log