Server: 192.168.1.2 Sync directory: /home/source
Client: 192.168.1.3 Sync Directory: /home/receive
1. Server configuration
shell>vi /etc/rsyncd.conf #Create configuration file rsyncd.conf
Configuration file content:
gid=nobody //The group running the rsync daemon, specify 0 as the root group
use chroot = no //Do not use chroot
max connections = 10 //Maximum Number of connections, 0 is
unlimited pid file = /var/run/rsyncde.pid //pid file storage location
lock file = /var/run/rsync.lock //lock file storage location
log file = /var/log/rsyncd .log //The storage location of the log record file
//This option can override the IP timeout time specified by the client
//This option can ensure that the rsync server will not wait forever for a crashed client
//The timeout unit is seconds, 0 Indicates that there is no timeout defined, which is also the default value.
//For anonymous rsync servers, an ideal number is 600.
Timeout = 300
Log format = %t %a %m %f %b
[backup] //This is authentication Module name, you need to specify
path=/home/source/ on the client side //The directory that needs to be mirrored
ignore errors //Some irrelevant IO errors can be ignored
read only = yes //This option sets whether to allow customers to upload files
list = no //Do not allow listing files
auth users = rsync //Authenticated username
secrets file = /etc/rsyncd.secrets //Secret file (defined below)
hosts allow = 192.168.1.3 //Allow hosts or network segment
hosts deny = 0.0.0.0/0 //Disable host
shell>vi etc/rsyncd.secrets #Create a password file
content:
shell>chmod 0600 /etc/rsyncd.secrets #Change password file permissions (important here)
shell>rsync --daemon; #Start the service, listen on port 873 by default (you can modify it yourself)
2. Client
The client does not need to start the rsync service
shell>vi /etc/rsyncd.secrets #This is also to create a password file, the file name is customized, but the content only needs to fill in the target service authorization password
shell>chmod 0600 /etc/rsyncd.secrets #Change file permissions
content:
Test: (create the test file in the server /home/source/ first)
Need to ntpupdate the two servers have the same time
shell>rsync -vzrtopg --delete --progress --password-file=/etc/rsyncd.secrets [email protected]::backup /home/receive
v:传输时的进度等信息, z:表示压缩, r:是递归, t:保持文件原有时间, o:保持文件原有属主, P:传输进度, g:保持文件原有用户组
--progress 指显示
--delete 指如果服务器端删除了这一文件,那么客户端也相应把文件删除,保持真正的一致
--password-file=/etc/rsyncd.secrets 认证密码
rsync 认证用户
backup 认证模块
如果客户端/home/receive/下产生了test文件代表同步成功
下一步写一个脚本文件实现真正的同步......
启动脚本:rsync.sh
shell>vi /home/rsync.sh
#!/bin/sh
rsync -vzrtopg --delete --progress --password-file=/etc/rsyncd.secrets [email protected]::backup /home/receive
|
关闭脚本:killrsync.sh
shell>vi /home/killrsync.sh
RSYNC_PID=`ps auxww|grep rsync |grep -v grep|awk '{print $2}'`
kill -9 $RSYNC_PID
设置定时任务 (crontab?google can help you!)
shell>crontab -e
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
0 3 * * * /home/rsync.sh //每天晚上3点执行一次同步
0 6 * * * /home/killrsync.sh //每天早上6点强制终止同步(如果还没完成)
shell>crontab -l #可以查看任务设置情况
shell>service crond status #查看crond是否已启动,若启动了能看到PID
shell>service crond start #启动crond服务