Created
November 25, 2019 15:56
-
-
Save joltcan/af2364ef07961265c48cec5aca3423f1 to your computer and use it in GitHub Desktop.
Rolling backup for mariadb. Rsync $BACKUPPATH to some other $HOST to have a rolling backup.
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 | |
BACKUPHOST=10.20.30.40 | |
PASSWORD=fettmepara | |
USERNAME=sst_user | |
BACKUPPATH=/backup/db | |
OPTIONS="--backup --compress --compress-threads=4 --host $BACKUPHOST --user $USERNAME --password=$PASSWORD" | |
STARTDAY=1 # 0=Sun, 1=Mon etc | |
DATE=$(date +%Y%m%dT%H%M) | |
WEEKDAY=$(date +%w) | |
ERR="" | |
echo $DATE | |
if [ -d $BACKUPPATH.prev ]; then | |
echo "Error, previous backup exist, something is bad!" | |
fi | |
{ | |
if [[ $WEEKDAY -eq $STARTDAY ]]; then | |
echo "Move previous week out of the way" | |
[ -d $BACKUPPATH ] && mv $BACKUPPATH $BACKUPPATH.prev | |
[[ $? -eq 0 ]] || ERR+="Error moving backup to prev" | |
echo "Create new $BACKUPPATH" | |
mkdir $BACKUPPATH | |
[[ $? -eq 0 ]] || ERR+="Error creating new backup dir" | |
echo "Creating full backup for day: $WEEKDAY" | |
mariabackup $OPTIONS --target-dir=$BACKUPPATH/full | |
# check for errors | |
[[ $? -eq 0 ]] || ERR+="Error doing full backup" | |
elif [[ $WEEKDAY -gt $STARTDAY ]]; then | |
echo "Creating incremental backup for day: $WEEKDAY" | |
mariabackup $OPTIONS --target-dir=$BACKUPPATH/day$WEEKDAY --incremental-basedir=$BACKUPPATH/full | |
# check for errors | |
[ $? -eq 0 ] || ERR+="Error doing incremental backup for day $WEEKDAY" | |
fi | |
if [ "$ERR" != "" ]; then | |
echo "Errors found with backup!" | |
# todo, add signal here | |
else | |
echo "Removing previous backups" | |
[ -d $BACKUPPATH.prev ] && rm -rf $BACKUPPATH.prev | |
fi | |
} | logger -t backupdb |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment