Skip to content

Instantly share code, notes, and snippets.

@jay16
Forked from acenqiu/redis-backup.sh
Created October 8, 2016 13:56
Show Gist options
  • Save jay16/d618c7105c44322c80bf60a5611528e2 to your computer and use it in GitHub Desktop.
Save jay16/d618c7105c44322c80bf60a5611528e2 to your computer and use it in GitHub Desktop.
redis rdb backup script
#!/bin/bash
#
## redis backup script
## usage
## redis-backup.sh port backup.dir
port=${1:-6379}
backup_dir=${2:-"/data/backup/redis"}
cli="/usr/local/bin/redis-cli -p $port"
rdb="/var/lib/redis/$port/dump.rdb"
test -f $rdb || {
echo "[$port] No RDB Found" ; exit 1
}
test -d $backup_dir || {
echo "[$port] Create backup directory $backup_dir" && mkdir -p $backup_dir
}
# perform a bgsave before copy
echo bgsave | $cli
echo "[$port] waiting for 30 seconds..."
sleep 60
try=5
while [ $try -gt 0 ] ; do
## redis-cli output dos format line feed '\r\n', remove '\r'
bg=$(echo 'info Persistence' | $cli | awk -F: '/rdb_bgsave_in_progress/{sub(/\r/, "", $0); print $2}')
ok=$(echo 'info Persistence' | $cli | awk -F: '/rdb_last_bgsave_status/{sub(/\r/, "", $0); print $2}')
if [[ "$bg" = "0" ]] && [[ "$ok" = "ok" ]] ; then
dst="$backup_dir/$port-dump.$(date +%Y%m%d%H%M).rdb"
cp $rdb $dst
if [ $? = 0 ] ; then
echo "[$port] redis rdb $rdb copied to $dst."
# delete rdb created 3 days ago
cd $backup_dir
find . \( -name "$port-dump*" \) -mtime +3 -exec rm -f {} \;
exit 0
else
echo "[$port] >> Failed to copy $rdb to $dst!"
fi
fi
try=$((try - 1))
echo "[$port] 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