rsync ssh data synchronization analysis

  • 2020-05-06 12:06:43
  • OfStack

1.rsync and ssh different remote synchronization command

 
 Command format:  
#rsync [option]  The source path   The target path  
 Among them:  
[option] :  
a: use archive Pattern is equal to -rlptgoD , that is, maintain the original file permissions  
z: Represents compression of data during transmission  
v: Displays to the screen  
e: Using a remote shell Program (can be used rsh or ssh )  
--delete: The exact copy is saved, the files deleted by the source host are also deleted by the target host  
--include=PATTERN: Nonexclusive conformity PATTERN A file or directory  
--exclude=PATTERN: Exclude all conformity PATTERN A file or directory  
--password-file: Designated for rsync The user authentication password for the server  

 The source and destination paths can be in the following format:  
rsync://[USER@]Host[:Port]/Path <--rsync Server path  
[USER@]Host::Path <--rsync Another representation of a server  
[USER@]Host:Path <-- The remote path  
LocalPath <-- The local path  

※ it is important to note that at least one of the source or destination paths must be a local path. If the local path is ignored, only the remote files will be listed.

Example:

mkdir test_new; // locally create a test directory

ssh ljz@192.168.1.109: / home ljz test / / / this is svn
accounts in the directory server
Update the contents of the test directory on svn to
in the local test_new directory
rsync - ave ssh ljz@192.168.1.109: / home/ljz test / / home/ljz/test_new/

※ it is important to note that if /home/ljz/test/ means all the contents of the test directory, and /home/ljz/test means all the contents of the
directory
Synchronize local content to a remote directory?

rsync -ave ssh /home/ljz/test_new/ ljz@192.168.1.109:/home/ljz/test/
1#rsync -avz --delete ljz@192.168.1.109:/home/ljz/test/ /home/ljz/New_Test/
Synchronize the contents of the test directory on 109 to the local New_Test directory and delete files or directories that do not exist in the local upper source path.
※ be sure to pay attention to the delete parameter. When using this parameter, it is recommended to specify the local directory with an absolute path to prevent the current directory from being emptied.

ii. Set up rsync server

When the server does not open ssh, or the backup party does not have ssh permission, we can set up rsync server, the anonymous data synchronization.
To use the rsync service, you need to set up the server and client:
1. Server
Master profile:
 
#vi /etc/rsyncd.conf 
log file = /var/log/rsyncd.log 
pid file = /var/run/rsyncd.pid 
lock file = /var/run/rsync.lock 
[test] #rsync The setting name of the section  
path = /var/www/html/test # The data storage path that needs to be synchronized  
comment = test folder # annotation  
uid = apache # What identity is used for file reading  
gid = apache # Again, there must be reads path Authorized users, groups  
ignore errors # Ignore the error  
read only = yes # read-only  
list = no # Can't list  
auth users = linuxing # The connection rsync Account number of the service  
secrets file = /etc/rsyncd.secrets # Specifies the location to store the password for the account  

Account password file:

#vi /etc/rsyncd.secrets
Account number: password (one group per line, account number and password separated by: number)
linuxing:backup

After saving, you need to ensure that the user is root and the permission is 600

#chown root:root /etc/rsyncd.secrets
#chmod 600 /etc/rsyncd.secrets

Startup service:
#rsync --daemon

Ensure automatic startup:
Modify/etc/xinetd d/rsync file, change the disable = yes to disable = no

#sed -i -e "/disable/{ s/yes/no/ }" /etc/xinetd.d/rsync
#service xinetd.d restart

2. Client
Usage 1 refers to the rsync:// method.
If the rsync server requires password authentication, you can add -- password-file parameter:

#rsync -azv --delete rsync://linuxing@192.168.1.100/test /var/www/html --password-file=/etc/test
#vi /etc/test
Specify the access password
bakcup
#chmod 600 /etc/test

3. Timing
In addition, since the rsync client has no timing function, we can achieve timing synchronization by adding scheduled tasks in crontab, such as :(do not use the -v parameter to prevent refresh)

#crontab -e
0 22 * * 1-5 /usr/bin/rsync -az --delete rsync://linuxing@192.168.1.100/test /var/www/html --password-file=/etc/test
Specify Monday to Friday, and synchronize at 10 o 'clock every night

Advantages of using rsync: network encryption is available over ssh, and trust can be established using ssh client keys. When synchronizing large, complex directory structures between two computers, it is faster than tar or wget. And you can synchronize it exactly.

iv. Appendix
If
is compiled to install rsync (such as FreeBSD), the path is a little different:
The server's configuration file: / usr local/etc/rsyncd conf
Startup file: / usr/local/etc/rc d/rsyncd sh
Command file: / / usr local bin/rsync
System startup hosting: /etc/ rc.conf (add rsyncd_enable="YES")

Related articles: