Skip to content

Instantly share code, notes, and snippets.

@sitano
Created May 18, 2015 13:57
Show Gist options
  • Save sitano/0b5d557ec6b95122e2e8 to your computer and use it in GitHub Desktop.
Save sitano/0b5d557ec6b95122e2e8 to your computer and use it in GitHub Desktop.
Force redis sync rdb snapshot, backup, tar gzip pack and rotate older then 5 days
#!/bin/bash
# 25 0 * * * /root/redis.backup.sh
PRIVATE_IPV4=`cat /etc/network/interfaces | grep -A 2 'iface eth1' | head -n +2 | tail -n 1 | awk '{print $2}'`
# 1. Dump RDB
(printf "select 2\r\nsave\r\n" | nc ${PRIVATE_IPV4} 6379) || exit 1
# 2. Copy dump
SRCPATH="/var/lib/redis/6379"
SRCFILE="dump.rdb"
DESTPATH="/var/lib/backup/redis"
DESTFILE=`date '+%Y-%m-%d-%H%M%S'`-redis-topx.rdb.tar.gz
DEST="${DESTPATH}/${DESTFILE}"
cd ${SRCPATH} && tar cfz ${DEST} ${SRCFILE}
# 3. Manage soft link
SOFT="${DESTPATH}/redis-topx.rdb.tar.gz"
ln -Tfs ${DEST} ${SOFT}
# 4. Rotate old
find ${DESTPATH}/*-redis-topx.rdb.tar.gz -mtime +5 -exec rm {} \; > /dev/null 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment