37:rsync Tools and Common Options

Posted by TheBrandon on Sat, 18 May 2019 04:20:32 +0200

1. rsync: remove sync (remote synchronization), data backup tools, can achieve local synchronization and remote synchronization, synchronization can be incremental synchronization, only synchronize different data, that is, only synchronize two different parts of the file, so the speed is very fast:

Five command formats for rsync: Note: src represents the source file; dest represents the target file:

rsync options src dest local to local

rsync options src user@ip address: / directory from local to remote

rsync options user@ip address: / directory dest from remote host to local

rsync options src user@ip address: dest from local to remote host (two colons, different authentication methods)

rsync options user@ip address:

Note: @The user in front can be omitted, and the user in the target host can be authenticated by the current user, or written as an ordinary user: rsync-av, src, xiaoxiao@IP address: / directory

options:

- a: Represents the recursive transfer of files, including the - rlptgoD option: - A - no - P

- r: This option needs to be added when synchronizing directories, similar to the - R option of cp:

- v: verbose, transmission visualization:

- l: Retain the soft connection (because there is no source file synchronizing the soft connection, so it can not be used after retaining):

- L: When synchronizing soft connections, the source files referred to by soft connections are synchronized:

- p: Reserve the privileges of the source file when synchronizing:

- o: Keep the ownership information of the source g file when synchronizing (e.g. after www synchronization or www, if not, display UID):

- g: Keep the group information of the source file (GID) while synchronizing:

- D: Keep the device information of the file while synchronizing:

- t: Keep file time information while synchronizing:

delte: When synchronizing, delete files that STC (source directory) does not have in DEST (target directory):

- exclude: Filter the specified files, using wildcards (-- exclude "*. txt")

- P: Display the synchronization process (more detail, rate, transmission percentage, etc.):

- u: update, if the target file is newer than the source file, the source file is not synchronized (the old and new are judged by mtime):

- z: Compressed files for transmission:

Example 1: Synchronize local files or directories: - - rsync - av > test.txt / tmp/123.txt

[root@localhost_001 ~]# rsync -av test.txt /tmp/123.txt     #Synchronize test.txt to 123.txt in the tmp directory
sending incremental file list
test.txt

sent 89 bytes  received 35 bytes  248.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost_001 ~]# ls -ld /tmp/123.txt 
-rw-r--r-- 1 root root 0 8 Month 1412:32 /tmp/123.txt
[root@localhost_001 ~]# rsync -av test /tmp/test1         #Synchronize test directory to / tmp / directory test1
sending incremental file list
created directory /tmp/test1
test/
test/123
test/2112 -> 123
test/bac.txt -> /tmp/ipt.txt
sent 409 bytes  received 143 bytes  1,104.00 bytes/sec
total size is 15  speedup is 0.03
[root@localhost_001 ~]# ls -ld /tmp/test1/
drwxr-xr-x 3 root root 18 8 Month 1412:34 /tmp/test1/

Example 2: Synchronize local files to remote hosts: rsync-av, test.txt, 192.168.149.130:/tmp/123.txt

rsync    -av    test.txt    root@192.168.149.130:/tmp/123.txt

Synchronize the local test.txt file to the tmp directory of the remote host 192.168.149.130:
[root@localhost_001 ~]# rsync -av test.txt root@192.168.149.130:/tmp/1231.txt
root@192.168.149.130's password: 
sending incremental file list
test.txt

sent 89 bytes  received 35 bytes  27.56 bytes/sec
total size is 0  speedup is 0.00

View under remote host 130:
[root@localhost_002 ~]# ls -ld /tmp/1231.txt
- rw-r--r -- 1 root 10 August 14, 12:32/tmp/1231.txt

Note: When performing remote synchronization, both hosts need to install rsync: yum install - y Rsync

Example 3: Synchronize remote host files to local: rsync-av root@192.168.149.130:/tmp/1121.txt/tmp/test.txt

[root@localhost_001 ~]# rsync -av 192.168.149.130:/tmp/1231.txt /tmp/test.txt
root@192.168.149.130's password: 
receiving incremental file list
1231.txt
sent 43 bytes  received 89 bytes  52.80 bytes/sec
total size is 0  speedup is 0.00
[root@localhost_001 ~]# ls -ld /tmp/test.txt 
-rw-r--r-- 1 root root 0 8 Month 1412:32 /tmp/test.txt

rsync synchronization is based on SSH protocol. It needs to use its port. Sometimes, if the port changes, it needs to add-e "ssh-p 56588"

Synchronize the local file / tmp/test.txt to the tmp directory of the remote host 130 through its ssh port "52588"
[root@localhost_001 ~]# rsync -av -e "ssh -p 52588" /tmp/test.txt 192.168.149.130:/tmp/test1.txt
root@192.168.149.130's password: 
sending incremental file list
test.txt
sent 89 bytes  received 35 bytes  35.43 bytes/sec
total size is 0  speedup is 0.00

View its files on the remote host 130:
[root@localhost_002 ~]# ls -ld /tmp/test1.txt 
- rw-r--r -- 1 root 10 August 14, 12:32/tmp/test1.txt

Example 4: -- delte: Delete files that are not in the source file in the target directory:

[root@localhost_001 test]# ls /root/test                   #View its source file
123  2112  234  4356  567  bac.txt  dir1  dir2
[root@localhost_001 test]# ls /tmp/test/
123  2112  222  234  4356  567  bac.txt  dir1  dir2       #View its target file
[root@localhost_001 test]# rsync -av --delete /root/test/    /tmp/test/    #Synchronous operation
sending incremental file list
deleting 222
./

sent 225 bytes  received 28 bytes  506.00 bytes/sec
total size is 15  speedup is 0.06

Example 5: exclude filter file: specified file is not synchronized:

[root@localhost_001 ~]# ls test             #Looking at the file, we found that the file with. txt exists:
123  2112  234  4356  567  bac.txt  dir1  dir2
[root@localhost_001 ~]# rsync -av --exclude "*.txt" /root/test/ /tmp/test/    #Filtering does not contain *. txt
sending incremental file list
created directory /tmp/test
./
123
2112 -> 123
234
4356
567
dir1/
dir2/
sent 355 bytes  received 138 bytes  986.00 bytes/sec
total size is 3  speedup is 0.01
[root@localhost_001 ~]# ls /tmp/test/         #Looking at the file again, we found that there was no txt file:
123  2112  234  4356  567  dir1  dir2

//To support filtering multiple options, you need to add multiple -- exclude:
[root@localhost_001 ~]# rsync -av --exclude "*.txt" --exclude "1*" /root/test/ /tmp/test/

 

Example 4: Synchronized Soft Connection: You need to increase L to synchronize its source files, otherwise you can't use it:

On 001 machine rsync Synchronize to 002 machine:
[root@localhost_001 ~]# rsync -avL -e "ssh -p 52588" /root/test/  root@192.168.149.130:/tmp/test1/
root@192.168.149.130's password:
bac.txt -> /tmp/ipt.txt
dir1/
dir2/
sent 393 bytes  received 146 bytes  154.00 bytes/sec
total size is 15  speedup is 0.03

Example 5: Synchronization is not synchronized if the target file is newer than the source file: -u update

001 Host operation:
[root@localhost_001 ~]# rsync -av -e "ssh -p 52588" /root/test.txt 192.168.149.130:/root/test1.txt
root@192.168.149.130's password: 
sending incremental file list
test.txt
sent 101 bytes  received 35 bytes  54.40 bytes/sec
total size is 7  speedup is 0.05
002 Host:
[root@localhost_002 ~]# cat test1.txt 
dsjdfl;jsafkla;sjf;klsafjk;lsdfjkals;f

sdfjksdfjs'ad
fsaljflfasdf

Example 6: Display detailed processes, such as rates, percentages, etc.: -p

[root@localhost_001 ~]# rsync -avP -e "ssh -p 52588" /root/test/  root@192.168.149.130:/tmp/test1/
root@192.168.149.130's password: 
sending incremental file list
created directory /tmp/test1
./
123
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=7/9)
2112 -> 123
234
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=5/9)
4356
              0 100%    0.00kB/s    0:00:00 (xfr#3, to-chk=4/9)
567
              0 100%    0.00kB/s    0:00:00 (xfr#4, to-chk=3/9)
bac.txt -> /tmp/ipt.txt
dir1/
dir2/
sent 393 bytes  received 146 bytes  119.78 bytes/sec
total size is 15  speedup is 0.03

Topics: rsync ssh yum