X
Tech
Why you can trust ZDNET : ZDNET independently tests and researches products to bring you our best recommendations and advice. When you buy through our links, we may earn a commission. Our process

'ZDNET Recommends': What exactly does it mean?

ZDNET's recommendations are based on many hours of testing, research, and comparison shopping. We gather data from the best available sources, including vendor and retailer listings as well as other relevant and independent reviews sites. And we pore over customer reviews to find out what matters to real people who already own and use the products and services we’re assessing.

When you click through from our site to a retailer and buy a product or service, we may earn affiliate commissions. This helps support our work, but does not affect what we cover or how, and it does not affect the price you pay. Neither ZDNET nor the author are compensated for these independent reviews. Indeed, we follow strict guidelines that ensure our editorial content is never influenced by advertisers.

ZDNET's editorial team writes on behalf of you, our reader. Our goal is to deliver the most accurate information and the most knowledgeable advice possible in order to help you make smarter buying decisions on tech gear and a wide array of products and services. Our editors thoroughly review and fact-check every article to ensure that our content meets the highest standards. If we have made an error or published misleading information, we will correct or clarify the article. If you see inaccuracies in our content, please report the mistake via this form.

Close

How to back up a Linux directory to a remote machine with rsync

Backing up a local directory to one on a remote machine on Linux is fast and flexible with this Linux command line tool.
Written by Jack Wallen, Contributing Writer
keyboard-fingers2gettyimages-1010651100
gorodenkoff/Getty Images

Linux is the most flexible operating system on the market. With this open-source platform, you can do far more than you can with your proprietary OS, without spending a dime on software.

When taking your first steps with Linux, you'll probably want to avoid the command line because it can be a bit daunting. Eventually, however, you might find you're ready to see just how much more power and flexibility you can eke out of your distribution of choice. One very handy (and useful) thing you can do is learn how to back up a local directory to a remote one with the help of the rsync command. And that's exactly what I'm going to show you.

Ready? Let's do it.

Also: Here's another reason why Linux is way cooler than your operating system

How to back up a Linux directory with rsync 

What you'll need: To make this work, you'll need two instances of Linux, one for the local (that houses the directory you want to back up) and one for the remote (that you'll back the directory up to). You'll also need a user with sudo privileges on both machines. Finally, you'll need to know the IP addresses of both machines (which can be found with the ip a command). I'll demonstrate this on two Ubuntu-based machines. If you're using a Fedora or Arch-based distribution, the only thing you'll need to alter is the installation command.

1. Install rsync on both machines

The first thing to do is install rsync, which can be achieved with the following command:

sudo apt-get install rsync -y

2. Configure rsync on the remote machine

Next, we need to configure rsync on the remote machine. Create a new configuration file with the command:

sudo nano /etc/rsyncd.conf

In that file, paste the following content:

[backup]
path=REMOTE_DIRECTORY
hosts allow = LOCAL_IP
hosts deny = *
list = true
uid = root
gid = root
read only = false

Where REMOTE_DIRECTORY is the directory on the remote machine that will house the backed-up files and LOCAL_IP is the IP address for the local machine.

Also: The best Linux distributions for beginners: Expert tested and reviewed

Save and close the file with the Ctrl+X keyboard shortcut.

Start and enable rsync with the command:

sudo systemctl enable --now rsync

3. Run your backup

We'll now test the backup process. On your local machine, you'll run the rsync command like this:

rsync -avz LOCAL_DIRECTORY REMOTE_IP::backup

Where LOCAL_DIRECTORY is the directory you want to back up and REMOTE_IP is the IP address of the remote machine. Notice the ::backup. That is the name of the backup we used in the configuration file on the remote machine (the line [backup]). The backup should run and complete fairly quickly (unless you have a large amount of files in the directory).

Also: Why I use multiple operating systems to get my work done 

Automate the backup

As I said, Linux is very flexible. We can automate this process with the help of the built-in cron tool. What we'll do is create a bash script for the backup with the command:

nano rsync.sh

In that file, type the same command you used earlier to run the backup, only we'll add the q option to suppress output, so it looks like this:

rsync -avzq LOCAL_DIRECTORY REMOTE_IP::backup

Save and close the file. Give the file executable permissions with the command:

chmod u+x rsync.sh

Now, we'll create a cron job with the command:

sudo crontab -e

In that file, paste the following:

00 01 * * * /home/USER/rsync.sh

Where USER is your username. Save and close the file.

Your new cron job will run the rsync backup daily at 1 a.m., so you always have a fresh backup of that directory.

Also: This could be the best Linux distro of the year (and it's not even close)

And that, my friends, is all there is to creating a basic remote backup job on Linux.

Editorial standards