Skip to content

Instantly share code, notes, and snippets.

@eddy8
Created May 9, 2021 12:25
Show Gist options
  • Save eddy8/dd0312ebfd8e0b96fdd19a1b59a4fc94 to your computer and use it in GitHub Desktop.
Save eddy8/dd0312ebfd8e0b96fdd19a1b59a4fc94 to your computer and use it in GitHub Desktop.
backup MySQL database by xtrabackup
#!/bin/bash
day=`date +%w`
dt=`date +%Y%m%d`
lastday=`date -d '1 days ago' +%Y%m%d`
user=root
pwd='6021c6e041b52921'
log=backuplog.`date +%Y%m%d`
case $day in
0)
# Sunday Full backup
find /backup/ -name "xtra_*" -mtime +6 -exec rm -rf {} \;
xtrabackup --user=$user --password=$pwd --backup --target-dir=/backup/xtra_base_$dt > /tmp/$log 2>&1
;;
1)
# Monday Relatively Sunday's incremental backup
xtrabackup --user=$user --password=$pwd --backup --target-dir=/backup/xtra_inc_$dt --incremental-basedir=/backup/xtra_base_$lastday > /tmp/$log 2>&1
;;
2)
# Tuesday Compared with Monday's incremental backup
xtrabackup --user=$user --password=$pwd --backup --target-dir=/backup/xtra_inc_$dt --incremental-basedir=/backup/xtra_inc_$lastday > /tmp/$log 2>&1
;;
3)
# Wednesday Full backup
find /backup/ -name "xtra_*" -mtime +6 -exec rm -rf {} \;
xtrabackup --user=$user --password=$pwd --backup --target-dir=/backup/xtra_base_$dt > /tmp/$log 2>&1
;;
4)
# Thursday Relatively Wednesday's incremental backup
xtrabackup --user=$user --password=$pwd --backup --target-dir=/backup/xtra_inc_$dt --incremental-basedir=/backup/xtra_base_$lastday > /tmp/$log 2>&1
;;
5)
# Friday Compared with Thursday's incremental backup
xtrabackup --user=$user --password=$pwd --backup --target-dir=/backup/xtra_inc_$dt --incremental-basedir=/backup/xtra_inc_$lastday > /tmp/$log 2>&1
;;
6)
# Saturday Compared with Friday's incremental backup
xtrabackup --user=$user --password=$pwd --backup --target-dir=/backup/xtra_inc_$dt --incremental-basedir=/backup/xtra_inc_$lastday > /tmp/$log 2>&1
;;
esac
find /tmp -mtime +6 -type f -name 'backuplog.*' -exec rm -rf {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment