Last active
January 18, 2022 05:04
-
-
Save iandark/6e8cb9e684dfc2efbc24a80caee58372 to your computer and use it in GitHub Desktop.
restore rdb file into redis
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 | |
| ## Make sure you have set; appendonly 'no' in redis.conf | |
| ## This script is an enhanced copy from https://github.com/msherer/redis_rdb_import/blob/master/redisImport.sh | |
| ## Thanks Matt Sherer, who ever who you are | |
| set -e | |
| ## | |
| # Paths for the original RDB file | |
| ## | |
| rdbPath=/var/lib/redis | |
| rdbFile=dump.rdb | |
| rdbBackup=dump$(date +%F).rdb.bak | |
| ## | |
| # Paths for the dump in the user directory that will be imported | |
| ## | |
| backupPath=$HOME/Downloads | |
| backupFile=dump.rdb | |
| # Check for original file and backup file | |
| if test -f "$rdbPath/$rdbFile" && test -f "$backupPath/$backupFile"; then | |
| # Stop Redis for import | |
| sudo systemctl stop redis.service | |
| rdbFilepath="$rdbPath/$rdbFile" | |
| rdbBackupFilepath="$rdbPath/$rdbBackup" | |
| backupFilepath="$backupPath/$backupFile" | |
| # Backup the original redis file | |
| echo "Making backup from $rdbFilepath -> $rdbBackupFilepath" | |
| mv $rdbFilepath $rdbBackupFilepath || { | |
| echo "Failed to move file $rdbFilepath -> $rdbBackupFilepath" | |
| echo "Restarting Redis." | |
| sudo systemctl start redis.service | |
| exit 1 | |
| } | |
| # Import redis dump | |
| echo "Importing redis backup from $backupPath/$backupFile -> $rdbFilePath" | |
| cp $backupFilepath $rdbFilepath || { | |
| echo "Failed to copy $backupFilepath -> $rdbFilepath" | |
| echo "Restarting Redis." | |
| sudo systemctl start redis.service | |
| exit 1 | |
| } | |
| # Start Redis after completion | |
| sudo systemctl start redis.service | |
| else | |
| echo "Please make sure $rdbPath/$rdbFile AND $backupPath/$backupFile both exist and try again." | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment