Copy and backup files and folders using the Rsync utility

More about Rsync

Rsync is a fast and versatile tool for copying files and folders. The utility can copy data both locally and to other hosts, including Cloud Storage from Introserv.
The utility options allow you to control many aspects of data copying and mirroring.
One of the main features is the ability to compress data while sending, which allows you to speed up the copying process between servers.
Another advantage is the exception when copying identical files. Therefore, only different files will be copied or transferred, which will undoubtedly save time synchronizing large directories.

Utility options

Let's look at examples of using Rsync to copy files and directories to remote Cloud Storage from Introserv using the SSH protocol. Important! Similar commands, only specifying the target server, will allow you to copy directories and files to any remote server via SSH.

Let's take a look at some of the main utility options that will be used most often. A more detailed list of available options is available in the man documentation (man rsync command):

-v – display detailed information about the process
-c – check file checksums
-q – minimal information
-a – archiving mode
-R – relative paths
-y – do not overwrite newer files
-b – create a backup copy
-l – copy symlinks
-L – copy contents of links
-H – copy hard links
-g – save group
-p – preserve file permissions
-t – save modification time
-x – work only in this FS
-e – use another transport protocol (for example, ssh)
-z – compress files before transfer
--delete – delete files that are not in the source
--exclude – exclude files
--recursive – enumerate directories recursively
--no-recursive – disable recursion
--progress – display file transfer progress
--stat – show transfer statistics
--max-size – maximum file size for transfer
--bwlimit — speed limit for file transfers

Brief syntax explanation

rsync -az --progress /home/user/test.file boxxxx@boxxxxx.introserv.cloud:/remote_folder/

-az --progress options that indicate that it is necessary to archive the transferred file, compress it before transfer, and also display the copying progress on the screen;

/home/user/test.file  this is the path to the file and the actual file itself that needs to be transferred;

boxxxx@boxxxxx.introserv.cloud: this is the destination resource (in our example it is cloud storage, but it can also be a remote server) boxxxx is the user name, after the sign @ is the resource name or IP address of the remote server;

/remote_folder/ this is a folder on a remote resource. In our example - in Introserv Cloud Storage.

Copy one file to a folder on a remote storage

rsync -az --progress /home/user/Desktop/test.file boxxxxx@boxxxxx.introserv.cloud:/remote_folder/

You will be prompted for a password for connecting to the storage:

Enter the password and continue, you will see the progress and file transfer speed:

Copy folder to the folder on a remote storage

Pay attention to the slashes in the source folder and destination folder lines. The absence of a slash in the source folder indicates that we want to recursively transfer the local Desktop folder with all its contents to the remote_folder folder

rsync -azr --progress /home/user/Desktop boxxxx@boxxxx.introserv.cloud:/remote_folder/

You will see the directory and all the files inside it being transferred:

Copy the contents of a local folder to a folder on a remote storage

Pay attention to the slash at the end of the source folder. Its presence indicates that the utility should copy the contents of the local folder to a folder on the remote storage

rsync -avzr --progress /home/user/Desktop/ boxxxx@boxxxx.introserv.cloud:/remote_folder/

You will see the progress and copying speed:

Copying a folder from a remote storage to a local server folder

Using Rsync, you can initiate a copy of a folder from a remote storage. The command looks like this:

rsync -zra --progress boxxxxx@boxxxxx.introserv.cloud:/remote_folder /home/user/Desktop/

You will see the progress and speed of copying a remote folder to a folder on the local server:

Copying the contents of a folder from a remote storage to a local server folder

Here it is also worth paying attention to the slash at the end of the source folder path. Its presence indicates that only the contents of the folder need to be copied

rsync -zra --progress boxxxxx@boxxxxx.introserv.cloud:/remote_folder/ /home/user/Desktop/

Copying the contents of a folder excluding and/or including files

The utility allows you to explicitly specify which files to transfer and which should be excluded. To do this, use the --include and --exclude options.

rsync -azr --progress --include 'test.file' --exclude 'test.file2' /home/user/Desktop/ boxxxx@boxxxx.introserv.cloud:/remote_folder/

You will see that only one file is transferred, specified by the --include option. Please note that the include and exclude options can be used separately from each other

Copy only files that are different from the files in the destination folder

To cancel copying identical files, you should use the -c option to check file checksums. This will allow you to exclude from the copying process those files that are already in the destination folder

rsync -azrс --progress /home/user/Desktop/ boxxxx@boxxxx.introserv.cloud:/remote_folder/

You will see that only 3 files were copied to the remote folder, with the exception of the test.odt file, which is already on the remote storage:

Using Rsync over SSH with a non-standard SSH port

To transfer files from/to a server with a changed SSH port, use the -e 'ssh -p port_number' option

The command to copy a file to a server on which the SSH port is changed to 44 will look like this:

rsync -azrс --progress -e 'ssh -p44' /home/user/Desktop/ user@server_ip:/remote_folder/