Зарегистрируйтесь сейчас для лучшей персонализированной цитаты!

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

Jan, 29, 2024 Hi-network.com
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 thersynccommand. 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 theip acommand). 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_DIRECTORYhosts allow = LOCAL_IPhosts deny = *list = trueuid = rootgid = rootread 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.

tag-icon Горячие метки: Технологии и оборудование Услуги и программное обеспечение 3. Операционные системы

Copyright © 2014-2024 Hi-Network.com | HAILIAN TECHNOLOGY CO., LIMITED | All Rights Reserved.