Rsync (Remote sync) is a tool to copy and synchronize files and folders on the same server or between different machines on Linux.
What distinguishes Rsync from the classic “copy and paste” is that this tool automatically recognizes which documents differ between two folders, so as to speed up the copying process from a source folder to a destination.
In this article you will learn how to install, configure and use Rsync on Linux Ubuntu 18.04 in order to synchronize files and folders and schedule a periodic backup of your server.
Installation
The rsync packages are already present in the Official Ubuntu Repository. To install it type:
$ sudo apt install rsync
Syntax
$ rsync options /path/source/ /path/destination/
Available options are:
-
-v: the copied files and folders will be shown on the shell
-
-r: to copy the folders and their contents
-
-h: instead of showing the size of the copied files in bytes, an easily readable format will be shown ( rather than 14.167.396 bytes, 14.17 Mb will be seen)
-
-z: to compress the transferred data
-
-a: it shows the original information of the file, such as timestamp, permissions, owner etc.
-
-u: when copying,
-
--delete: rsync will delete the files in the destination folder that are not present in the source folder.
-
--exclude: certain files or folders will be ignored when copying.
-
--progress: shows the percentage of the copying process and the remaining time.
-
--existing: only the files already existing in the destination folder will be copied.
Synchronizing two folders on the same computer
Copying from one folder to another is done with the "rsync" command followed by the source folder (from which the items are copied) and the destination folder. Regardless of the fact that the folder to copy contains folders inside , adding the tag to the "-r" tag is to be preferred:
$ rsync -r path/source path/destination
The entire "source" folder is copied in the "destination" folder.
$ rsync -r ~/source/ ~/destination
The content of the "source" folder is copied in the "destination" folder.
Beware of the difference between the two commands: by writing "/" after the source folder the system is told to copy the files contained in the folder.
Not entering “/” implies the selection of the entire folder and, consequently, the creation of a copy of the source folder in the destination folder.
Copying files of a single format
Rsync also allows the copy of certain types of files only, by specifying the format or name.
Assuming that the files are in "txt" format, all the files in this format can be copied by typing:
$ rsync path/source_folder/*.txt path/destination_folder
With "* .txt" you have expressly requested a copy of "all .pdf files".
Copying files on SSH protocol between two machines
You can also use the SSH protocol to copy your files and folders from your computer to another. The use of this protocol guarantees that, during the transfer, all the files are encrypted and that, therefore, no one will be able to read them.
The command requires the option "-e ssh" followed by the source and the destination of the copy.
$ rsync -r ssh path/source/ user@IP_machine:/path/destination/
The password of the user accessing the target machine will be required.
Copying files based on size
Rsync filters the files to be copied also based on the size that they occupy on the disk.
$ rsync --max-size=15k path/source_folder/*.* path/destination_folder/*.*
Only the files smaller than 15Kb will be copied.
The larger files will be ignored.
$ rsync --min-size=20k ~/source_folder/*.* ~/destination_folder/*.*
Only the files larger than 20Kb will be copied.
The smaller files will be ignored.
Excluding files from copying based on extension
If you don't want to copy some file types, you can use the tag “--exclude” to exclude some files from copying based on extension.
In the example below, files in .PDF format will not be copied from the "source" folder to the "destination" folder.
$ rsync -r --exclude="*.pdf" path/source_folder/ path/destination_folder
Removing content from source folder after transfer
Likewise the concept of "cut and paste", with rsync files can be moved from a source folder to a destination folder. In this case, the files are copied in the destination folder and then removed from the source folder.
$ rsync -r --remove-source-files path/source_folder path/destination_folder
Setting transfer rate limit
In the event of copying through the network, you may need to set an upload speed limit for your files. In this case, use the "--bwlimit = KBPS" tag, replacing "KBPS" with the desired transfer rate.
$ rsync --bwlimit=KBPS -r ssh source_path/folder user@IP_receiver:/destination_path
Replace "KBPS" with the maximum transfer speed value expressed in KB / s (2048 KB/s = 2MB/s)
Making periodic and automated copies
In the event of using rsync to make a backup but do not want to type the commands every time, there is a tool perfect for you: Cron.
Already present in Ubuntu 18.04, this service allows to set some "rules" for the periodic repetition of some commands.
To start creating your rules and then start setting up your automatic copying process, type:
$ crontab -e
A text editor where to indicate which command to repeat on a regular basis will open. Once set the rules, save and close it.
How to configure a crontab
The writing of the crontab must follow a precise syntax, specifying 6 ordered parameters:
m h dom mon dow command
-
m: minutes (0-59)
-
h: hours (0-23)
-
dom: day of the month (1-31)
-
mon: month (1-12)
-
dow: day of the week (0:sunday - 6:saturday)
-
command: command to execute
Examples of possible configurations:
30 * * * * rsync path/source/*.pdf path/destination
(every month, every day and every hour at 30 minutes, all ".pdf" files in the "source" folder will be copied in the "destination" folder)
*/2 * * * * rsync path/source/*.pdf path/destination ( all the .pdf files present in the "source" folder in the "destination" folder are copied every day of the month, every 2 minutes)
50 8 * * 1,3 rsync path/source/*.pdf path/destination (the command to copy .pdf files in the "source" folder in the "destination" folder is executed from Monday to Wednesday at 8:50am)
Once saved and closed the editor, to see the list of crontabs type:
$ crontab -l