First, the basic introduction of rsync
1. What is rsync
Rsync is an open source, fast, multi-functional, incremental local goods remote data mirroring synchronous backup of excellent tools, rsync use and unix,linux,windows and other platforms
2, the characteristics of rsync
1) Support for copying special files
2) can have exclude specified file or directory
3) can maintain the original file or directory permissions
4) can achieve incremental synchronization, that is, only synchronize the changed data
5) can use RCP,SSH and other ways to transfer files
6) support for anonymous or authenticated process mode transfer
7) compression before transfer, suitable for offsite backup
8) Use TCP 873 port
3. rsync Mode of work
1) Local Data transfer
Rsync [option] src DST
Case:
[Email protected] ~]# cd/opt/
[Email protected] opt]# mkdir test
[email protected] opt]# Touch test/11.txt
[Email protected] opt]# chmod-r test/
[[email protected] opt]# ls
RH Test
[Email protected] opt]# ls-l
Total 8
Drwxr-xr-x. 2 root root 4096 Mar-RH
DRWX------. 2 root root 4096 Sep 9 10:01 test
[Email protected] opt]# rsync-avz/opt//tmp/
( where/opt/, simply synchronizing the contents of the/opt/directory, the OPT directory itself is not synchronized, and/opt said that the opt Directory and content are all synced to/tmp )
Sending incremental file list
Created Directory/tmp
./
rh/
test/
Test/11.txt
Sent bytes received bytes 340.00 bytes/sec
Total size was 0 Speedup is 0.00
[Email protected] opt]# LS-L/tmp
Total 8
Drwxr-xr-x. 2 root root 4096 Mar-RH
DRWX------. 2 root root 4096 Sep 9 10:01 test
2) remote transfer (via SSH transmission)
Pull: All hosts timed to find a host pull data
rsync [option] [[Email protected][host....:src....dest
Push: One host is responsible for transmitting data to other hosts
rsync [Option]src....[[email protected]]host:dest
Instance:
Server: server1.cn ip:192.168.119.128
Client: server2.cn ip:192.168.119.130
(In a remote synchronization task, the client responsible for initiating the rsync synchronization operation is called the initiator and is responsible for responding
The server for the Rsync sync operation from the client is the backup source)
On the SERVER1 server
[Email protected] ~]# Rpm-qa rsync
Rsync-3.0.6-12.el6.x86_64
[Email protected] ~]# yum-y install xinetd
[Email protected] ~]# Vim/etc/xinetd.d/rsync
# Default:off
# description:the Rsync server is a good addition to an FTP server, as it \
# allows CRC Checksumming etc.
Service rsync
{
Disable = no
Flags = IPV6
Socket_type = Stream
wait = no
user = root
Server =/usr/bin/rsync
Server_args =--daemon
Log_on_failure + = USERID
}
[[Email protected] ~]# service xinetd start
Starting xinetd: [OK]
[Email protected] ~]# SS-TNL |grep 873
LISTEN 0::: 873:::*
Common options:
-A equivalent to-rlptgod (often used)
-R: Subdirectories are processed in recursive mode
-P: Keep the file's original permissions
- Z :--compress indicates compressed transport (often used)
- P : Display transfer speed (often used)
--delete Delete files that have the destination location but not the original location
--exclude= files that need to be filtered
- v Show details of the synchronization process
Combat: Backup Server1 on/usr/local/nginx/html to Server2 the/web-back , create user reg1
in Server1 Configure the following
[Email protected] ~]# Useradd Rget1
[Email protected] ~]# echo ' test123 ' |passwd--stdin rget1
Changing password for user Rget1.
Passwd:all authentication tokens updated successfully.
[Email protected] ~]# cd/usr/local/nginx/html/
[[email protected] html]# LD-/usr/local/nginx/html/
LD:-: No Such file:no such file or directory
[Email protected] html]# ls-d/usr/local/nginx/html/
/usr/local/nginx/html/
[Email protected] html]# ls-ld/usr/local/nginx/html/
Drwxr-xr-x. 2 root root 4096 Sep 9 10:59/usr/local/nginx/html/
[Email protected] html]# Setfacl-r-M user:rget1:rwx/usr/local/nginx/html/
[Email protected] html]# Setfacl-r-M default:rget1:rwx/usr/local/nginx/html/
[Email protected] html]# getfacl/usr/local/nginx/html/
Getfacl:removing leading '/' from absolute path names
# file:usr/local/nginx/html/
# Owner:root
# Group:root
User::rwx
User:rget1:rwx
Group::r-x
Mask::rwx
Other::r-x
Default:user::rwx
Default:user:rget1:rwx
Default:group::r-x
Default:mask::rwx
Default:other::r-x
in Server2 Configure the following
[Email protected] ~]# Mkdir/web-backup
[ email protected] ~]# RSYNC-AVZP--delete [email protected]:/usr/local/nginx/html//web-backup/ (Pull)
[email protected] ' s password:
Receiving incremental file list
./
50x.html
Index.html
index.php
Sent bytes received 923 bytes 284.00 bytes/sec
Total size was 1169 speedup is 1.18
[Email protected] ~]# ls-l/web-backup/
Total 12
-RW-RWXR--. 1 root root 537 Sep 8 10:37 50x.html
-RW-RWXR--. 1 root root 612 Sep 8 10:37 index.html
-RW-RWXR--. 1 root root 8 Sep 11:59 index.php
Combat 2 : Using SSH The key implements no interactive backup, and the Server1 regular backup of data on to Server2 on
in Server2 on the configuration:
[[email protected] ~]# ssh-keygen-t RSA
Generating public/private RSA key pair.
Enter file in which to save the key (/ROOT/.SSH/ID_RSA):
Enter passphrase (empty for no passphrase):
Enter same Passphrase again:
Your identification has been saved In/root/.ssh/id_rsa.
Your public key has been saved in/root/.ssh/id_rsa.pub.
The key fingerprint is:
9e:c0:ed:c3:cc:db:d5:49:2a:58:7f:7a:f6:3d:f2:87 [email protected]
The key ' s Randomart image is:
+--[RSA 2048]----+
| |
| |
| |
| . . |
| o S. . |
| B +. + . |
| X. + +. |
| + O +e.o|
| ... ooo=|
+-----------------+
[Email protected] ~]# Ssh-copy-id [email protected]
[email protected] ' s password:
Now try logging to the machine, with "ssh" [email protected] ' ", and check in:
. Ssh/authorized_keys
To make sure we haven ' t added extra keys so you weren ' t expecting.
Test: SSH [email protected]
[Email protected] ~]# RSYNC-AZP--delete [email protected]:/usr/local/nginx/html//web-backup/
[Email protected] ~]# ll-l/web-backup/
Total 12
-RW-RWXR--. 1 root root 537 Sep 8 10:37 50x.html
-RW-RWXR--. 1 root root 612 Sep 8 10:37 index.html
-RW-RWXR--. 1 root root 8 Sep 11:59 index.php
To write a script:
[Email protected] ~]# vim rsync.sh
src=/usr/local/nginx/html/
Dst=/web-backup
Rsync-az--delete [email protected]: $SRC $Dst
[Email protected]/]# CRONTAB-E
No crontab for root-using an empty one
3 * * */root/rsync.sh
3) transfer in daemon mode
Combat 3 : Configuring Rsync servers and directories that need to be backed up without the use of system users
in Server1 -side Creation/etc/rsyncd.conf
[Email protected] ~]# vim/etc/rsyncd.conf
Uid=nobody
Gid=nobody
address=192.168.119.128
port=873
Host allow= 192.168.119.130 192.168.119.131
Use chroot=yes ( Lock home directory)
Max Connections=5 (maximum number of connections)
PID File=/var/run/rsyncd.pid
Lock File=/var/run/rsync.lock
Log File=/var/log/rsyncd.log
MOTD FILE=/ETC/RSYNCD.MOTD
[Wwwroot]
path=/usr/local/nginx/html/
Comment=rsync Wwwroot of Www.test.com
Read Only=yes (provide backup in read-only mode)
List=yes (allows viewing of module information)
Auth Users=backuper (the user who specified the backup, regardless of the system user)
Secrets file =/etcrsync.passwd (Specify the password file to hold the user)
[[email protected] ~]# echo "Welcome to Backup Server" >/ETC/RSYNCD.MOTD
[Email protected] ~]# VIM/ETC/RSYNC.PASSWD
Backuper : pwd123
[Email protected] ~]# chmod 600/etc/rsync.passwd
[[email protected] ~] #vim/etc/xinetd.d/rsync (Put Disabled=yes change into Disabled=no )
[Email protected] ~]# service xinetd restart
stopping xinetd: [OK]
Starting xinetd: [OK]
in Server2 on the test:
[Email protected] ~]# RSYNC-AZP [email protected]::wwwroot/web-backup/
rsync:failed to connect to 192.168.119.128:no route to host (113)
Rsync error:error in Socket IO (code ten) at CLIENTSERVER.C (124) [receiver=3.0.6]
(indicates that the other side has a firewall blocked, need to release 873 Port)
[Email protected] ~]# RSYNC-AZP [email protected]::wwwroot/web-backup/
Welcome to Backup Server
Password:
Receiving incremental file list
./
html/
Html/50x.html
537 100% 524.41kb/s 0:00:00 (xfer#1, TO-CHECK=5/10)
Html/index.html
612 100% 39.84kb/s 0:00:00 (xfer#2, TO-CHECK=4/10)
html/index.php
100% 1.22kb/s 0:00:00 (xfer#3, TO-CHECK=3/10)
logs/
Logs/access.log
1977 100% 113.57kb/s 0:00:00 (xfer#4, TO-CHECK=2/10)
Logs/error.log
1495 100% 81.11kb/s 0:00:00 (xfer#5, TO-CHECK=1/10)
sbin/
Sbin/nginx
6382486 100% 10.70mb/s 0:00:00 (xfer#6, TO-CHECK=0/10)
Sent bytes received 2747564 bytes 50417.50 bytes/sec
Total size was 6387127 speedup is 2.32
[Email protected] ~]# ls-l/web-backup/
Total 24
-RW-RWXR--. 1 root root 537 Sep 8 10:37 50x.html
Drwxrwxr-x. 2 root root 4096 Sep 9 10:59 HTML
-RW-RWXR--. 1 root root 612 Sep 8 10:37 index.html
-RW-RWXR--. 1 root root 8 Sep 11:59 index.php
Drwxr-xr-x. 2 root root 4096 Sep 8 10:39 logs
Drwxr-xr-x. 2 root root 4096 Sep 8 10:37 sbin
Without entering a password, directly backing up the data can be scripted
[Email protected] ~]# export rsync_password=pwd123456
[Email protected] ~]# RSYNC-AZP [email protected]::wwwroot/web-backup/
Second, configure the rsync+inotify to achieve real-time synchronization
Rsync+inotify for remote Data backup