Redis is a key-value cached database, which can be persisted (permanently saved) to disk storage (ie, database). In this article, you will learn how to backup Redis database on Ubuntu 14.04 server.
In general, Redis data is saved to the disk in the .rdb
file by default, which is a point-in-time snapshot of the Redis data set. Snapshots are generated at specified intervals, so they are very suitable for backup.
To complete the steps in this tutorial, you need:
/etc/redis/redis.conf
.Redis stores its data in a directory on the server, which is what we want to back up. First, we need to know where it is.
In Ubuntu and other Linux distributions, the Redis database directory is /var/lib/redis
. However, if you are managing your inherited server and the Redis data location has changed, you can find it by typing:
sudo locate *rdb
Or, you can also find it from the redis-cli
prompt, please enter:
redis-cli
If the Redis server is not running, the response will be:
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected>
In this case, start Redis and reconnect with the following command:
sudo service redis-server start
redis-cli
The shell prompt should now change to:
127.0.0.1:6379>
When connecting to Redis, the next two commands will authenticate it and get the data directory:
auth insert-redis-password-here
config get dir
The output of the last command should be your Redis data directory:
1)" dir"2)"/var/lib/redis"
Make a note of your Redis directory. If it is different from the directory shown, make sure to use this directory throughout the tutorial.
You can now exit the database command line interface:
exit
Check if this is the correct directory:
ls /var/lib/redis
You should see a dump.rdb
file. That's Redis data. If appendonly
is also enabled, you will also see an appendonly.aof
or another .aof
file, which contains a log of all write operations received by the server.
Basically, the .rdb
file is the current snapshot, and the .aof
file saves your Redis history. Both are worth backing up.
We will start with the .rdb
file and end with an automatic backup of two files.
In this section, you can create some sample data to store in the Redis database. If you already have data on your server, you only need to back up the existing content.
Log in to the database command line interface:
redis-cli
Certification:
auth insert-redis-password-here
Let's add some sample data. You should get a reply of OK
after each step.
SET shapes:triangles "3 sides"
SET shapes:squares "4 sides"
Confirm that the data has been added.
GET shapes:triangles
GET shapes:squares
The output is included below:
"3 sides"
"4 sides"
To commit these changes to the /var/lib/redis/dump.rdb
file, please save:
save
You can exit:
exit
If you want, you can check the contents of the dump file immediately. It should contain your data, even in a machine-readable form.
sudo cat /var/lib/redis/dump.rdb
document content:
REDIS0006?shapes:squares4 sidesshapes:triangles3 sides??o????C
Now that you know the location of the Redis data, you can perform a backup. The introduction of (http://redis.io/topics/persistence) from Redis official [website] is as follows:
Redis is very suitable for data backup because you can copy RDB files while the database is running: RDB will never be modified once it is generated, and it uses a temporary name when it is generated and only uses rename (2) to rename it atomically to its final When the target new snapshot is completed.
Therefore, you can back up or copy database files while the Redis server is running. Suppose you back it up to a directory under your home folder. Performing the backup is as simple as typing the following:
sudo cp /var/lib/redis/dump.rdb /home/sammy/redis-backup-001
**Redis saves content here regularly, which means that if you run the above command, the latest backup cannot be guaranteed. **You need to save the data first.
However, if you can accept a small amount of data loss, you only need to back up this one file.
Save database state
To get an updated copy of Redis data, a better way is to access redis-cli
(Redis command line).
Follow the instructions in step one for authentication.
Then, issue the save
command:
save
The output should be similar to:
OK(1.08s)
Exit the database.
Now you can run cp
according to the command given above to make sure your backup is up to date.
Although the cp
command will provide a one-time backup of the database, the best solution is to set up a cron job that will automate the process, use an executable incremental update tool, and restore the data when needed.
In this section, we will configure an automatic backup to back up the entire Redis data directory, including two data files.
There are several automatic backup tools available. In this tutorial, we will use an updated and user-friendly tool rdiff-backup
.
rdiff-backup
is a command line backup tool. You may not have rdiff-backup
installed on the server, so you must install it first:
sudo apt-get install -y rdiff-backup
Now that it is installed, you can test it by backing up the Redis data to a folder in the home directory. In this example, we assume that your home directory is: /home/sammy
Please note that if the script does not exist, the script will automatically create the target directory, that is, you don't have to create it yourself.
Using --preserve-numeric-ids, the ownership of the source folder and the target folder will be the same.
sudo rdiff-backup --preserve-numerical-ids /var/lib/redis /home/sammy/redis
Like the previous cp
command, this is a one-time backup. What has changed is that we are now backing up the entire /var/lib/redis
directory and using rdiff-backup
.
Now we will use cron to automatically perform the backup so that it can be backed up at the set time. To accomplish this, open the system crontab:
sudo crontab -e
(If you have not used crontab on this server before, please select your favorite text editor at the prompt.)
Append the following entry at the bottom of the filek.
00*** rdiff-backup --preserve-numerical-ids --no-file-statistics /var/lib/redis /home/sammy/redis
This Cron entry will perform Redis backup at midnight every day. The --no file statistics switch will prohibit writing to the file_statistics
file in the rdiff-backup-data
directory, which will make rdiff-backup
run faster and take up less disk space.
Alternatively, you can use this entry for daily backups:
@ daily rdiff-backup --preserve-numerical-ids --no-file-statistics /var/lib/redis /home/sammy/redis
Currently, the backup will be done once a day, so you can come back tomorrow for the final test. Or, you can temporarily increase the backup frequency to ensure that it works properly.
Since these files are owned by the redis system user, you can use this command to verify that they are in place (make sure the backup is actually triggered):
ls -l /home/sammy/redis
Your output should be similar to:
total 20-rw-rw----1 redis redis 70 Sep 1413:13 dump.rdb
drwx------3 root root 12288 Sep 1413:49 rdiff-backup-data
- rw-r-----1 redis redis 119 Sep 1413:09 redis-staging-ao.aof
You can now back up Redis data daily, which is stored in the home directory on the same server.
Now that you have understood how to back up the Redis database, this step will show you how to restore the database from the backup file dump.rdb
.
Restoring a backup requires you to replace the active Redis database file with the restored file. **As this can be destructive, we recommend restoring to a new Redis server as much as possible. **
You don't want to overwrite your real-time database with more problematic recovery. However, renaming does not delete the current file will reduce the risk, even if you restore to the same server, this is the strategy of this tutorial.
First, check the contents of the dump.rdb
file. Make sure it has the data you want.
You can check the contents of the dump file directly, but remember that it uses Redis readable format instead of user readable format:
sudo cat /home/gilly/redis/dump.rdb
This is a small database; your output should look like this:
REDIS0006?shapes:triangles3 sidesshapes:squares4 sides??!^?\?,?
If your most recent backup has no data, you should not proceed with the restore. If there is data, you can continue.
Let us simulate data loss, which is the reason for backup recovery.
Log in to Redis:
redis-cli
In this command sequence, we will use Redis to authorize and delete the shapes:triangles
entry:
auth insert-redis-password-here
DEL shapes:triangles
Now let us make sure to delete the entry:
GET shapes:triangles
The output should be:
( nil)
Save and exit:
save
exit
Now, if you plan to restore to a new Redis server, make sure that the new Redis server is up and running.
For the purpose of this tutorial, we will only follow this Redis cluster tutorialInstall Redis step, but if you want more complex settings, you can follow the entire article.
After verifying that Redis is running on the new server by running redis-benchmark -q -n 1000 -c 10 -P 5
, you can continue.
Before we can replace the Redis dump file, we need to stop the currently running Redis instance. **After stopping Redis, your database will be offline. **
sudo service redis-server stop
The output should be:
Stopping redis-server: redis-server
Check if it actually stopped:
sudo service redis-server status
The output is as follows:
redis-server is not running
Next, we will rename the current database file.
Redis reads its contents from the dump.rdb
file. We rename the current one to make way for our recovery file.
sudo mv /var/lib/redis/dump.rdb /var/lib/redis/dump.rdb.old
Please note that if you are sure that the current version is better than the backup file, you can restore dump.rdb.old
.
AOF tracks every write operation of the Redis database. However, since we are trying to restore from a point-in-time backup, we don't want Redis to recreate the operations stored in its AOF file.
You can also list the contents of the directory /var/lib/redis/
. If you see the file .aof
there, it means you have enabled AOF.
Let's rename the .aof
file and exclude it for now. This will rename every file ending in .aof
, so if you have multiple AOF files, you should rename the files individually instead of running this command:
sudo mv /var/lib/redis/*.aof /var/lib/redis/appendonly.aof.old
Edit the Redis configuration file to temporarily turn off AOF:
sudo nano /etc/redis/redis.conf
In the AOF
section, look for the appendonly
directive and change yes
to no
. This will disable it:
appendonly no
Now we will use our recovery file, if you follow the previous steps in this tutorial, you should save the file /home/sammy/redis/dump.rdb
.
If you want to restore to a new server, it is time to upload files from the backup server to the new server:
scp /home/sammy/redis/dump.rdb sammy@your_new_redis_server_ip:/home/sammy/dump.rdb
Now, on the restore server (can be the original Redis server or a new server), you can use cp
to copy files to the /var/lib/redis
folder:
sudo cp -p /home/sammy/redis/dump.rdb /var/lib/redis
(If you upload the file to /home/sammy/dump.rdb
, please use the command /home/sammy/dump.rdb`` sudo cp -p /home/sammy/dump.rdb /var/lib/redis
To copy files.)
Or, if you want to use rdiff-backup
, run the command shown below. Please note that this only applies when restoring from the folder rdiff-backup
that was initially set. When using rdiff-backup
, you must specify the name of the file in the target folder:
sudo rdiff-backup -r now /home/sammy/redis/dump.rdb /var/lib/redis/dump.rdb
Detailed information about the -r
option can be found on the project website given at the end of this article.
If you are restoring to the same server where the backup was made, you may already have the correct permissions.
If you copy the backup file to a new server, you may have to update the file permissions.
Let's check the permissions of the dump.rdb
file in the /var/lib/redis/
directory.
ls -la /var/lib/redis/
If you see something like this:
- rw-r-----1 sammy sammy 70 Feb 2515:38 dump.rdb
- rw-rw----1 redis redis 4137 Feb 2515:36 dump.rdb.old
You need to update the permissions so that the file belongs to the redis user and group:
sudo chown redis:redis /var/lib/redis/dump.rdb
Update files can also be written by the group:
sudo chmod 660/var/lib/redis/dump.rdb
Now list the contents of the /var/lib/redis/
directory again:
ls -la /var/lib/redis/
Now, your restored dump.rdb
file has the correct permissions:
- rw-rw----1 redis redis 70 Feb 2515:38 dump.rdb
- rw-rw----1 redis redis 4137 Feb 2515:36 dump.rdb.old
If your Redis server daemon ran before restoring files, it will now fail to start-it will display a message such as Could not connect to Redis at 127.0.0.1:6379: Connection refused
-check Redis logs.
If you see a line of Fatal error loading the DB: Permission denied. Exiting.
in the log, then you need to check the permissions of the dump.rdb
file as described in this step.
Now we need to start the Redis server again.
sudo service redis-server start
Let's see if the repair works.
Log in to Redis:
redis-cli
Check the shapes:triangles
entry:
GET shapes:triangles
The output should be:
"3 sides"
This result indicates that our repair work has taken effect.
exit
If you are not using AOF, you are done! The Redis instance you restored should return to normal.
If you want to resume or start using AOF to track all writes to the database, follow these instructions. The AOF file must be recreated from the Redis command line.
Log in to Redis:
redis-cli
Open AOF:
BGREWRITEAOF
You should get the output:
Background append only file rewriting started
Run the info
command. This will produce a lot of output:
info
Scroll to the Persistence section and check if the aof entry matches the one shown here. If aof_rewrite_in_progress is 0, the re-creation of the AOF file has been completed.
# Persistence
...
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:0
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
If you confirm that the re-creation of the AOF file has been completed, you can now exit the Redis command line:
exit
You can list the file /var/lib/redis
again:
ls /var/lib/redis
You should see the real-time file .aof
again, such as appendonly.aof
or redis-staging-ao.aof
, as well as the dump.rdb
file and other backup files.
After confirmation, stop the Redis server:
sudo service redis-server stop
Now, open AOF again in the redis.conf
file:
sudo nano /etc/redis/redis.conf
Then change the value of appendonly
to yes
: re-enable AOF:
appendonly yes
Start Redis:
sudo service redis-server start
If you want to verify the contents of the database again, just run the "**Check Database Content" section again.
That's it, the Redis instance should return to normal.
When you don't mind backing up data to a directory on the same server, it is very useful to back up Redis data in the way given in this article.
Of course, the safest way is to back up to a different machine. You can use multiple backup methods with the same file in the /var/lib/redis
directory.
To learn more about the open source information tutorial on backup and restore, please go to [Tencent Cloud + Community] (https://cloud.tencent.com/developer?from=10680) to learn more.
Reference: "How To Back Up and Restore Your Redis Data on Ubuntu 14.04"
Recommended Posts