-
-
Save Photosynthesis/4ac9283608f530ca057d to your computer and use it in GitHub Desktop.
This file contains 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 | |
# | |
## OpenShift redis backup script | |
## usage | |
## Edit openshift ID and backup directory to reflect your setup | |
## Install cron cartridge and place this script in the appropriate cron directory for your needs (daily, hourly, etc.) | |
openshift_id="your_openshift_id" | |
backup_dir="app-root/repo/redis-cron-backup" | |
backup_filename="redis-dump.$(date +%Y%m%d%H%M).rdb" | |
cli="redis-cli $REDIS_CLI" | |
dump_dir="app-root/data/.redis/dbs/dump.rdb" | |
path_start="/var/lib/openshift/$openshift_id" | |
backup_path="$path_start/$backup_dir" | |
rdb_path="$path_start/$dump_dir" | |
test -f $rdb_path || { | |
echo "[ERROR]: No RDB found at $rdb_path" ; exit 1 | |
} | |
test -d $backup_dir || { | |
echo "[ERROR]: Backup directory not found. Please create backup directory $backup_path" | |
} | |
# perform a bgsave before copy | |
echo "Doing BGSAVE through Redis CLI" | |
echo "BGSAVE" | $cli | |
echo "waiting for 30 seconds..." | |
sleep 60 | |
echo "Testing Redis status..." | |
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 | |
echo "Status OK. Copying dump." | |
dst="$backup_path/$backup_filename" | |
cp $rdb_path $dst | |
if [ $? = 0 ] ; then | |
echo "Redis rdb $rdb copied to $dst. Deleting old backups." | |
# delete rdb created 3 days ago | |
cd $backup_path | |
find . \( -name "Redis-dump*" \) -mtime +3 -exec rm -f {} \; | |
exit 0 | |
else | |
echo "Failed to copy $rdb_path to $dst!" | |
fi | |
fi | |
try=$((try - 1)) | |
echo "Redis status not OK. Redis may be 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