Last active
May 28, 2018 14:27
-
-
Save tonyseek/764219053d727087fadc2827125013d0 to your computer and use it in GitHub Desktop.
Perform MySQL incremental backup with Percona XtraBackup
This file contains 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/sh | |
set -e | |
MYSQL_USERNAME='root' | |
MYSQL_PASSWORD='' | |
MYSQL_HOST='127.0.0.1' | |
MYSQL_PORT='3306' | |
MYSQL_DATADIR='/var/lib/mysql' | |
MYSQL_BACKUP_BIN="/usr/bin/xtrabackup" | |
MYSQL_BACKUP_BASEDIR='/nfs/mysql-backup' | |
MYSQL_BACKUP_ROTATE='72' | |
MYSQL_BACKUP_POSITION="$(cat "$MYSQL_BACKUP_BASEDIR/position" 2>/dev/null || true)" | |
if [ -z "$MYSQL_BACKUP_POSITION" ]; then | |
MYSQL_BACKUP_INCRDIR="$MYSQL_BACKUP_BASEDIR/base" | |
MYSQL_BACKUP_POSITION_NEXT="1" | |
else | |
MYSQL_BACKUP_INCRDIR="$MYSQL_BACKUP_BASEDIR/inc$MYSQL_BACKUP_POSITION" | |
MYSQL_BACKUP_POSITION_NEXT="$((MYSQL_BACKUP_POSITION + 1))" | |
fi | |
mysql_backup_archive() { | |
if [ -d "$1" ]; then | |
(cd "$(dirname "$1")" && tar -cJPf "$1.tar.xz" "$1" && rm -rf "$1") | |
fi | |
} | |
mysql_backup_unarchive() { | |
if [ ! -d "$1" ]; then | |
(cd "/" && tar -xJf "$1.tar.xz" && rm -rf "$1.tar.xz") | |
fi | |
} | |
if [ -n "$MYSQL_BACKUP_ROTATE" ]; then | |
if [ "$MYSQL_BACKUP_POSITION_NEXT" -gt "$MYSQL_BACKUP_ROTATE" ]; then | |
MYSQL_BACKUP_POSITION_NEXT="$((MYSQL_BACKUP_POSITION_NEXT % MYSQL_BACKUP_ROTATE))" | |
fi | |
fi | |
printf "Unarchiving base dir %s\\n" "$MYSQL_BACKUP_INCRDIR" | |
mysql_backup_unarchive "$MYSQL_BACKUP_INCRDIR" | |
MYSQL_BACKUP_TARGETDIR="$MYSQL_BACKUP_BASEDIR/inc$MYSQL_BACKUP_POSITION_NEXT" | |
if [ -d "$MYSQL_BACKUP_TARGETDIR" ] || [ -f "$MYSQL_BACKUP_TARGETDIR.tar.xz" ] | |
then | |
printf "Rotating the old backup %s\\n" "$MYSQL_BACKUP_TARGETDIR" | |
rm -rf "$MYSQL_BACKUP_TARGETDIR" "$MYSQL_BACKUP_TARGETDIR.tar" | |
fi | |
printf "Performing backup to %s based on %s\\n" \ | |
"$MYSQL_BACKUP_TARGETDIR" "$MYSQL_BACKUP_INCRDIR" | |
"$MYSQL_BACKUP_BIN" \ | |
--backup \ | |
--target-dir="$MYSQL_BACKUP_TARGETDIR" \ | |
--incremental-basedir="$MYSQL_BACKUP_INCRDIR" \ | |
--datadir="$MYSQL_DATADIR" \ | |
--user="$MYSQL_USERNAME" \ | |
--password="$MYSQL_PASSWORD" \ | |
--host="$MYSQL_HOST" \ | |
--port="$MYSQL_PORT" | |
printf "Updating the incremental backup position\\n" | |
echo "$MYSQL_BACKUP_POSITION_NEXT" > "$MYSQL_BACKUP_BASEDIR/position" | |
printf "Archiving base dir %s\\n" "$MYSQL_BACKUP_INCRDIR" | |
mysql_backup_archive "$MYSQL_BACKUP_INCRDIR" | |
printf "Archiving the incremental backup %s\\n" "$MYSQL_BACKUP_TARGETDIR" | |
mysql_backup_archive "$MYSQL_BACKUP_TARGETDIR" | |
printf "All successful\\n" |
This file contains 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/sh | |
set -e | |
REDIS_DATADIR='/var/lib/redis' | |
REDIS_BACKUP_BASEDIR='/nfs/redis-backup' | |
REDIS_BACKUP_ROTATE='72' | |
REDIS_BACKUP_POSITION="$(cat "$REDIS_BACKUP_BASEDIR/position" 2>/dev/null || true)" | |
if [ -z "$REDIS_BACKUP_POSITION" ]; then | |
REDIS_BACKUP_POSITION_NEXT="1" | |
else | |
REDIS_BACKUP_POSITION_NEXT="$((REDIS_BACKUP_POSITION + 1))" | |
fi | |
if [ -n "$REDIS_BACKUP_ROTATE" ]; then | |
if [ "$REDIS_BACKUP_POSITION_NEXT" -gt "$REDIS_BACKUP_ROTATE" ]; then | |
REDIS_BACKUP_POSITION_NEXT="$((REDIS_BACKUP_POSITION_NEXT % REDIS_BACKUP_ROTATE))" | |
fi | |
fi | |
REDIS_BACKUP_TARGETDIR="$REDIS_BACKUP_BASEDIR/$REDIS_BACKUP_POSITION_NEXT" | |
if [ -d "$REDIS_BACKUP_TARGETDIR" ] || [ -f "$REDIS_BACKUP_TARGETDIR" ] | |
then | |
printf "Rotating the old backup %s\\n" "$REDIS_BACKUP_TARGETDIR" | |
rm -rf "$REDIS_BACKUP_TARGETDIR" | |
fi | |
printf "Performing backup to %s\\n" "$REDIS_BACKUP_TARGETDIR" | |
mkdir -p "$REDIS_BACKUP_TARGETDIR" | |
cp "$REDIS_DATADIR/appendonly.aof" "$REDIS_BACKUP_TARGETDIR" | |
cp "$REDIS_DATADIR/dump.rdb" "$REDIS_BACKUP_TARGETDIR" | |
printf "Updating the backup position\\n" | |
echo "$REDIS_BACKUP_POSITION_NEXT" > "$REDIS_BACKUP_BASEDIR/position" | |
printf "All successful\\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment