Rsync is a powerful tool for copying files to and from remote servers or between various networks machines. You can use it for copying files locally, or to backup your files to your remote server. You can also use it as a command line replacement for FTP when transferring your website files to a server.
- Basic Rsync Usage
- Why Use Rsync?
- Helpful Options
Basic Rsync Usage
Here are a few common use cases that would be applicable to most users.
How to Transfer Website Files With Rsync
In this example, imagine you have a server hosting your website files in the
/var/www/hmtl directory, and you want to transfer only local HTML files to that directory on the server. This is the most basic usage of rsync but demonstrates the full capabilities of the program.
rsync *.html --delete [email protected]:/var/www/html
This command uses wildcard expansion (
*) grab any file that ends with the
.html file extension followed by a host specification (
[email protected]), a colon (
:), and finally the server file path into which you want rsync to place your files. The
--delete option will remove any files from the destination that have been deleted at the source. This means if you had deleted one of those HTML files locally it will also be deleted from the server. This is what differentiates rsync from many other file transfer systems, it helps keep your files clean and organized.
How To Create a Local Backup Of Your Website Files
Now imagine taking the above situation in the reverse: going up to your website files as the source and downloading them to a local destination. This is an effective and easy way of backing up your website files.
rsync -av --delete [email protected]:/var/www/html /mnt/backupdrive
In this instance, we are asking rsync to take the contents of the
/var/www/html directory (which contains all of your website files) and delivering them to a local destination: a hard drive mounted to
Why Use Rsync?
There are many different backup and file transfer alternatives out there. So why use rsync?
After repeated use, rsync is faster than other command line file transfer tools (scp, for example) because it checks your files before transfer and only copies data that has been updated at the source. This means that subsequent backups and file transfers will be much faster after the initial transfer. Even if you are moving large media files, rsync is capable of handling just about anything you throw at it.
Because rsync runs in your command line, you can save your transfer in a script. This allows you to run a transfer whenever you want without having to remember the options or risk typing in a wrong file path. You can also schedule your transfers via cron.
As you can imagine, rsync is highly customizable. You can customize virtually every element of the transfer. Below are some helpful options for file transfer that you might want to use in your scripts.
You’ll notice we used the
-a option in our backup script along with a
-v. This copies files recursively, going into each directory down the path provided, and “verbosity” — telling you what is happening. The
--delete option will sync the source and destination by deleting files from the destination that have been deleted from the source. (So if you are going to use the
--delete be aware that it can cause some data loss.)
If you don’t need cPanel, don't pay for it. Only pay for what you need with our Cloud VPS solutions.?
CentOS, Debian, or Ubuntu? ? No bloatware? ? SSH Key management made easy
Managed cPanel VPS hosting makes server administration easy.