One of my favorite topics is creating backup systems. It just so happens that I sincerely believe that with a properly configured backup, a system administrator's life becomes much easier. rdiff-backup is a system that allows you to save changes and retrieve the state of any saved file at any point in time when a backup was made. As the name rdiff-backup suggests, this system is built on the principles of rdiff. And rdiff-backup itself saves the history of changes, so that you can roll back to any point. And the main advantage is that rdiff-backup allows backing up to a remote server.
In general, rdiff-backup is a fairly simple system, without any complexities, yet functional.
We will build a remote backup system, specifically, we will back up server1 to server2.
Let's start with installing rdiff-backup
yum install rdiff-backup (requires the epel repository), or you can take the rpm from pkgs.org
dependencies:
Installed:
rdiff-backup.x86_64 0:1.2.8-5.el6
Dependency Installed:
librsync.x86_64 0:0.9.7-15.el6 pylibacl.x86_64 0:0.5.0-1.el6
pyxattr.x86_64 0:0.5.0-1.el6
The build goes without unnecessary questions. And one more nuance – if you are going to back up to a remote server, you also need to install rdiff-backup on that remote server.
The next step in building the backup system is to create a special user from which the entire backup system will operate. This user does not need to authenticate with a password. Therefore, when asked adduser Use password-based authentication? [yes]: answer no
In general, the user addition dialog looks like this:
adduser backup
The user must be added on both servers. Identically.
The next step. Since rdiff-backup works via ssh, we will generate authorization keys.
I wrote about how to generate ssh keys for passwordless authentication in the article SSH, passwordless access, but for clarity, we will go through this step fully.
On server1, the one we will back up from, we will generate keys.
First, switch to the backup user:
su backup
And execute ssh-keygen:
ssh-keygen
Now exit su by pressing CTRL-D.
And copy the public key to server2:
scp /home/backup/.ssh/id_rsa.pub hilik@server2.hilik.org.ua:
And now on server2, copy or add (if this is not the first key) the public key to /home/backup/.ssh/authorized_keys:
cat ~/id_rsa.pub >> /home/backup/.ssh/authorized_keys
Now the last step remains, configuring the backup itself. First of all, on the remote server (server2), prepare a directory for backups:
mkdir /storage/backup
For the backup, I use the script /root/bin/rdiff-backup.sh, which backs up everything to the backup server. It looks like this:
#!/bin/sh
#Определяем имя сервера который бекапим, нужно для создания иерархии в бекап директории на удаленном сервере
HOST_NAME=`/bin/hostname -s`
#Адрес сервера на который будем бекапить
REMOTE_HOST='server2'
#Пользователь от имени которого будем бекапить
USER='backup'
#Директория на удаленном сервере в которой создаются бекапы
PREFIX='/storage/backup'
#Директории которые необходимо бекапить
WHAT='/etc /usr/local/etc /root /var/cron /sys/i386/conf /usr/local/www'
#Главный цикл бекапа
for i in $WHAT
do
`/usr/local/bin/rdiff-backup --remote-schema 'ssh -i /home/$USER/.ssh/id_rsa -C %s rdiff-backup --server' --create-full-path --exclude-device-files --exclude-fifos --exclude-sockets --exclude-symbolic-links -b $i $USER@$REMOTE_HOST::/$PREFIX/$HOST_NAME$i`
echo "/usr/local/bin/rdiff-backup --remote-schema 'ssh -i /home/$USER/.ssh/id_rsa -C %s rdiff-backup --server' --create-full-path --exclude-device-files --exclude-fifos --exclude-sockets --exclude-symbolic-links -b $i $USER@$REMOTE_HOST::/$PREFIX/$HOST_NAME$i"
done
Now it remains to set up this script in cron. It must be used from root:
0 1 * * * /root/bin/rdiff-backup.sh
At this point, the backup stage is complete. Backups will be saved on the remote server in the directory you specified. A subdirectory named after the server will be created within that directory, and the backup will be placed there. This location contains files reflecting the current state, the state at the last backup point, and change logs.
To add new servers, simply create a user, generate keys, and deploy the script.
Now, regarding restoration. If you need to retrieve files in their latest state, you can simply copy them from the backup. However, if you need to restore to a specific point in time, you must use rdiff-backup in restore mode.
For example, let's restore the /etc directory of server1 to /home/hilik/restore/etc.old as it was 5 days ago.
rdiff-backup --remote-schema 'ssh -i /home/backup/.ssh/id_rsa -C %s rdiff-backup --server' -r 5D backup@server2::/usr/local/share/backup/server1/etc /home/hilik/restore/etc.old
Of course, this process can be scripted or aliased, but fortunately, I don't need to roll back files to a state from several days ago very often.
That concludes the setup of rdiff-backup. This system has been running for quite a long time, and I have not experienced any inconvenience in its use.
