Linux file Sync Tool-rsync
Install package
yum install -y rsync
Rsync Common Options
-A: Archive mode, which means to transfer files recursively and maintain all properties;-rlptgod;
-r: Synchronize the directory to add, similar to CP when adding r;
-V: Display some information when synchronizing, let us know synchronous country creates;
-L: Keep soft links;
-L: Synchronization of soft links will be the source files to synchronize;
-P: Preserve file permission attributes;
-O: Keep the owner of the file;
-G: Keep the genus Group of files;
-D: Maintain device file information;
-T: Keep the file's time attribute;
--delte: Delete files in dest that are not in SRC;
--exclude: Filter the specified file, such as--exclude "Logs" will filter out the file log, out of sync;
-P: Shows the synchronization process, such as rate, more detailed than-V;
-U: If the file in dest is newer than SRC, it is out of sync;
-Z: compression when testing;
Backup
Rsync-av/etc/passwd/tmp/1.txt
Back up the/etc/passwd to the Tmp/1.txt file
[[email protected] ~]# rsync -av /etc/passwd /tmp/1.txtsending incremental file listpasswdsent 1229 bytes received 31 bytes 2520.00 bytes/sectotal size is 1155 speedup is 0.92[[email protected] ~]# ls /tmp/1.txt
Back up to a remote computer
rsync-av/etc/passwd [Email Protected]:/tmp/2.txt
Synchronizing the/etc/passwd files to the/tmp/2.txt of the 192.168.188.2 machine
[[email protected] ~]# rsync -av /etc/passwd [email protected]:/tmp/2.txtThe authenticity of host ‘192.168.188.2 (192.168.188.2)‘ can‘t be established.ECDSA key fingerprint is SHA256:vl9780juEAW/PspOqj/J4E4yHiQtIG6ZQ2R9S1OAdb8.ECDSA key fingerprint is MD5:f8:71:01:1c:c1:71:88:97:d8:ca:25:88:79:45:17:1b.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added ‘192.168.188.2‘ (ECDSA) to the list of known hosts.[email protected]‘s password:sending incremental file listpasswdsent 1229 bytes received 31 bytes 168.00 bytes/sectotal size is 1155 speedup is 0.92[[email protected] ~]# ls /tmp/1.txt2.txt[[email protected] ~]#
Backup to a remote computer (in case of SSH modified port)
Rsync-avp-e "Ssh-p"/root/abc/[email protected]:/tmp/test2/
[[email protected] tmp]# rsync -avP -e "ssh -p 22" /root/abc/ [email protected]:/tmp/test2/[email protected]‘s password:sending incremental file listsent 365 bytes received 19 bytes 109.71 bytes/sectotal size is 255434 speedup is 665.19[[email protected] tmp]#
Note: To backup to a remote server, both servers must have the Rsync package installed before they can be backed up;
Synchronizing folders
rsync-av/root/abc//tmp/111_dest/
[[email protected] ~]# rsync -av /root/abc/ /tmp/111_dest/sending incremental file listcreated directory /tmp/111_dest./1.txt2.txt3.txtip.txta/a/1.txta/2.txta/b/a/b/2.txta/b/c/a/k/a/k11/a/s/test/test/1.txttest/2.txttest/a.txtsent 256259 bytes received 233 bytes 512984.00 bytes/sectotal size is 255434 speedup is 1.00[[email protected] ~]# cd /tmp/[[email protected] tmp]# ls111_dest systemd-private-603500798f13486a90f57d6e2dd53912-chronyd.service-UCHrHd1.txt systemd-private-603500798f13486a90f57d6e2dd53912-vgauthd.service-UNh9hx2.txt systemd-private-603500798f13486a90f57d6e2dd53912-vmtoolsd.service-JJIguS[[email protected] tmp]#
Delete files from the backup directory that are not part of the source
Rsync-av--delete/root/abc//tmp/111_dest/
Back up the files in the/root/abc/folder to the/tmp/111_dest/directory,
and delete files in/tmp/111_dest/that do not belong to/root/abc/;
[[email protected] 111_dest]# touch new.txt[[email protected] 111_dest]# ls1.txt 2.txt 3.txt a ip.txt new.txt test[[email protected] 111_dest]# rsync -av --delete /root/abc/ /tmp/111_dest/sending incremental file list./deleting new.txtsent 354 bytes received 22 bytes 752.00 bytes/sectotal size is 255434 speedup is 679.35[[email protected] 111_dest]# ls1.txt 2.txt 3.txt a ip.txt test[[email protected] 111_dest]#
Filter specified file is not synchronized
Rsync-av--exclude ". 123"/root/abc//tmp/111_dest/
back up/root/abc/to/tmp/111_dest/, but do not back up the. 123 files in the past;
[[email protected] abc]# ls1.txt 2.txt 3.txt a a.123 ip.txt test[[email protected] abc]# cat a.123[[email protected] abc]# rsync -av --exclude "*.123" /root/abc/ /tmp/111_dest/sending incremental file list./sent 354 bytes received 22 bytes 752.00 bytes/sectotal size is 255434 speedup is 679.35[[email protected] abc]# cd /tmp/111_dest/[[email protected] 111_dest]# ls1.txt 2.txt 3.txt a ip.txt test[[email protected] 111_dest]#[[email protected] 111_dest]# ls /root/abc/1.txt 2.txt 3.txt a a.123 ip.txt test[[email protected] 111_dest]#
Protect new files in a backed up file
rsync-avu/root/abc//tmp/111_dest/
If there is a new file in the/tmp/111_dest/than the source file, then protect the new file from being killed synchronously;
And synchronize the/root/abc/to the/tmp/111_dest/;
[[email protected] abc]# cd /tmp/111_dest/[[email protected] 111_dest]# touch new.new[[email protected] 111_dest]# rsync -avu /root/abc/ /tmp/111_dest/sending incremental file list./sent 368 bytes received 22 bytes 780.00 bytes/sectotal size is 255434 speedup is 654.96[[email protected] 111_dest]# ls1.txt 2.txt 3.txt a a.123 ip.txt new.new test[[email protected] 111_dest]# ls /root/abc/1.txt 2.txt 3.txt a a.123 ip.txt test[[email protected] 111_dest]#
Methods of synchronizing to other machine methods via SSH
Need to enter a password
rsync-avl/root/abc/[Email protected]:/tmp/test2/
Or
RSYNC-AVL [Email protected]:/tmp/test2//root/abc/
Editing a configuration file by using the background service method
/etc/rsyncd.conf
Add File Contents
port=873 //监听端口873,默认为873端口;log file=/var/log/rsync.log //指定日志文件pid file=/var/run/rsyncd.pid //指定pid文件address=192.168.188.2 //指定启动rsyncd服务的ip,默认为全部ip上启动;[test] //指定模块名,自定义;path=/root/rsync //指定数据存放路径use chroot=true //安全仿佛,默认为true,如果有软链接文件建议设置为false;max connections=4 //指定最大连接数,默认为0,没有限制;read only=no //如果为true,则不能上传到该模块指定目录list=true //uid=root //指定传输时使用哪个用户名进行gid=root //指定传输时使用哪个用户组auth users=test //指定传输时要使用哪个用户名secrets file=/etc/rsyncd.passwd //指定密码文件,文件权限为600,格式:用户名:密码hosts allow=192.168.133.132 1.1.1.1 2.2.2.2 192.168.133.0/24 //
Start the service
Rsync--daemon
Synchronization format:
Not completed, tomorrow to be continued
Linux File Sync Tool-rsync