Back up a Linux directory to a remote machine with rsync

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. 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). 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.
This site uses cookies to offer you a better browsing experience. By browsing this website, you agree to our use of cookies.