Skip to content

Instantly share code, notes, and snippets.

@melinite
Last active July 8, 2017 09:30
Show Gist options
  • Save melinite/ebba3c02b886f092445269db09677091 to your computer and use it in GitHub Desktop.
Save melinite/ebba3c02b886f092445269db09677091 to your computer and use it in GitHub Desktop.
redis rdb backup script
#!/bin/bash
## simple redis rdb backup script
## usage
## rdb-backup.sh rdb.path backup.dir bgsave.wait.seconds
SRCPATH=${1:-"/var/lib/redis/"}
DESTPATH=${2:-"/opt/redisbackups"}
DESTFILE=${3:-"`date '+%Y-%m-%d-%H%M%S'`-redis.rdb.tar.gz"}
WAIT=${4:-10} ## default wait for 10 seconds
DEST="${DESTPATH}/${DESTFILE}"
BUCKETDEST="/mnt/disks/sf3-production-backup/redisbackups"
test -f "$SRCPATH"/dump.rdb || {
echo No rdb file found ; exit 1
}
test -d "$DESTPATH" || {
echo Creating backup directory $backup_to && mkdir -p "$DESTPATH"
}
## launch bgsave
echo bgsave | redis-cli -h 10.122.0.5
echo "waiting for $WAIT seconds..."
sleep $WAIT
try=5
while [ $try -gt 0 ] ; do
saved=$(echo 'info Persistence' | redis-cli -h 10.122.0.5 | awk '/rdb_bgsave_in_progress:0/{print "saved"}')
ok=$(echo 'info Persistence' | redis-cli -h 10.122.0.5 | awk '/rdb_last_bgsave_status:ok/{print "ok"}')
if [[ "$saved" = "saved" ]] && [[ "$ok" = "ok" ]] ; then
tar cf - -C "$SRCPATH" dump.rdb | pigz -9 > "$DEST"
mv "$DEST" "$BUCKETDEST"
if [ $? = 0 ] ; then
echo "redis rdb backed up to $DEST and moved to $BUCKETDEST."
exit 0
else
echo ">> Failed to backup!"
fi
fi
try=$((try - 1))
echo "redis maybe busy, waiting and retry in 5s..."
sleep 5
done
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment