rsync stands for “remote sync” and is a powerful command line utility for synchronizing directories either on a local system or with remote machines. It’s built into nearly every Linux system by default which allows users to perform rsync over ssh communication/backup.
There are many backup tools around and many ways how to use them, so why use rsync? For example, it is possible to use gzip and ftp to make a local copy of your web site. This approach have couple drawbacks such as: data is transferred over the internet unencrypted and we are most likely transferring data which we had copied over the day before.
To solve an unencrypted transfer problem, we could use scp instead of ftp. However, this time the transfer time will be even longer, as scp will create an extra overhead of creating encrypted tunnel for our backup internet connection. To stop transferring duplicate data, we can use rsync.
If we combine rsync with ssh, compression, bash and cron we can end up with an ultimate backup tool. In this tutorial, you’ll see how to use rsync over ssh for incremental backups on Linux.
In this tutorial you will learn:
- How to use rsync over ssh
- How to create an rsync backup script in Bash
- How to use rsync to backup a MySQL directory
- How to schedule automatic rsync updates in cron
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
|Conventions||# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
At this point, we need to create passwordless ssh login. By doing this, we can avoid the need of entering password when doing our backup. This way we can make the whole backup process completely automatic. Please follow this tutorial to make ssh login to your server without password.
Most likely, you already have rsync installed on your system. But just in case your distribution doesn’t include it by default, or you had removed the tool at some earlier point in time, you can use the appropriate command below to install rsync with your system’s package manager.
$ sudo apt install rsync
$ sudo dnf install rsync
$ sudo pacman -S rsync
Use rsync over ssh to create backups
With rsync and SSH installed, we can use the tools to create incremental backups. See some of the examples below on how to do it. Of course, you can adapt any of these commands to your own needs by adding or removing command line options.
- Two options that most users like to include in their commands is
-v(verbose). Along with these, you’ll need to supply the
-e sshoption to instruct rsync to connect to a remote system over SSH. Here’s what the most basic syntax would look like.
$ rsync -av -e ssh /src/ user@remote:/path/to/dst
This command would synchronize the source directory
/srcwith the destination directory
- If the remote server is accepting SSH connections on a different port than the default (22), you can use the following command syntax to instruct rsync to connect to that port. This example assumes that SSH is running on port 2200.
$ rsync -av -e 'ssh -p 2200' /src/ user@remote:/path/to/dst
- When using rsync over ssh, the
--progressoptions are very helpful. They will resume partial transfers in case a previous sync is interrupted, and show you the current transfer progress of all files, respectively. You can use the
-Poption to combine both of these features into a single flag.
$ rsync -avP -e ssh /src/ user@remote:/path/to/dst
- Another useful option that only becomes relevant with remote transfers is the
-zflag, which enables file compression. This can save a little bit of bandwidth and speed up data transfers, but will cost your system a bit of CPU usage to compress files before transferring them.
$ rsync -avPz -e ssh /src/ user@remote:/path/to/dst
Use rsync over ssh to backup MySQL database
In case that your website is using database such as mysql we first need to make a database backup, then use rsync to transfer the changes to a remote server. Here’s an example of an rsync script to backup MySQL:
#!/bin/bash # create database backup ssh firstname.lastname@example.org '( mysqldump --password='pass' \ mydatabase > ~/public_html/mywebsite/mydatabase.sql )' rsync -avze ssh --delete\ email@example.com:~/public_html/mywebsite /backup/local-copy
At this point, the script will create a local copy of the remote
~/public_html/mywebsite directory and store it in
–delete option will ensure to delete all files from the remote directory which no longer exist in the source directory, thus keeping both directories in complete sync. rsync’s
-z option ensures a compression during transfer.
We are ready to test our new backup script:
$ chmod 700 backupscript.sh $ ./backupscript.sh
Use cron to schedule rsync backups
If everything works well with your rsync script, we can schedule to run this backup script every day.
For example, to make the script run at 2:00 AM every day, we would open up cron with the following command:
$ crontab -e
And add the following line to cron:
0 2 * * * /path/to/backupscript.sh
In this tutorial, we saw how to use rsync over ssh to create incremental backups and send them to a remote server. Using ssh with our backups ensures that all of our data remains encrypted while it’s in transfer. We also learned about various rsync options which add further convenience to using the command. Creating an rsync script and scheduling it in cron is one of the best ways to create a simple, secure, and efficient backup.