Learning Rsync Rocky Linux
Learning Rsync Rocky Linux
Version : 2025/01/05
Table of contents
1. Licence 3
2. Backup Brief 4
3. Preface 10
3.2.1 pull/download 11
3.2.2 push/upload 12
4.1 pull/download 16
4.2 push/upload 16
5. /etc/rsyncd.conf 18
6. Foreword 20
8. Brief 28
8.4 Demo 30
1. Licence
RockyLinux offers Linux courseware for trainers or people wishing to learn how to
administer a Linux system on their own.
SA : Share Alike.
• https://docs.rockylinux.org
• https://github.com/rocky-linux/documentation
Our media sources are hosted at github.com. You'll find the source code repository
where the version of this document was created.
From these sources, you can generate your own personalized training material
using mkdocs. You will find instructions for generating your document here.
You'll find all the information you need to join us on our git project home page.
We wish you all a pleasant reading and hope you enjoy the content.
2. Backup Brief
What is a backup?
Backup refers to the duplication of data in the file system or database. In the event
of an error or disaster, the effective data of the system can be restored in a timely
manner and normal operation.
• Full backup: refers to a one-time copy of all files, folders or data in the hard disk
or database. (Pros: the best, can recover data faster. Disadvantages: take up a
larger hard disk space.)
• Incremental backup: refers to the backup of the data updated after the last full
backup or incremental backup. The process is like this, such as a full backup on
the first day; a backup of the newly added data on the second day, as opposed to a
full backup; on the third day, a backup of the newly added data on the basis of the
second day, relative to the next day, and so on.
• Differential backup: Refers to the backup of the changed files after the full
backup. For example, a full backup on the first day; a backup of the new data on
the second day; a backup of the new data from the second day to the third day on
the third day; and a backup of all the new data from the second day to the fourth
day on the fourth day, and so on.
• Hot backup: Refers to the backup when the system is in normal operation. As the
data in the system is updated at any time, the backed-up data has a certain lag
relative to the real data of the system.
Some people will say, can't I just use the tar or cp command on the first server
and send it to the second server via scp or sftp ?
In a production environment, the amount of data is relatively large. First of all, tar
or cp consumes a lot of time and occupies system performance. Transmission via
scp or sftp also occupies a lot of network bandwidth, which is not allowed in the
actual production environment. Secondly, these commands or tools need to be
manually entered by the administrator and need to be combined with the crontab
of the scheduled task. However, the time set by crontab is not easy to grasp, and it
is not appropriate for data to be backed up if the time is too short or too long.
rsync appears to meet the above needs. It uses the GNU open source license
agreement. It is a fast incremental backup tool. The latest version is 3.2.3
(2020-08-06). You can visit the Official website for more information.
The original rsync was maintained by the Australian programmer Andrew Tridgell
(shown in Figure 1 below), and now it has been maintained by Wayne Davison
(shown in Figure 2 below) ) For maintenance, you can go to github project address
to get the information you want.
note
rsync itself is only an incremental backup tool and does not have the function of real-time data synchronization (it needs
to be supplemented by other programs). In addition, synchronization is one-way. If you want to realize two-way
synchronization, you need to cooperate with other tools.
The core of rsync is its Checksum algorithm. For more information, you can go
to How Rsync works and The rsync algorithm. This section is beyond the author's
competence and will not be covered too much.
• Can selectively retain file synchronization attributes, such as hard link, soft link,
owner, group, corresponding permissions, modification time, etc., and can retain
some of the attributes;
• Support two protocols for transmission, one is ssh protocol, the other is rsync
protocol
3. Preface
• SSH protocol verification login method: use SSH protocol as the basis for user
identity authentication (that is, use the system user and password of GNU/Linux
itself for verification), and then perform data synchronization.
• rsync protocol verification login method: use rsync protocol for user identity
authentication (non-GNU/Linux system users, similar to vsftpd virtual users), and
then perform data synchronization.
Before the specific demonstration of rsync synchronization, you need to use the
rsync command. In Rocky Linux 8, the rsync rpm package is installed by default,
and the version is 3.1.3-12, as follows:
The author's personal use: rsync -avz original location target location
graph LR;
RockyLinux8-->|pull/download|Fedora34;
Fedora34-->|push/upload|RockyLinux8;
graph LR;
RockyLinux8-->|push/upload|Fedora34;
Fedora34-->|pull/download|RockyLinux8;
tip
Here, both Rocky Linux 8 and Fedora 34 use the root user to log in. Fedora 34 is the client and Rocky Linux 8 is the server.
3.2.1 pull/download
Since it is based on the SSH protocol, we first create a user in the server:
On the client side, we pull/download it, and the file on the server is /rsync/aabbcc
[root@fedora ~]# ls
aabbcc
tip
If the server's SSH port is not the default 22, you can specify the port in a similar way--- rsync -avz -e 'ssh -p [port]' .
3.2.2 push/upload
First check the permissions of the /rsync/ directory. Obviously, there is no "w"
permission. We can use setfacl to give permission:
mask::rwx
other::rx
How to do it?
Just write the corresponding parameters and values in the configuration file. In
Rocky Linux 8, you need to manually create the file /etc/rsyncd.conf.
Some parameters and values of this file are as follows, here has more parameter
descriptions:
Item Description
read only = yes yes means read only, no means read and write
auth users = li Enable virtual users and define what a virtual user is called. Need to create it yourself
secrets file = /etc/ Used to specify the location of the virtual user's password file, which must end in .db. The
rsyncd_users.db content format of the file is "Username: Password", one per line
tip
Write some file content to /etc/rsyncd.conf, and write the user name and password
to /etc/rsyncd_users.db, the permission is 600
You may need to dnf -y install rsync-daemon before you can start the service:
systemctl start rsyncd.service
4.1 pull/download
success! In addition to the above writing based on the rsync protocol, you can also
write like this: rsync://[email protected]/share
4.2 push/upload
You are prompted that the reading error is related to the "read only = yes" of the
server . Change it to "no" and restart the service
[root@Rocky ~]# systemctl restart rsyncd.service
rsync error: some files/attrs were not transferred (see previous errors) (code
23) at main.c(1330) [sender = 3.2.3]
Our virtual user here is li, which is mapped to the system user nobody by default.
Of course, you can change it to other system users. In other words, nobody does
not have write permission to the /rsync/ directory. Of course, we can use
[root@Rocky ~]# setfacl -mu:nobody:rwx /rsync/ , try again, and succeed.
5. /etc/rsyncd.conf
In the previous article rsync demo 02 we introduced some basic parameters. This
article is to supplement other parameters.
Parameters Description
fake super = yes yes means that you do not need the daemon to run as root to store the complete attributes of the file.
uid = user id
gid = Two parameters are used to specify the user and group used to transfer files when running the rsync
daemon as root. The default is nobody
use chroot = yes Whether the root directory needs to be locked before transmission, yes yes, no no. In order to
increase security, rsync defaults to yes.
max connections = 4 The maximum number of connections allowed, the default value is 0, which means that there is no
restriction
lock file = /var/run/ The specified lock file, which is associated with the "max connections" parameter
rsyncd.lock
transfer logging = Whether to enable ftp-like log format to record rsync uploads and downloads
yes
timeout = 900 Specify the timeout period. If no data is transmitted within the specified time, rsync will exit directly.
The unit is seconds, the default value is 0 means never time out
ignore nonreadable Whether to ignore files to which the user does not have access rights
= yes
motd file = /etc/ Used to specify the path of the message file. By default, there is no motd file. This message is the
rsyncd/rsyncd.motd welcome message displayed when the user logs in.
hosts allow = Used to specify which IP or network segment clients are allowed to access. You can fill in the ip,
10.1.1.1/24 network segment, host name, host under the domain, and separate multiples with spaces. Allow
everyone to access by default
hosts deny = Which ip or network segment clients specified by the user are not allowed to access. If hosts allow
10.1.1.20 and hosts deny have the same matching result, the client cannot access eventually. If the client's
address is neither in the hosts allow nor in the hosts deny, the client is allowed to access. By default,
there is no such parameter
auth users = li Enable virtual users, multiple users are separated by commas in English state
syslog facility = Define the level of system log. These values can be filled in: auth, authpriv, cron, daemon, ftp, kern,
daemon lpr, mail, news, security, syslog, user, uucp, local0, local1, local2 local3, local4, local5, local6 and
local7. The default value is daemon
/etc/rsyncd.conf
uid = nobody
gid = nobody
address = 192.168.100.4
use chroot = yes
max connections = 10
syslog facility = daemon
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
lock file = /var/run/rsyncd.lock
[file]
comment = rsync
path = /rsync/
read only = no
dont compress = *.gz *.bz2 *.zip
auth users = li
secrets file = /etc/rsyncd users.db
6. Foreword
With inotify-tools, this program tool can realize one-way real-time synchronization.
Since it is real-time data synchronization, the prerequisite is to log in without
password authentication.
First, generate a public key and private key pair on the client, and keep pressing
Enter after typing the command. The key pair is saved in the /root/.ssh/ directory.
Then, use the scp command to upload the public key file to the server. For
example, I upload this public key to the user testrsync
tip
On the client side, the rsync service prepares an environment variable for the
system-RSYNC_PASSWORD, which is empty by default, as shown below:
[root@fedora ~]#
If you want to achieve password-free authentication login, you only need to assign
a value to this variable. The value assigned is the password previously set for the
virtual user li. At the same time, declare this variable as a global variable.
Try it, success! No new files appear here, so the list of transferred files is not
displayed.
tip
You can write this variable into /etc/profile to make it take effect permanently. The content is: export RSYNC_PASSWORD=13579
Append the environment variable PATH, write it to the configuration file and let it
take effect permanently.
Why not use the inotify-tools RPM package of the EPEL repository? And the
way to use source code to compile and install?
You can adjust the kernel parameters according to the needs of the production
environment. By default, there are three files in /proc/sys/fs/inotity/
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
move There are files or directories that are moved to or removed from the monitoring directory
Type the command in the first terminal pts/0, and the window is locked after
pressing Enter, indicating that it is monitoring
In the second terminal pts/1, go to the /rsync/ directory and create a file.
tip
We are operating in Rocky Linux 8 server, using SSH protocol for demonstration.
For the password-free authentication login of the SSH protocol, please refer to
rsync password-free authentication login, which is not described here. An example
of the content of a bash script is as follows. You can add different options after the
command according to your needs to meet your needs. For example, you can also
add --delete after the rsync command.
#!/bin/bash
a="/usr/local/inotify-tools/bin/inotifywait -mrq -e modify,move,create,delete /
rsync/"
b="/usr/bin/rsync -avz /rsync/* [email protected]:/home/testfedora/"
$a | while read directory event file
do
$b &>> /tmp/rsync.log
done
tip
When using the SSH protocol for data synchronization transmission, if the SSH service port of the target machine is not 22, you can
use a method similar to this—— b="/usr/bin/rsync -avz -e 'ssh -p [port-number]' /rsync/* [email protected]:/home/
testfedora/"
tip
If you want to start this script at boot [root@Rocky ~]# echo "bash /root/rsync_inotify.sh &" >> /etc/rc.local [root@Rocky ~]# chmod
+x /etc/rc.local
If you are using the rsync protocol for synchronization, you need to configure the
rsync service of the target machine, please refer to rsync demo 02, rsync
configuration file, rsync free Secret authentication login
8. Brief
• Both Rocky Linux 8 and Fedora 34 require source code compilation and
installation inotify-tools, which is not specifically expanded here.
• Both machines must be password-free login authentication, here we use the SSH
protocol for
tip
The configuration files of the two machines /etc/ssh/sshd_config should be opened PubkeyAuthentication yes
Ocaml is a programming language, and the bottom layer of unison depends on it.
src/unison
[root@fedora /usr/local/src/unison-2.51.4]# cp -p src/unison /usr/local/bin
8.4 Demo
tip
For two-way synchronization, the scripts of both machines must be started, otherwise an error will be reported.
tip
If you want to start this script at boot [root@Rocky ~]# echo "bash /root/unison1.sh &" >> /etc/rc.local
[root@Rocky ~]# chmod +x /etc/rc.local
tip
If you want to stop the corresponding process of this script, you can find it in the htop command and then kill
Learning Rsync On Rocky Linux (English version) Copyright © 2023 The Rocky Enterprise Software Foundation