rsync (remote synchronization), is a powerful command-line tool for copying/synchronizing files and folders on local or remote hosts.
It is ideal for copying files and backing up a file server, for example. However, it is not suitable for files in use, such as databases or virtual machines, as in this case you will get corrupted (unusable) file copies.
rsync is not usually pre-installed, so the first thing to do is to install it in your Linux distribution.
user@host:~$ sudo apt install rsync
[root@host ~]# pacman -S rsync
Here are a few examples of how rsync can be used.
Let's start by using rsync to copy a file from our /home/std/ folder to /tmp/.
user@host:~$ rsync -av /home/std/myfile_source /tmp/myfile_dest
We can also use rsync to copy a folder recursively. Here, we'll copy our entire /home/std folder to /tmp/.
user@host:~$ rsync -av /home/std/ /tmp/folder_dest
Even more interestingly, we can use rsync to copy files or folders over the network. For this to work, both rsync and SSH must be installed on the source and destination servers.
user@host:~$ rsync -av -e 'ssh -p 22' /home/std user@192.168.1.100:/data/
user@host:~$ rsync -av -e 'ssh -p 22' user@192.168.1.100:/data/std /home/
As explained above, rsync is particulary well suited to backing up file servers. We'll look at how to back up to USB drive, to a remote host and how to perform incremental backups to save disk space.
Here, we're going to perform a full backup of our GNU/Linux operating system. We will exclude all special or unnecessary folders (/dev, /mnt, /proc, /sys and /tmp). The destination folder on the external hard drive will be automatically named with the current date in back-YYYY-MM-dd format.
root@host:~# rsync -av --delete --delete-excluded --exclude=/tmp --exclude=/proc --exclude=/sys --exclude=/dev --exclude=/mnt / /mnt/usb/back-$(date "+%Y-%m-%d")
One of rsync's strengths is its ability to perform incremental backups. To do this, it uses hardlinks. For each file to be backed up, rsync compares it with the last backup (the current folder on the external hard drive, which is a symbolic link to the folder of the last backup): if the file is identical, it creates a hard link and consumes no disk space, otherwise (if the file is new or different) it creates a new file.
Of course, the current symbolic link must be created manually and recreated after each backup so that it points to the latest backup.
As you can see from the diagram, the first backup will occupy the most space (10 GB here), then the other backups will occupy only the difference (1 GB or 2 GB in this example).
One last thing to know: in the event we want to reclaim space on our external hard drive, we'll need to delete the backups from the oldest to the most recent (to avoid breaking hardlinks).
This is detailed application of what was seen above.
user@host:~$ rsync -av --delete --delete-excluded --exclude=/tmp --exclude=/proc --exclude=/sys --exclude=/dev --exclude=/mnt --exclude=/media --link-dest=/mnt/USB/current / /mnt/usb/back-$(date "+%Y-%m-%d")
user@host:~$ rm -f /mnt/usb/current
user@host:~$ ln -s /mnt/usb/back-$(date "+%Y-%m-%d") /mnt/usb/current
user@host:~$ du -sh /mnt/usb/back-*
10G back-2022-08-11
1G back-2022-08-12
2G back-2022-08-13
1G back-2022-08-14
Same as above, except that this time the destination is a GNU/Linux server and the backup will take place over the network.
user@host:~$ rsync -av --delete --delete-excluded --exclude=/tmp --exclude=/proc --exclude=/sys --exclude=/dev --exclude=/mnt --exclude=/media --link-dest=/backup/current / user@192.168.1.100:/backup/back-$(date "+%Y-%m-%d")
user@host:~$ ssh -p 22 user@192.168.1.100 "rm /backup/current"
user@host:~$ ssh -p 22 user@192.168.1.100 "ln -s /backup/back-$(date "+%Y-%m-%d") /backup/current"
I've listed everything I found useful here.
user@host:~$ rsync --progress -av /home/std/folder_source/ remoteuser@192.168.1.100:/tmp/folder_dest
user@host:~$ rsync --stats -av /home/std/folder_source/ remoteuser@192.168.1.100:/tmp/folder_dest
user@host:~$ rsync --progress -av --bwlimit=100k /home/std/folder_source/ remoteuser@192.168.1.100:/tmp/folder_dest
Note: requires local or ssh transfer without password to work.
user@host:~$ rsync --progress -av remoteuser@192.168.1.100:/ --no-relative --files-from=<(ssh remoteuser@192.168.1.100 find /data/* -mtime -3 -type f) /tmp/
user@host:~$ rsync --progress /home/std/ /tmp/folder_dest
root@host:~# visudo
user ALL=NOPASSWD:/usr/bin/rsync
user@host:~$ rsync -av -e 'ssh -p 22' --rsync-path='sudo rsync' /home/std user@192.168.1.100:/data/
Contact :