One, rsync remote synchronization server
Brief introduction:
rsync (remote synchronize) is a data synchronization tool that allows you to quickly sync files between multiple hosts via Lan/wan, or use rsync to synchronize different directories on your local hard drive.
Rsync is a tool for replacing RCP, which uses the so-called "rsync algorithm" to synchronize files between local and remote two hosts, which transmits only the different parts of two files, rather than the entire transfer every time, so it is quite fast.
Rsync supports most Unix-like systems, which are well tested on Linux, Solaris, and BSD. In addition, it also has the corresponding version under the Windows platform, the more well-known have cwrsync and Sync2nas
Characteristics:
1) Ability to update entire directory tree and file system
2) Selectively maintain symbolic connections, hard links, file attributes, permissions, devices, and time;
3) No special permission required for installation
4) for multiple files, the internal pipeline reduces the delay of file waiting.
5) can be used as transmission port with rsh, SSH or direct port
6) Support for anonymous rsync synchronization files, is the ideal image tool
Use of the rsync command:
Basic format: rsync [options] Original position target location
Common options:
-A: Archive mode, recursive reserved object properties, equivalent to-rlptgod
-V: Show details of the synchronization process (ver)
-Z: Compress when transferring files
-H: Keep hard-link files
-A: Preserve ACL attribute information
--delete: Delete a file with the destination location but not the original location
-R: Recursive mode, containing all files in directories and subdirectories
-L: The symbol connection file is still copied as a symbol connection file
-P: Retain permission flags for files
-T: Time stamp to keep files
-G: Reserved group tags for files (super users only)
-O: Preserve the main tag of the file (Superuser only)
-D: Keep device files and other special files
Server-side: synchronization source
Client: Initiator
How to build a sync Source: SSH or rsync
Access Control List:
Setfacl: Setting Special Permissions
-M Add permissions
-X Deletes a special permission
-B Remove All special permissions
-R recursion
Default setting defaults (also in effect for future created files)
Second, using SSH to build a synchronization source
1. Server side:
1) Create a sync directory, such as: Mkdir/var/ssh
2) Create a test file such as: Touch/var/ssh/ssh.txt
3) Set permissions, such as:
Useradd A1 #用来上传
Useradd A2 #用来下载
Chown A1:a1/var/ssh
2. Client:
1) Create a sync directory, such as: Mkdir/ssh
2) Synchronize server data, such as: rsync-avz [email protected]:/var/ssh/*/ssh
Third, using rsync to build a synchronization source
1. Server side:
1) Create a sync directory, such as: Mkdir/var/rsync
2) Create a test file such as: Touch/var/rsync/rsync.txt
3) Create a master configuration file, such as:
Vim/etc/rsyncd.conf
Use chroot = yes
Address = Server IP address #监听地址
Port 873 #监听端口
Log file =/var/log/rsyncd.log #日志文件
PID file =/var/run/rsyncd.pid #pid文件
[Share] #共享目录名
Comment = rsync share directory #描述信息
Path =/var/rsync #真实目录
Read Only = yes #只读
Dont compress = *.gz *.bz2 #这两种格式的文件传输过程中不压缩
Auth users = AA #允许登录用户
Secrets file =/etc/rsyncd_users.db #用户密码文件
4) Create the account file:
Vim/etc/rsyncd_users.db
aa:123456
5) Open service: Rsync--daemon
6) Shutdown Service: Kill $ (cat "/var/run/rsyncd.pid")
2, the Client
1) Create a sync directory, such as: Mkdir/rsync
2) Synchronize server data, such as:
Rsync-avz [Email Protected]::share/rsync
Rsync-avz Rsync://[email Protected]/share/rsync
Iv. Synchronization Scripts
1. SSH-based remote synchronization server:
Client: ssh-keygen-t RSA #生成公钥和私钥文件
Ssh-copy-id [email protected] #将公钥文件上传到服务器端
SSH [email protected] #测试密钥对是否成功
Rsync-avz [email protected]:/var/ssh/*/ssh #测试同步是否成功
2. Rsync-based remote synchronization server:
Client: Export rsync_password=123456 #声明全局变量, save the password of the connected user
Rsync-avz [Email Protected]::share/rsync #测试同步是否成功
Five, automatic backup
INotify
Two components: inotifywait: Continuous monitoring, one change, immediate output results
Inotifywatch: Collect file system changes and output summary changes after running the results
To modify kernel parameters:
Max_queued_events Monitoring Queue
Max_user_instances maximum number of monitoring instances
max_user_watches maximum number of monitored files per instance
1, one-way synchronization
1) rsync sync Source: Refers to the remote server for the backup operation, also known as the backup source. Mainly includes two kinds: rsync source, SSH source
2) Editing the kernel
vim/etc/sysctl.conf Add the following content
Fs.inotify.max_queued_events = 16384
Fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches-1048576
Sysctl-p #检查sysctl. conf configuration file is correct
3) Inotifywait-mrq-e modify,create,move,delete/var/www/html
-M continuous monitoring
-R monitors all child objects
-Q Simplified output
-e Specifies the type of event to monitor
4) scripting, automatically synchronizing when monitoring changes to data
Vim rsync.sh
#!/bin/bash
inotify_cmd= "Inotifywait-mrq-e modify,create,attrib,move,delete/var/www/html"
rsync_cmd= "Rsync-avz--delete/var/www/html [email protected]:/var/www/html]
Do
If [$ (pgrep rsync | wc-l)-le 0]
Then
$RSYNC _cmd
Fi
Done
2. Bidirectional Synchronization
1) To ensure that the two servers can be accessed via SSH without password, the operation is as follows (here, for example, the root user):
Create keys under Server1 (192.168.1.1) and Server2 (192.168.1.2), respectively
Server1:
SSH-KEYGEN-T RSA
Ssh-copy-id 192.168.1.2
Server2:
SSH-KEYGEN-T RSA
Ssh-copy-id 192.168.1.1
Perform the following tests on two machines, respectively:
SSH 192.168.1.1
SSH 192.168.1.2
2) Install Unison
Install OCaml first, with a version of at least 3.07 or higher
: http://caml.inria.fr/pub/distrib/ocaml-3.10/
TAR-ZXF ocaml-3.10.2.tar.gz
CD ocaml-3.10.2
./configure
Make World opt
Make install
Installing unison
: http://www.seas.upenn.edu/~bcpierce/unison//download/releases/unison-2.13.16/
TAR-ZXF unison-2.13.16.tar.gz
CD unison-2.13.16
Make Uistyle=text threads=true static=true
CP Unison/usr/local/bin
Installing INotify
: http://inotify-tools.sourceforge.net
Tar xvf inotify-tools-3.14.tar.gz
CD inotify-tools-3.14
./configure
Make
Make install
ln-sv/usr/local/lib/libinotify*/usr/lib/
Unison-batch/home/server1/ssh:192.168.10.2//home/server2
#测试两台服务器之间是否可以同步文件
3) Writing automatic synchronization scripts
Create script on Server1 unison.sh
vim/root/unison.sh
#!/bin/bash
Ip2= "192.168.1.2"
Src2= "/server1/"
Drt2= "/server2/"
/usr/local/bin/inotifywait-mrq-e Create,delete,modify,move $SRC 2 | While Read line
Do
/usr/local/bin/unison-batch $src 2 ssh://$ip 2/$DRT 2
Echo-n "$line" >>/var/log/inotify.log
echo $ (date | cut-d ""-f1-4) >>/var/log/inotify.log
Done
chmod a+x/root/unison.sh
Create script on Server2 unison.sh
vim/root/unison.sh
#!/bin/bash
ip1= "192.168.1.1"
src1= "/server2/"
drt1= "/server1/"
/usr/local/bin/inotifywait-mrq-e Create,delete,modify,move $SRC 1 | While Read line
Do
/usr/local/bin/unison-batch $src 1 ssh://$ip 1/$DRT 1
Echo-n "$line" >>/var/log/inotify.log
echo $ (date | cut-d ""-f1-4) >>/var/log/inotify.log
Done
chmod a+x/root/unison.sh
This article is from the Linux OPS blog, so be sure to keep this source http://jinghsy.blog.51cto.com/7648537/1661845
Management and configuration of Rsync server in Linux system