Article Title: Use the Linux rsync command to achieve: multi-server image synchronization. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Function: When you create, modify, or delete a directory or file in a directory on the local client, you only need to execute the same shell script to modify the directory or file attributes, rsync will automatically find the modified or deleted data and send it to the server, so that the data in a directory on the Linux server is consistent with the data in a directory on the local client.
★Method 1:
I. server side (for example, 10.0.0.1 *):
1. Create users and groups
Groupadd www-g 48
Useradd-u 48-g www
Mkdir-p/opt/htdocs
Chmod + w/opt/htdocs
Chown www: www/opt/htdocs
2. Edit the rsync configuration file
Vi/etc/rsyncd. conf
Enter the following content:
Uid = www
Gid = www
Max connections = 10
Use chroot = no
Log file =/var/log/rsyncd. log
Pid file =/var/run/rsyncd. pid
Lock file =/var/run/rsyncd. lock
[Zhangyan]
Path =/opt/htdocs
Comment = my htdocs
Ignore errors
Read only = no
Hosts allow = 10.0.0.21 10.0.0.22
3. Start the rsync server
/Usr/bin/rsync -- daemon
Ii. Client (for example, 10.0.0.21 and 10.0.0.22 ):
1. Create a shell script push. sh.
Vi push. sh
Enter the following content (10.0.0.1 * is the target server to be pushed, and zhangyan is the module name in the rsyncd. conf configuration file on the server ):
Reference
#! /Bin/sh
/Usr/bin/rsync-vzrtopg -- delete $1 10.0.0.10: zhangyan/
/Usr/bin/rsync-vzrtopg -- delete $1 10.0.0.11: zhangyan/
/Usr/bin/rsync-vzrtopg -- delete $1 10.0.0.16: zhangyan/
/Usr/bin/rsync-vzrtopg -- delete $1 10.0.0.19: zhangyan/
Note: The password is not set here because it is transmitted between the internal LAN.
2. Grant the push. sh execution permission.
Chmod + x./push. sh
3. Synchronize the files in a directory (for example,/home/zhanguan/abc) of the push client to the/opt/htdocs directory on the server:
./Push. sh/home/zhanguan/abc/
Note: Do not forget the/behind abc /. If the file to be changed is too large, you can add a space and A & sign at the end of the preceding statement to push it to the background for execution. Example:
./Push. sh/home/zhanguan/abc /&
★Method 2:
I. server side (for example, 10.0.0.1 *):
1. Create users and groups
Groupadd www-g 48
Useradd-u 48-g www
Mkdir-p/opt/htdocs
Chmod + w/opt/htdocs
Chown www: www/opt/htdocs
Passwd www
Set a password for the www user.
Ii. Client:
1. Synchronize the files in a directory of the client (for example,/home/zhanguan/abc) to the/opt/htdocs directory on the server:
/Usr/bin/rsync-vzrtopg -- delete/home/zhanguan/abc/-e ssh www@10.0.0.10:/opt/htdocs/
Enter the password and press Enter. You do not need to start the rsync service on the server.