In this article, we will delve into the process of setting up the rsync daemon for continuous directory synchronization between two servers. This is an essential procedure for system administrators who need to ensure that files and directories are mirrored across different servers.
To start the Rsync daemon for continuous directory synchronization between two servers, you need to configure the Rsync daemon on the server hosting the directory you want to synchronize. This involves creating a configuration file, setting up authentication, and enabling the Rsync daemon. Once set up, you can use the Rsync command to synchronize the directories between the servers.
Introduction to Rsync
Rsync is a powerful tool that provides fast, incremental file transfer and synchronization between different hosts. It can significantly improve file transfer speed and efficiency by only sending the changes in the files instead of sending whole files every time.
Setting Up the Rsync Daemon
To start, we need to set up the rsync daemon on the server that hosts the directory you want to synchronize (server A). This daemon will allow the other server (server B) to connect and synchronize the directory.
Step 1: Configuration of Rsync Daemon
On server A, create a file named
rsyncd.conf in the
/etc directory. This file will contain the configuration for the rsync daemon. Here’s an example of what it might look like:
uid = rsync
gid = rsync
use chroot = no
pid file = /var/run/rsyncd.pid
comment = share
path = /etc/dirtosync
read only = false
list = yes
hosts allow = *
auth users = rsync
secrets file = /etc/rsyncd.secrets
In this configuration:
gidspecify the user and group ID that the daemon will run as.
use chrootspecifies whether the daemon should use chroot to sandbox the rsync process.
pid filespecifies the location where the daemon will write its process ID.
[share]is the name of the share that will be accessible to server B.
pathis the directory that will be shared.
read onlyspecifies whether the share should be read-only.
listspecifies whether the share should be listed when the client requests a list of shares.
hosts allowspecifies which hosts are allowed to access the share.
auth usersspecifies which users are allowed to access the share.
secrets filespecifies the file that contains the usernames and passwords for authentication.
Step 2: Creating the Secrets File
Next, create a file called
rsyncd.secrets in the
/etc directory with the following content:
This file contains the usernames and passwords for authentication. The format is
Step 3: Setting Permissions
Ensure that the rsync daemon has the necessary permissions. You can set the owner and group of the directory
rsync using the following command:
chown rsync:rsync /etc/dirtosync
Step 4: Enabling the Rsync Daemon
On server A, edit the file
/etc/default/rsync and set the value of
true. This will enable the rsync daemon to start at boot.
Step 5: Starting the Rsync Daemon
You can start the rsync daemon on server A using the command:
With the rsync daemon set up on server A, you can now synchronize the directory from server A to server B.
The command to do this on server B is:
rsync -avzr rsync@<serverA>:/etc/dirtosync/* /etc/dirtosync/
In this command:
-astands for “archive”, which preserves permissions, ownerships, and timestamps, and recurses into subdirectories.
-vstands for “verbose”, which provides detailed information about the files being transferred.
-zstands for “compress”, which compresses the data before sending it.
-rstands for “recursive”, which copies directories recursively.
The command above will only synchronize the files once. To continuously synchronize the files, you can set up a cron job on server B to run the rsync command at regular intervals.
For example, you can create a cron script in
/etc/cron.daily that contains the rsync command. This will run the rsync command once a day.
Setting up the rsync daemon for continuous directory synchronization between two servers can be a complex task, but it’s an essential skill for any system administrator. By following the steps outlined in this article, you should be able to set up the rsync daemon and synchronize directories between two servers.
The rsync daemon is used to facilitate continuous directory synchronization between two servers by allowing them to connect and transfer only the changes in files instead of sending the entire files.
Rsync improves file transfer efficiency by sending only the changes made to files instead of sending the whole file. This significantly reduces the amount of data that needs to be transferred, resulting in faster synchronization.
The rsync daemon configuration file,
rsyncd.conf, should be created in the
/etc directory on the server that hosts the directory you want to synchronize.
To enable the rsync daemon to start at boot, you need to edit the file
/etc/default/rsync and set the value of
The owner and group of the directory to be synchronized should be set to
rsync using the
chown command. For example:
chown rsync:rsync /etc/dirtosync.
To synchronize directories between server A and server B, you can use the command:
rsync -avzr rsync@<serverA>:/etc/dirtosync/* /etc/dirtosync/. This command will copy the files from server A to server B.
To set up continuous synchronization, you can create a cron job on server B that runs the rsync command at regular intervals. For example, you can create a cron script in
/etc/cron.daily that contains the rsync command to run it once a day.