About rsync
A fast incremental backup tool
Remote Sync
Support local replication or synchronization with other SSH and Rsync hosts
Configure rsync source server
rsync sync source
Refers to the remote server of the backup operation, also known as the backup source
Configure rsync source
Basic thinking
Set up rsync.conf configuration file and independent account file
Enable -- daemon mode of rsync
Application example
User backuper, allowing downlink synchronization
The directory of the operation is / var/www/html
Configuration file rsyncd.conf
It needs to be established manually, and the syntax is similar to Samba configuration
Authentication configuration: auth users, secrets file, anonymous if not added
rsync account file
Use the record format of "user name: password", one user record per line
Independent account data, independent of system account
Enable rsync service
Provide services alone through -- daemon
Execute kill $(cat /var/run/rsync.pid) to shut down the rsync service
Using the rsync backup tool
Usage of rsync command
rsync [options] original location target location
Common options
-a: archive mode, recursion and retention of object properties, etc. for - rlptgoD
-v: display details of the synchronization process
-z: compress when transferring files
-H: keep hard connection files
-A: keep ACL attribute information
--Delete: delete files that exist in the target location but not in the original location
--checksum: decide whether to skip files based on the checksums of objects
Two representations of configuration source
Format 1: user name @ host address:: share module name
Format 2: rsync: / / username @ host address / shared module name
rsync real time synchronization
Lack of periodic synchronization
The backup time is fixed, the delay is obvious, and the real-time performance is poor
When the synchronous source does not change for a long time, intensive periodic tasks are unnecessary
Advantages of real-time synchronization
Start backup as soon as synchronization source changes
Do not perform backup as long as the synchronization source is unchanged
About inotify (installed on the initiator)
Inotify is a Linux feature that monitors file system operations such as read, write, and create. Inotify is sensitive, easy to use, and much more efficient than busy polling for cron tasks.
It can monitor the change of file system and make notification response;
Auxiliary software: inotify tools
Experimental environment
rsyncd server 192.168.13.128
client server 192.168.13.129
1. Modify the configuration file on the rsyncd server
[root@rsyncd ~]# rpm -q rsync
rsync-3.0.9-18.el7.x86_64
[root@rsyncd ~]# vim /etc/rsyncd.conf
uid = nobody ##Anonymous user
gid = nobody
use chroot = yes ##Home detention directory
pid file = /var/run/rsyncd.pid ##pid file
address = 192.168.13.128 ##Monitor address
port = 873 ##Port number
log file = /var/log/rsyncd.log ##log file path
hosts allow = 192.168.13.0/24 ##Allow address segment access
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 ##Types that do not require compression
[wwwroot] ##Shared module name
path = /var/www/html ##Route
comment = www.kgc.com ##Definition name
read only = yes ##read-only
auth users = backuper ##Authentication user name
secrets file = /etc/rsyncd_users.db ##Password file
[root@rsyncd ~]# vim /etc/rsyncd_users.db ##Create password file
backuper:123123 ##User name: password
[root@rsyncd ~]# chmod 600 /etc/rsyncd_users.db ##Give root access to read and write
[root@rsyncd ~]# rsync --daemon ##Start rsync service
[root@rsyncd ~]# netstat -ntap | grep rsync ##View port
tcp 0 0 192.168.13.128:873 0.0.0.0:* LISTEN 36346/rsync
[root@rsyncd ~]# systemctl stop firewalld.service ##Turn off firewall
[root@rsyncd ~]# setenforce 0
[root@rsyncd ~]# yum install httpd -y ##Install httpd service
[root@rsyncd ~]# cd /var/www/html/
[root@rsyncd html]# echo "this is test web" > index.html ##Create web page information
[root@rsyncd html]# cd ../
[root@rsyncd www]# chmod 777 html/ ##Give maximum permission for any user
2. On the client server, pull the synchronization source rsyncd
[root@client ~]# systemctl stop firewalld.service ##Turn off firewall
[root@client ~]# setenforce 0
[root@client ~]# rpm -q rsync ##Check if rsync service is installed
rsync-3.0.9-18.el7.x86_64
[root@client ~]# yum install httpd -y ##Install httpd service
[root@client ~]# cd /var/www/
[root@client www]# chmod 777 html/ ##Give maximum permission
[root@client www]# rsync -avz backuper@192.168.13.128::wwwroot /var/www/html/
##Pull sharing module
Password: ##Input password
[root@client www]# cat html/index.html ##View synchronization
this is test web
[root@client www]# rm -rf html/index.html
[root@client www]# vim /etc/server.pass ##Create a local password file
123123
[root@client www]# chmod 600 /etc/server.pass ##Grant authority
[root@client www]# rsync -avz --delete --password-file=/etc/server.pass backuper@192.168.13.128::wwwroot /var/www/html/
##Specify the local password file, delete the files in the target location but not in the original location, so as to realize interaction free
3. Install inotify monitoring on the client
[root@client www]# vim /etc/sysctl.conf ##Modify kernel parameter file
fs.inotify.max_queued_events = 16384 ##queue
fs.inotify.max_user_instances = 1024 ##Number of instances per queue
fs.inotify.max_user_watches = 1048576 ##Number of files per instance
[root@client www]# sysctl -p ##Load
[root@client www]# mount.cifs //192.168.100.3/LNMP-C7 /mnt / × mount
Password for root@//192.168.100.3/LNMP-C7:
[root@client www]# cd /mnt/
[root@client mnt]# tar zxvf inotify-tools-3.14.tar.gz -C /opt/ ##Unzip inotify to / opt
[root@client mnt]# cd /opt/
[root@client opt]# cd inotify-tools-3.14/
[root@client inotify-tools-3.14]# yum install gcc gcc-c++ make -y ##Components necessary for the installation environment
[root@client inotify-tools-3.14]# ./configure ##To configure
[root@client inotify-tools-3.14]# make && make install ##Compilation and installation
[root@client inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html/
##Monitoring
##Restart the terminal of a client
[root@client ~]# cd /var/www/html/
[root@client html]# touch abc
[root@client html]# rm -rf abc
##View on client on monitor
/var/www/html/ CREATE abc
/var/www/html/ DELETE abc
4. Create a script in the client and trigger the rsync synchronous operation script through inotifywait
[root@client inotify-tools-3.14]# cd /opt/
[root@client opt]# vim inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,move,delete /var/www/html/"
RSYNC_CMD="rsync -avz --delete --password-file=/etc/server.pass /var/www/html/ backuper@192.168.13.128::wwwroot/"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 0 ]; then
$RSYNC_CMD
fi
done
[root@client opt]# chmod +x inotify.sh ##Give execution permission
##Ensure that both the server and the client have the maximum permissions
5. Modify the configuration file on the rsyncd server
[root@rsyncd www]# vim /etc/rsyncd.conf
read only = no ##Close read only
[root@rsyncd www]# netstat -natp | grep rsync
tcp 0 0 192.168.13.128:873 0.0.0.0:* LISTEN 36346/rsync
[root@rsyncd www]# kill -9 36346 ##Close
[root@rsyncd www]# netstat -natp | grep rsync
[root@rsyncd www]# rm -rf /var/run/rsyncd.pid ##Delete pid file
[root@rsyncd www]# rsync --daemon ##Start rsync service
6. Execute inotify script file on the client
[root@client opt]# ./inotify.sh
##Restart a client terminal
[root@client html]# echo "this is test" > test.txt ##Add text
##View monitoring service information
[root@client opt]# ./inotify.sh
sending incremental file list
./
rsync: failed to set times on "/." (in wwwroot): Operation not permitted (1)
test.txt
sent 121 bytes received 30 bytes 302.00 bytes/sec
total size is 30 speedup is 0.20
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9]
sending incremental file list
sent 66 bytes received 8 bytes 148.00 bytes/sec
total size is 30 speedup is 0.41
7. View on the rsync server
[root@rsyncd www]# cd html/
[root@rsyncd html]# ls
index.html test.txt ##Synchronous completion
##Deletion is also synchronized
Thank you for reading!