rsync can be used to copy and synchronize files in the local file system or over different kind of connections. The program has several modes of operation. It can be for example be used to communicate over a secure shell (ssh) connection, but it can also be configured to act as a server. In that case clients can ask for a list of shares the rsync server provides and download them. The big advantage of using rsync comes into play here: The daemon can run under a different user account then the clients. This can be used to make backups.
Simple client mode #
In the easiest case rsync can nearly be used like copy, once you
remember -av
:
rsync -av [sourcepath] [targetpath]
Or directly over ssh:
rsync -av -e ssh [host]:[hostsourcepath] [localtargetpath]
Server mode #
pid file = /var/run/rsyncd.pid
uid = root
gid = wheel
[root]
path = /
comment = root file system
looks similar to samba's configuration file. The root
in square
brackets could also have been anything. After configuration the rsync
daemon can be started. Because we did not configure a password we want
to make the rsync server available on local interfaces only:
rsync --address 127.0.0.1 --daemon
rsync should now be listening on port 127.0.0.1:873
and we could
connect to it from the local machine by using:
ssh -L 8730:localhost:873 RsyncHostOrIP
to bind the local port 8730 to the remote port 873. We can then use the local port to securely communicate with the remote rsync:
rsync rsync://localhost:8730
Simplification is possible by using a ~/.ssh/config
-file, but that is
beyond the topic here and here will be an extra article about ssh some
day.
Elegant way to incrementally back up on an external hard disk #
The following script can be copied to an external disk drive. Once called it does an incremental backup, while keeping versions of deleted files. This is archived by creating a new directory each time the script gets called. This directory contains only new files and hard links to the unmodified files. Because directories are named after the date the backup took place it is possible to access preview versions of the whole file system.
#!/usr/bin/bash -v
SOURCE=/home/
DATE_REUSE=$(readlink -n latest)
DATE_TODAY=$(date +%Y-%m-%d_%H:%M:%S)
rsync -ahoi --info=all \
--link-dest=../$DATE_REUSE \
--log-file=$DATE_TODAY.log \
$SOURCE \
`pwd`/$DATE_TODAY \
2> >(while read line; do echo -e "\e[01;31m$line\e[0m" >&2; done)
echo $?
rm -f latest
ln -s $DATE_TODAY latest
* modify SOURCE=/home/
(line 2) according to your needs. * line 12
colorizes rsync output to improve its readability.