How to Transfer and Backup Files With Rsync

How to Transfer and Backup Files With Rsync

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

Here are a few common use cases that would be applicable to most users. More advanced Rsync command require cPanel VPS hosting or cloud and unmanaged VPS level hosting. The below commands should work over SSH with your cPanel user.

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 user@server:/var/www/html

This command uses wildcard expansion (*) grab any file that ends with the .html file extension followed by a host specification (user@server), 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 user@server:/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 /mnt/backupdrive.

Why Use Rsync?

There are many different backup and file transfer alternatives out there. So why use rsync?

Delta Transfers

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.

Scripting Capabilities

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.

Helpful Options

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 scalable Cloud VPS Hosting.

check markCentOS, Debian, or Ubuntu check markNo Bloatware check markSSH and Root Access

CM
Christopher Maiorana Content Writer II

Christopher Maiorana joined the InMotion community team in 2015 and regularly dispenses tips and tricks in the Support Center, Community Q&A, and the InMotion Hosting Blog.

More Articles by Christopher

One thought on “How to Transfer and Backup Files With Rsync

  1. I was trying to set this up as a cron job…what worked is:

    rsync -a /home/user/public_html/images/ /home/user/public_html/images_backup/

    Replace /user/ with your account username.
    Keep forward slashes at the end to copy the files from one dir to the other.
    If you do not have the / on the first path, it will copy the folder instead of the individual files.

Was this article helpful? Join the conversation!