Forked from SQLadmin/xtrabackup_full_increment_restore.sh
Last active
March 1, 2022 17:13
-
-
Save masipmarch/fd6e40c30b31de4421eda24a5459f349 to your computer and use it in GitHub Desktop.
Automate xtrabackup for FULL/Incremental and restore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # https://sqlgossip.com | |
| # Script de copies de seguretat amb percona xtrabackup | |
| set -e | |
| set -u | |
| usage() { | |
| echo "usage: $(basename $0) [option]" | |
| echo "option=full: Perform Full Backup" | |
| echo "option=incremental: Perform Incremental Backup" | |
| echo "option=restore: Start to Restore! Be Careful!! " | |
| echo "option=help: show this help" | |
| } | |
| full_backup() { | |
| if [ ! -d $BACKUP_DIR ] | |
| then | |
| mkdir $BACKUP_DIR | |
| fi | |
| rm -rf $BACKUP_DIR/* | |
| echo `date '+%Y-%m-%d %H:%M:%S:%s'`": Cleanup the backup folder is done! Starting backup" >> $BACKUP_DIR/xtrabackup.log | |
| xtrabackup --defaults-file=/etc/mysql/copies.cnf --backup --history --compress --slave-info --parallel=4 --compress-threads=4 --target-dir=$BACKUP_DIR/FULL | |
| echo `date '+%Y-%m-%d %H:%M:%S:%s'`": Backup Done!" >> $BACKUP_DIR/xtrabackup.log | |
| } | |
| incremental_backup() | |
| { | |
| if [ ! -d $BACKUP_DIR/FULL ] | |
| then | |
| echo "ERROR: Unable to find the FULL Backup. aborting....." | |
| exit -1 | |
| fi | |
| if [ ! -f $BACKUP_DIR/last_incremental_number ]; then | |
| NUMBER=1 | |
| else | |
| NUMBER=$(($(cat $BACKUP_DIR/last_incremental_number) + 1)) | |
| fi | |
| echo `date '+%Y-%m-%d %H:%M:%S:%s'`": Starting Incremental backup $NUMBER" >> $BACKUP_DIR/xtrabackup.log | |
| if [ $NUMBER -eq 1 ] | |
| then | |
| xtrabackup --defaults-file=/etc/mysql/copies.cnf --backup --history --slave-info --incremental --parallel=4 --compress-threads=4 --target-dir=$BACKUP_DIR/inc$NUMBER --incremental-basedir=$BACKUP_DIR/FULL | |
| else | |
| xtrabackup --defaults-file=/etc/mysql/copies.cnf --backup --history --slave-info --incremental --parallel=4 --compress-threads=4 --target-dir=$BACKUP_DIR/inc$NUMBER --incremental-basedir=$BACKUP_DIR/inc$(($NUMBER - 1)) | |
| fi | |
| echo $NUMBER > $BACKUP_DIR/last_incremental_number | |
| echo `date '+%Y-%m-%d %H:%M:%S:%s'`": Incremental Backup:$NUMBER done!" >> $BACKUP_DIR/xtrabackup.log | |
| } | |
| restore() | |
| { | |
| echo `date '+%Y-%m-%d %H:%M:%S:%s'`": Decompressing the FULL backup" >> $BACKUP_DIR/xtrabackup-restore.log | |
| xtrabackup --decompress --remove-original --parallel=4 --target-dir=$BACKUP_DIR/FULL | |
| echo `date '+%Y-%m-%d %H:%M:%S:%s'`": Decompressing Done !!!" >> $BACKUP_DIR/xtrabackup-restore.log | |
| echo `date '+%Y-%m-%d %H:%M:%S:%s'`": Prepareing FULL Backup ..." >> $BACKUP_DIR/xtrabackup-restore.log | |
| xtrabackup --prepare --apply-log-only --target-dir=$BACKUP_DIR/FULL | |
| echo `date '+%Y-%m-%d %H:%M:%S:%s'`": FULL Backup Preparation Done!!!" >> $BACKUP_DIR/xtrabackup-restore.log | |
| P=1 | |
| while [ -d $BACKUP_DIR/inc$P ] && [ -d $BACKUP_DIR/inc$(($P+1)) ] | |
| do | |
| echo `date '+%Y-%m-%d %H:%M:%S:%s'`": Decompressing incremental:$P" >> $BACKUP_DIR/xtrabackup-restore.log | |
| xtrabackup --decompress --remove-original --parallel=4 --target-dir=$BACKUP_DIR/inc$P | |
| echo `date '+%Y-%m-%d %H:%M:%S:%s'`": Decompressing incremental:$P Done !!!" >> $BACKUP_DIR/xtrabackup-restore.log | |
| echo `date '+%Y-%m-%d %H:%M:%S:%s'`": Prepareing incremental:$P" >> $BACKUP_DIR/xtrabackup-restore.log | |
| xtrabackup --prepare --apply-log-only --target-dir=$BACKUP_DIR/FULL --incremental-dir=$BACKUP_DIR/inc$P | |
| echo `date '+%Y-%m-%d %H:%M:%S:%s'`": incremental:$P Preparation Done!!!" >> $BACKUP_DIR/xtrabackup-restore.log | |
| P=$(($P+1)) | |
| done | |
| if [ -d $BACKUP_DIR/inc$P ] | |
| then | |
| echo `date '+%Y-%m-%d %H:%M:%S:%s'`": Decompressing the last incremental:$P" >> $BACKUP_DIR/xtrabackup-restore.log | |
| xtrabackup --decompress --remove-original --parallel=4 --target-dir=$BACKUP_DIR/inc$P | |
| echo `date '+%Y-%m-%d %H:%M:%S:%s'`": Decompressing the last incremental:$P Done !!!" >> $BACKUP_DIR/xtrabackup-restore.log | |
| echo `date '+%Y-%m-%d %H:%M:%S:%s'`": Prepareing the last incremental:$P" >> $BACKUP_DIR/xtrabackup-restore.log | |
| xtrabackup --prepare --target-dir=$BACKUP_DIR/FULL --incremental-dir=$BACKUP_DIR/inc$P | |
| echo `date '+%Y-%m-%d %H:%M:%S:%s'`": Last incremental:$P Preparation Done!!!" >> $BACKUP_DIR/xtrabackup-restore.log | |
| fi | |
| } | |
| ## Parameters | |
| SECRET='mysql-user-password' | |
| BACKUP_DIR=/var/backup/xtrabackup/$(date +\%Y-\%m-\%d) | |
| DATA_DIR=/mysqldata | |
| if [ $# -eq 0 ] | |
| then | |
| usage | |
| exit 1 | |
| fi | |
| case $1 in | |
| "full") | |
| full_backup | |
| ;; | |
| "incremental") | |
| incremental_backup | |
| ;; | |
| "restore") | |
| restore | |
| ;; | |
| "help") | |
| usage | |
| break | |
| ;; | |
| *) echo "invalid option";; | |
| esac | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment