Let's say that shortly after taking the backup, we are in the redis-cli and accidentally overwrite data that we need:
127.0.0.1:6379> set packt:welcome blahblahblahblah OK 127.0.0.1:6379> get packt:welcome "blahblahblahblah"
To get the prior data back, we will need to restore the latest backup file. First of all, we will need to exit the redis-cli and stop the Redis instance. Do note that this will result in an outage of your database:
sudo service redis_6379 stop
Next, either rename or remove the existing dump.rdb file:
sudo mv /var/lib/redis/6379/dump.rdb /var/lib/redis/6379/dump_bad.rdb
Check to see if AOF is enabled:
grep appendonly /etc/redis/6379.conf appendonly no # The name of the append only file (default: "appendonly.aof") appendfilename "appendonly.aof"
If AOF is enabled, modify the value of appendonly to yes and rename any *.aof files in the Redis directory. Next, copy the most recent backup of the dump file to the data directory:
sudo cp /local/redis/backup/dump_20170803.rdb /var/lib/redis/6379/dump.rdb
Double-check the ownership of the dump.rdb file, and ensure that it is owned by the user that Redis runs as:
ls -al /var/lib/redis/6379/
total 16 drwxr-xr-x 2 root root 4096 Sep 2 15:50 . drwxr-xr-x 3 root root 4096 Jun 18 19:38 .. -rw-r--r-- 1 root root 388 Aug 3 15:02 dump_bad.rdb -rw-r--r-- 1 root root 394 Aug 3 15:49 dump.rdb
To verify that the restore worked as expected, we can now restart Redis and query the packt:welcome key:
sudo service redis_6379 start
src/redis-cli -a currentHorseBatteryStaple
127.0.0.1:6379> get packt:welcome
"Hello world from Java!"