07.06.2023

How to Use Rsync to Create a Backup on CentOS 8

In this tutorial, we will use Rsync to create a backup on CentOS 8. Backups will be sent to the second server and stored there. So we need 2 working and configured CentOS 8 servers.

Installing Rsync packages

Let's check if the necessary Rsync packages are installed and install them if not.

dnf install rsync rsync-daemon

Start the Rsync daemon and enable its automatic starting.

systemctl enable --now rsyncd

Check if auto start is enabled and working.

systemctl status rsyncd
● rsyncd.service - fast remote file copy program daemon
Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2020-12-19 13:18:16 UTC; 6s ago
...

Check if the Rsync service is listening on the network port.

netstat -tulpn | grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 55746/rsync
tcp6 0 0 :::873 :::* LISTEN 55746/rsync

Configuring the backup source server

Open the configuration file:

nano /etc/rsyncd.conf

Insert following lines in it:

# Rsync daemon global config
pid file = /var/run/rsyncd.pid
# User name and group for reading source files
uid = rsync-user
gid = rsync-user
# Do not allow to modify the source files
read only = yes
# Data Source Configuration
[data]
path = /home/user
list = yes
auth users = rsync-user
secrets file = /etc/secret.passwd

Create a file with credentials for authorization on this machine.

nano /etc/secret.passwd

Enter username and password in it.

rsync-user:rsync-pass

Change the file permissions to read-only by the root user.

chmod 0600 /etc/secret.passwd

Restart the Rsync daemon.

systemctl restart rsyncd

You also need to create the user that was specified in the configuration and file with credentials.

useradd rsync-user

Сonfiguring the receiving Rsync backup server

Create a file for the password.

nano /etc/secret.passwd

Enter the password for the Rsync user on the source server.

rsync-pass

Change its permissions here as well.

chmod 0600 /etc/secret.passwd

Running a backup

Create a folder for backup.

mkdir /opt/backup

The following command will start the backup. Replace source-server-ip with the source server's IP address.

rsync -a --password-file=/etc/secret.passwd rsync-user@source-server-ip::data /opt/backup/$(date +%Y-%m-%d)/

To set up periodic backups, you can add a cron task with the last command in it.