Rsync Backup¶

This documentation explain how to automaticaly backup a directory to a remote computer using rsync protocol. To achieve this goal, standard open-source software such as cron/rsync/ssh will be used.
| Program | Description |
|---|---|
| cron | time-based job scheduler |
| ssh | network protocol to allow remote login securely |
| rsync | file synchronization and file transfer program |
rsync client¶
Local copy¶
To synchronize two directories on the same device using rsync:
rsync -a --delete /src/dir /dst/dir/
Options
Replace /src/dir and /dst/dir by your source and destination directories.
--delete: deletes files in the destination directory if they don't exist in the source directory.
Install ssh keys¶
The following procedure install ssh security keys from the client to the remote computer in order to login without the need of password.
This step is mandatory for rsync to connect to the remote computer automatically.
For the full procedure, please refer to Install SSH Keys.
Configuring rsync¶
rsync is typically used to synchronize files and directories between two different systems. It uses a type of delta encoding (hashing files on both sides) to minimize the network usage. zlib may be used for additionnal compression, and ssh for additionnal security.
Note
The rsync client and server should be located in the same sub-network.
# installing rsync
sudo apt-get install rsync
# execute one rsync task
rsync -avz -e ssh <dir> <user>@<host>:~/<remote_dir>
Replace Fields
| Field | Description |
|---|---|
<dir> |
directory to backup on the client |
<user> |
user account of the server |
<host> |
address of the server |
<remote_dir> |
remote directory where the backup will be saved on the server |
Tip
This rsync command-line synchronizes your directory with the remote backup directory.
Configuring cron¶
cron is a time-based job scheduler in unix operating system.
The origin of the name cron came from the Greek "chronos".
Create the cron task
#!/bin/bash
rsync -avz --delete -e ssh <dir> <user>@<host>:~/<remote_dir>
Give execution right to this cron job:
sudo chmod +x /etc/cron.daily/backup_task
This rsync job will be executed daily by cron.
rsync server¶
Configure rsync¶
# install rsync
sudo apt-get install rsync
# configure user/password
sudo nano /etc/rsyncd.secrets
<user>:<password>
sudo chmod 600 /etc/rsyncd.secrets
Important
Replace <user>:<password> with your own credentials.
Configure the rsync server configuration file:
sudo mkdir /path/to/backup
sudo chown -R debian:debian /path/to/backup
sudo chmod 775 /path/to/backup
uid = debian
gid = debian
max connections = 3
socket options = SO_KEEPALIVE
pid file = /var/run/rsyncd.pid
[rsync_data]
path = /path/to/backup
comment = rsync backup directory
auth users = *
secrets file = /etc/rsyncd.secrets
hosts allow = <allowed-ip>
hosts deny = *
list = true
read only = no
Important
Replace /path/to/backup with your own backup directory (ex: /media/data/rsync).
Replace <allowed-ip> by the ipv4 address of the allowed rsync client.
Activate rsync¶
# activate rsync
sudo sed -i 's/RSYNC_ENABLE=false/RSYNC_ENABLE=true/g' /etc/default/rsync
sudo service rsync restart
# enable rsync script
sudo systemctl enable rsync
sudo systemctl is-active rsync
Tip
The rsync server is up and running.