HostArmada Web Hosting Knowledge Base

Knowledge is power! Use that power and achieve total and unconditional control over the Web Hosting Services!

The rsync command.

Rsync (Remote Sync) is an exceptionally versatile tool for copying and synchronizing files/directories in local or remote environments. Mastering this command is going to help you effectively transfer data across different servers or generate and restore backups. What makes it so great is the large number of options, allowing control over its behavior, which enables you to tune it to your needs. Additionally, it uses the "delta-transfer algorithm" that reduces the amount of bandwidth transferred over the network by sending the difference between the source and the destination files only. We are going to cover only a few examples in this article, which can hopefully teach you the fundamentals of this command-line utility and help you accomplish your goals.

The most basic syntax of the command is as follows:

rsync <option> <source> <destination>

Copy files/directories in a local environment

Using rsync, you can easily create backups of your website by copying the files from its root directory to a folder designated for backup storage. On a cPanel server, the primary domain name's files are located within the "public_html" directory inside the home dir of the user. The backups are stored in the "/backup" global directory.

Here is the command you can use:

rsync -aHvz /home/user/public_html/ /backup/website-backup 

Please notice the trailing slash at the source. If the trailing slash is not placed there, rsync is going to create the "website-backup" folder, and then it is going to copy the "public_html" directory with all its files inside. The trailing slash at the destination does not change the outcome of the sync process.

Now, let's go over the options we use for this command:

  • a - adding this option is going to enable "archive mode". The archive mode preserves all file permissions, ownerships and modification times of files and folders.
  • H - adding this option is going to preserve hard links.
  • v - adding this is going to provide more information while the sync is ongoing.
  • z - adding this option is going to compress the transferred data, reducing the amount of bandwidth transmitted.

Copy files/directories in a remote environment

Let's perform the above example, however, let's use a remote server on which to transfer the backup:

rsync -aHvz /home/user/public_html/ user@backupserver:/backups/website-backup 

By default, rsync is going to use OpenSSH service to transfer the files, as it is the most secure channel. There are cases when some service providers change their SSH ports for security reasons. If this is the case, rsync provides an option to define the new port.

Here is how the command is going to look like:

rsync -e "ssh -p <port>" -aHvz /home/user/public_html/ user@backupserver:/backup/website-backup 

The "-e" flag allows you to define the SSH service's custom port. This command can work in the opposite direction allowing you to pull files from the remote server to your local server. The "pull" process is also known as a "reverse rsync", and it can be used to restore a previous version of your website. Here is how this looks like:

rsync -aHvz user@backupserver:/backup/website-backup/ /home/user/public_html/ 

An additional utility of the rsync command

Did you know that rsync may be used to delete files too? It is one of the fastest if not the fastest way to do that, especially if you are dealing with many files that utilize a huge amount of disk space. To use this option, you need to create an empty directory. The name does not matter in the scenario - the important thing is that it remains empty. Lastly, you have to execute the following command:

rsync -a --delete name-of-empty-folder/ name-of-folder-you-want-to-delete/ 

What rsync is going to start doing is synchronizing the empty directory with the full one, essentially syncing nothing with something, removing all of the files inside the populated directory. Please be very mindful when doing this, as this process is irreversible, and unless you have an available backup, you are not going to be able to recover your data.

Was this article useful and on point?

Find out more about HostArmada entire range of optimized Web Hosting Services and take action today on improving your website Loading Speed, Security, and overall Stability!