Linux rsync environment to build (build non-ROOT)
Preparing the Environment
-
rsync
downloadhttps://rsync.samba.org/ftp/rsync/src/rsync-3.1.3.tar.gz
-
inotify
downloadhttps://github.com/rvoicilas/inotify-tools/archive/3.20.1.tar.gz
-
Environmental Information
Here is the structure of the two servers, respectively, hostname, ip, status, both servers are centos 6.9
web server: 29.2.70.75
Backup server: 29.2.70.76
Preparing to Install
Primary server
The server (i.e., that end of synchronization to the other machines), needs to be installed rsync
and inotify
, web server as the server, transfer files to the backup server.
-
Install rsync
1. tar xvzf rsync-3.1.3.tar.gz 2. cd rsync-3.1.3 3. ./configure --prefix=/home/tomcat/opt/rsync 4. make && make install
-
Create a password file authentication
1. cd /home/tomcat/opt/rsync 2. echo "rsync-pwd" > rsync.passwd
Which rsync-pwd can set their own password, rsync.passwd name can also set up their own.
Modify the permissions 600
chmod 600 rsync.passwd
.
-
-
Install inotify
1. 安装autoconf,automake,libtool等工具,过程略 2. tar xvzf inotify-tools-3.20.1.tar.gz 3. cd inotify-tools-3.20.1 4. ./autogen.sh 5. ./configure --prefix=/home/tomcat/opt/inotify 6. make && make install
-
Creating rsync replication script
This function is mainly to the server side of the directory / home / tomcat / tmp in the content, if you modify (either to add, modify, delete files) can be monitored via inotify, and through rsync real-time synchronization to the client's / home / tomcat / tmp, the following is achieved through the shell script, the script is as follows:
#!/bin/bash host=29.2.70.76 src=/home/tomcat/tmp/ des=web user=tomcat /home/tomcat/opt/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src | while read files do /usr/bin/rsync --port=30001 -vzrtopg --delete --progress --password-file=/home/tomcat/opt/rsync/rsync.passwd $src $user@$host::$des echo "${files} was rsynced" >>/home/tomcat/rsync.log 2>&1 done ##如果不需要保留属性,或者保留属性需要其他权限可以去掉-vzrtopg中的pg选项
This script named rsync.sh , on his specified directory, for example, I will put / home / tomcat under, and modify the permissions to 764, the specific operation is:
chmod 764 rsync.sh
and then run the scriptsh /home/tomcat/rsync.sh &
.After the note, keep in mind, only install and start the rsync rsync backup server, start rsync.sh script, otherwise it sometimes appears full screen. Otherwise, an error similar to the following:
rsync: failed to connect to 29.2.70.76: Connection refused (111) rsync error: error in socket IO (code 10) at clientserver.c(107) [sender=2.6.8]
We can also add this script to the boot entry (if you have permission):
echo "/tmp/rsync.sh" >> /etc/rc.local
Backup Server
-
Install rsync (backup server only need to install rsync)
Ditto
-
To create a user and password authentication file
echo "tomcat:rsync-pwd" > /home/tomcat/opt/rsync/rsync.passwd
Remember that the password file in a web end establishment, only the password, no user name; and the establishment of the backup server in the password file, the user name and password are,
chmod 600 rsync.passwd
, 600 permissions to copy the password file. -
Establish rsync configuration file, save it to / home / tomcat / opt / rsync / bin / directory
use chroot = no max connections = 10 strict modes = yes pid file = /home/tomcat/run/rsyncd.pid lock file = /home/tomcat/run/rsync.lock log file = /home/tomcat/logs/rsyncd.log port = 30001 #需要和主服务保持一致 [web] path = /home/tomcat/tmp/ # 存放文件的目录 comment = web file ignore errors read only = no write only = no hosts allow = 29.2.70.75 hosts deny = * list = false #auth users = webuser secrets file = /home/tomcat/opt/rsync/rsync.passwd
Wherein the web server is the server where the authentication module name, and the primary server in need of the same, more than my own server configuration in the configuration, for reference.
-
Start rsync
/home/tomcat/opt/rsync/bin/rsync --daemon --config=/home/tomcat/opt/rsync/bin/rsync.conf
Normal master file server has changed, we can all be synchronized over.
Precautions
- If you do not modify the port, the default port need to start with the root user, because the default port is 873.