Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Perform MySQL incremental backup with Percona XtraBackup
#!/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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment