Sync two local directories with rsync
The command line application rsync is a very useful utility for synchronizing files and directories between two computers, two harddisks or even two directories on the same file system.
My most common use-case is making backup of my workstation to an external harddrive, essentially from one directory to another as follows.
rsync -av --progress --delete /home/cairne/ /media/cairne/WD
The flags used are:
-a, --archive Make identical copy of the file
--delete Delete files from destination not in the source
-v, --verbose Increase output message verbosity
--progress Show progress of synchronization
The trailing slash on the source directory tells rsync to process the contents of the directory, not the directory itself.
Excluding files from synchronization
Using the --exclude
option you can remove items from the process.
rsync -zav --exclude 'target' src/javamail backup@nas.local:/var/backup
If the number of files to exclude is large, the exclusion patterns can instead
be read from a file using the --exclude-from
option.
The -z
option enables compression, which speeds up the process slightly when
synchronizing over the network.
Bandwidth limit
Synchronizing large amounts can make your connection slow for other application,
so to limit the throughput use the --bwlimit
option.
rsync -zav --bwlimit 1000 src/javamail backup@nas.local:/var/backup
This limits the transfer to 1 megabyte per second.
rsync with a time limit
With the option --time-limit
you can force rsync to stop the process after a
specified amount of minutes since start.
rsync -zav --time-limit 30 src/javamail backup@nas.local:/var/backup
Another option, useful for scheduled operations is the --stop-at
option.
rsync -zav --stop-at '2019-12-28T06:45' src/linux backup@nas.local:/backup
This option will stop rsync at 6:45 in the morning of 28th December if it is still running at the time. Useful for large jobs spanning several hours.