linux命令rysnc是一个在linux系统上通过网络在系统间同步文件夹和文件的网络协议。前提rsync需在远程和本地系统上安装2.rsync用法rsync [参数] source path destination pathrsync参数参数说明-v详细模式输出传输时的进度等信息-z传输时进行压缩以提高传输效率-a归档模式表示以递归方式传输文件并保持所有文件属性等于-rtopgdl-l会把软链接指向的目标文件复制到对端目录中-e指定使用ssh还是其他方式来数据同步--bwlimit10限制传输速度--excludePATH表示指定排除不需要传输的文件等号后面跟文件名可以是万用字符模式(如*.txt)--delete表示删除DST中RSC没有的文件3.实例3.1.本地目录传输命令rsync -avz /root/245/ /opt/24512345678910111213141516171819[rootkibana ~]# rsync -avz /root/245/ /opt/245sending incrementalfilelistcreated directory/opt/245./245.txtaa.txtbb.txttest-/root/192.168.10.245.txtsent 308 bytes received 110 bytes 836.00 bytes/sectotal size is 116 speedup is 0.28[rootkibana 245]# pwd/opt/245[rootkibana 245]# ls -l总用量 12-rw-r--r-- 1 root root 52 11月 14 09:04 245.txt-rw-r--r-- 1 root root 16 11月 14 09:05 aa.txt-rw-r--r-- 1 root root 24 11月 14 09:06 bb.txtlrwxrwxrwx 1 root root 24 11月 14 09:05test-/root/192.168.10.245.txt[rootkibana 245]#3.2.增量同步命令rsync -avz /root/245/ /opt/2451234567891011121314151617181920212223242526272829303132[rootkibana 245]# pwd/root/245[rootkibana 245]# ls245.txt aa.txt bb.txttest[rootkibana 245]# touch cc.txt[rootkibana 245]# echo cc cc.txt[rootkibana 245]# ll总用量 16-rw-r--r-- 1 root root 52 11月 14 09:04 245.txt-rw-r--r-- 1 root root 16 11月 14 09:05 aa.txt-rw-r--r-- 1 root root 24 11月 14 09:06 bb.txt-rw-r--r-- 1 root root 3 11月 14 09:26 cc.txtlrwxrwxrwx 1 root root 24 11月 14 09:05test-/root/192.168.10.245.txt[rootkibana 245]# rsync -avz /root/245/ /opt/245 #首次会在本地和远程之间拷贝所有内容后续只会传输发生改变的块或者字节。sending incrementalfilelist./cc.txtsent 229 bytes received 38 bytes 534.00 bytes/sectotal size is 119 speedup is 0.45[rootkibana 245]#[rootkibana 245]# pwd/opt/245[rootkibana 245]# ls -l总用量 16-rw-r--r-- 1 root root 52 11月 14 09:04 245.txt-rw-r--r-- 1 root root 16 11月 14 09:05 aa.txt-rw-r--r-- 1 root root 24 11月 14 09:06 bb.txt-rw-r--r-- 1 root root 3 11月 14 09:26 cc.txtlrwxrwxrwx 1 root root 24 11月 14 09:05test-/root/192.168.10.245.txt[rootkibana 245]# cat cc.txtcc[rootkibana 245]#3.3.本地目录传输(不拷贝软链接)命令rsync -rvz /root/245/ /opt/2451234567891011121314151617181920212223242526272829[rootkibana 245]# rsync -rvz /root/245/ /opt/245sending incrementalfilelistcreated directory/opt/245skipping non-regularfiletest./245.txtaa.txtbb.txtcc.txtztj/ztj/ztj.txtsent 454 bytes received 190 bytes 1,288.00 bytes/sectotal size is 123 speedup is 0.19[rootkibana 245]#[rootkibana opt]# pwd/opt[rootkibana opt]# ls245[rootkibana opt]# cd 245/[rootkibana 245]# ls245.txt aa.txt bb.txt cc.txt ztj[rootkibana 245]# ll总用量 16-rw-r--r-- 1 root root 52 11月 14 09:32 245.txt-rw-r--r-- 1 root root 16 11月 14 09:32 aa.txt-rw-r--r-- 1 root root 24 11月 14 09:32 bb.txt-rw-r--r-- 1 root root 3 11月 14 09:32 cc.txtdrwxr-xr-x 2 root root 21 11月 14 09:32 ztj[rootkibana 245]#3.4.本地目录传输(不拷贝ztj目录)命令rsync --excludeztj -avz /root/245/ /opt/24512345678910111213141516171819202122232425[rootkibana 245]# ls245.txt aa.txt bb.txt cc.txttestzt[rootkibana 245]# rsync --excludeztj -avz /root/245/ /opt/245sending incrementalfilelist./245.txtaa.txtbb.txtcc.txttest-/root/192.168.10.245.txtsent 372 bytes received 98 bytes 940.00 bytes/sectotal size is 119 speedup is 0.25[rootkibana 245]#[rootkibana 245]# pwd/opt/245[rootkibana 245]# ls245.txt aa.txt bb.txt cc.txttest[rootkibana 245]# ll总用量 16-rw-r--r-- 1 root root 52 11月 14 09:04 245.txt-rw-r--r-- 1 root root 16 11月 14 09:05 aa.txt-rw-r--r-- 1 root root 24 11月 14 09:06 bb.txt-rw-r--r-- 1 root root 3 11月 14 09:26 cc.txtlrwxrwxrwx 1 root root 24 11月 14 09:05test-/root/192.168.10.245.txt[rootkibana 245]#3.5.本地目录传输(删除destination path中source path没有的文件)命令rsync --excludeztj --delete -avz /root/245/ /opt/245123456789101112131415161718[rootkibana 245]# pwd/opt/245[rootkibana 245]# touch test.txt[rootkibana 245]# ls245.txt aa.txt bb.txt cc.txttesttest.txt[rootkibana 245]# rsync --excludeztj --delete -avz /root/245/ /opt/245sending incrementalfilelistdeletingtest.txtsent 179 bytes received 24 bytes 406.00 bytes/sectotal size is 119 speedup is 0.59[rootkibana 245]# ll总用量 16-rw-r--r-- 1 root root 52 11月 14 09:04 245.txt-rw-r--r-- 1 root root 16 11月 14 09:05 aa.txt-rw-r--r-- 1 root root 24 11月 14 09:06 bb.txt-rw-r--r-- 1 root root 3 11月 14 09:26 cc.txtlrwxrwxrwx 1 root root 24 11月 14 09:05test-/root/192.168.10.245.txt[rootkibana 245]#3.6.远程主机同步命令rsync -avz /root/245/ root192.168.10.247:/opt/245/1234567891011121314151617181920[rootkibana 245]# rsync -avz /root/245/ root192.168.10.247:/opt/245/The authenticity of host192.168.10.247 (192.168.10.247)cant be established.ECDSA key fingerprint is SHA256:on9qXOFnb3h2UVou3YekMaIqC8l4pD0IvCMimfQwFsA.ECDSA key fingerprint is MD5:52:fe:8d:28:ea:f0:33:45:b1:1c:e1:29:d0:b4:e3:2a.Are you sure you want tocontinueconnecting (yes/no)?yesWarning: Permanently added192.168.10.247(ECDSA) to the list of known hosts.root192.168.10.247s password:sending incrementalfilelistcreated directory/opt/245./245.txtaa.txtbb.txtcc.txttest-/root/192.168.10.245.txtztj/ztj/ztj.txtsent 480 bytes received 156 bytes 181.71 bytes/sectotal size is 123 speedup is 0.19[rootkibana 245]#12345678910[rootrhel77 ~]# cd /opt/245/[rootrhel77 245]# ls -ltotal 16-rw-r--r-- 1 root root 52 Nov 14 09:04 245.txt-rw-r--r-- 1 root root 16 Nov 14 09:05 aa.txt-rw-r--r-- 1 root root 24 Nov 14 09:06 bb.txt-rw-r--r-- 1 root root 3 Nov 14 09:26 cc.txtlrwxrwxrwx 1 root root 24 Nov 14 09:05 test - /root/192.168.10.245.txt #远程主机拷贝软链接文件不会同步拷贝过来drwxr-xr-x 2 root root 21 Nov 14 09:28 ztj[rootrhel77 245]#3.7.指定使用ssh方式同步数据命令rsync -avz /root/245/ -e ssh -p 22 root192.168.10.247:/opt/245/123456789101112131415[rootkibana 245]# rsync -avz /root/245/ -e ssh -p 22 root192.168.10.247:/opt/245/root192.168.10.247s password:sending incrementalfilelistcreated directory/opt/245./245.txtaa.txtbb.txtcc.txttest-/root/192.168.10.245.txtztj/ztj/ztj.txtsent 480 bytes received 156 bytes 424.00 bytes/sectotal size is 123 speedup is 0.19[rootkibana 245]#12345678910[rootrhel77 ~]# cd /opt/245/[rootrhel77 245]# ls -ltotal 16-rw-r--r-- 1 root root 52 Nov 14 09:04 245.txt-rw-r--r-- 1 root root 16 Nov 14 09:05 aa.txt-rw-r--r-- 1 root root 24 Nov 14 09:06 bb.txt-rw-r--r-- 1 root root 3 Nov 14 09:26 cc.txtlrwxrwxrwx 1 root root 24 Nov 14 09:05test-/root/192.168.10.245.txt#远程主机拷贝软链接文件不会同步拷贝过来drwxr-xr-x 2 root root 21 Nov 14 09:28 ztj[rootrhel77 245]#3.8.rsync帮助命令命令rsync --help123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156[rootkibana 245]# rsync --helprsyncversion 3.1.2 protocol version 31Copyright (C) 1996-2015 by Andrew Tridgell, Wayne Davison, and others.Web site: http://rsync.samba.org/Capabilities:64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints,socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,append, ACLs, xattrs, iconv, symtimes, preallocrsynccomes with ABSOLUTELY NO WARRANTY. This isfreesoftware, and youare welcome to redistribute it under certain conditions. See the GNUGeneral Public Licencefordetails.rsyncis afiletransfer program capable of efficient remote updatevia a fast differencing algorithm.Usage:rsync[OPTION]... SRC [SRC]... DESTorrsync[OPTION]... SRC [SRC]... [USER]HOST:DESTorrsync[OPTION]... SRC [SRC]... [USER]HOST::DESTorrsync[OPTION]... SRC [SRC]...rsync://[USER]HOST[:PORT]/DESTorrsync[OPTION]... [USER]HOST:SRC [DEST]orrsync[OPTION]... [USER]HOST::SRC [DEST]orrsync[OPTION]...rsync://[USER]HOST[:PORT]/SRC[DEST]The:usages connect via remote shell,while::rsync://usages connectto anrsyncdaemon, and require SRC or DEST to start with a module name.Options-v, --verbose increase verbosity--infoFLAGS fine-grained informational verbosity--debugFLAGS fine-grained debug verbosity--msgs2stderr special output handlingfordebugging-q, --quiet suppress non-error messages--no-motd suppress daemon-mode MOTD (see manpage caveat)-c, --checksum skip based on checksum, not mod-time size-a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)--no-OPTION turn off an implied OPTION (e.g. --no-D)-r, --recursive recurse into directories-R, --relative use relative path names--no-implied-dirsdont send implieddirswith --relative-b, --backupmakebackups (see --suffix --backup-dir)--backup-dirDIRmakebackups into hierarchy basedinDIR--suffixSUFFIXsetbackup suffix (default ~ w/o--backup-dir)-u, --update skip files that are newer on the receiver--inplace update destination filesin-place (SEE MAN PAGE)--append append data onto shorter files--append-verify like --append, but with old datainfilechecksum-d, --dirstransfer directories without recursing-l, --links copy symlinks as symlinks-L, --copy-links transformsymlinkinto referentfile/dir--copy-unsafe-links onlyunsafesymlinks are transformed--safe-links ignore symlinks that point outside thesourcetree--munge-links munge symlinks tomakethem safer (but unusable)-k, --copy-dirlinks transformsymlinkto adirinto referentdir-K, --keep-dirlinks treat symlinkeddiron receiver asdir-H, --hard-links preserve hard links-p, --perms preserve permissions-E, --executability preserve thefiles executability--chmodCHMOD affectfileand/ordirectory permissions-A, --acls preserve ACLs (implies --perms)-X, --xattrs preserve extended attributes-o, --owner preserve owner (super-user only)-g, --group preserve group--devices preserve device files (super-user only)--copy-devices copy device contents as regularfile--specials preserve special files-D same as --devices --specials-t, --timespreserve modificationtimes-O, --omit-dir-timesomit directories from --times-J, --omit-link-timesomit symlinks from --times--super receiver attempts super-user activities--fake-super store/recoverprivileged attrs using xattrs-S, --sparse handle sparse files efficiently--preallocate allocate dest files before writing them-n, --dry-run perform a trial run with no changes made-W, --whole-filecopy files whole (without delta-xfer algorithm)-x, --one-file-system dont cross filesystem boundaries-B, --block-sizeSIZE force a fixed checksum block-size-e, --rshCOMMAND specify the remote shell to use--rsync-pathPROGRAM specify thersyncto run on the remote machine--existing skip creating new files on receiver--ignore-existing skip updating files that already exist on receiver--remove-source-files sender removes synchronized files (non-dirs)--del analiasfor--delete-during--delete delete extraneous files from destinationdirs--delete-before receiver deletes before transfer, not during--delete-during receiver deletes during the transfer--delete-delayfinddeletions during, delete after--delete-after receiver deletes after transfer, not during--delete-excluded also delete excluded files from destinationdirs--ignore-missing-args ignore missingsourceargs without error--delete-missing-args delete missingsourceargs from destination--ignore-errors delete evenifthere are I/Oerrors--force force deletion of directories evenifnot empty--max-deleteNUM dont deletemorethan NUM files--max-sizeSIZE dont transfer anyfilelarger than SIZE--min-sizeSIZE dont transfer anyfilesmaller than SIZE--partial keep partially transferred files--partial-dirDIR put a partially transferredfileinto DIR--delay-updates put all updated files into place at transfers end-m, --prune-empty-dirsprune empty directory chains from thefile-list--numeric-ids dont map uid/gidvalues by user/groupname--usermapSTRING custom username mapping--groupmapSTRING custom groupname mapping--chownUSER:GROUP simple username/groupnamemapping--timeoutSECONDSsetI/Otimeoutinseconds--contimeoutSECONDSsetdaemon connection timeoutinseconds-I, --ignore-timesdont skip files that matchinsize and mod-time-M, --remote-optionOPTION send OPTION to the remote side only--size-only skip files that matchinsize--modify-windowNUM compare mod-timeswith reduced accuracy-T, --temp-dirDIR create temporary filesindirectory DIR-y, --fuzzyfindsimilarfileforbasisifno destfile--compare-destDIR also compare destination files relative to DIR--copy-destDIR ... and include copies of unchanged files--link-destDIR hardlink to filesinDIR when unchanged-z, --compress compressfiledata during the transfer--compress-levelNUM explicitlysetcompression level--skip-compressLIST skip compressing files with a suffixinLIST-C, --cvs-exclude auto-ignore files the same way CVS does-f, --filterRULE add afile-filtering RULE-F same as --filterdir-merge /.rsync-filterrepeated: --filter- .rsync-filter--excludePATTERN exclude files matching PATTERN--exclude-fromFILEreadexclude patterns from FILE--includePATTERN dont exclude files matching PATTERN--include-fromFILEreadinclude patterns from FILE--files-fromFILEreadlist ofsource-filenames from FILE-0, --from0 all *-from/filterfiles are delimited by 0s-s, --protect-args no space-splitting; only wildcard special-chars--addressADDRESS bind addressforoutgoing socket to daemon--portPORT specify double-colon alternate port number--sockoptsOPTIONS specify custom TCP options--blocking-io use blocking I/Oforthe remote shell--stats give somefile-transfer stats-8, --8-bit-output leave high-bit chars unescapedinoutput-h, --human-readable output numbersina human-readableformat--progress show progress during transfer-P same as --partial --progress-i, --itemize-changes output a change-summaryforall updates--out-formatFORMAT output updates using the specified FORMAT--log-fileFILE log what were doing to the specified FILE--log-file-formatFMT log updates using the specified FMT--password-fileFILEreaddaemon-access password from FILE--list-only list the files instead of copying them--bwlimitRATE limit socket I/Obandwidth--outbufN|L|Bsetoutput buffering to None, Line, or Block--write-batchFILE write a batched update to FILE--only-write-batchFILE like --write-batch but w/oupdating destination--read-batchFILEreada batched update from FILE--protocolNUM force an older protocol version to be used--iconvCONVERT_SPEC request charset conversion of filenames--checksum-seedNUMsetblock/filechecksum seed (advanced)-4, --ipv4 prefer IPv4-6, --ipv6 prefer IPv6--version print version number(-h) --help show this help (-h is --help onlyifused alone)Usersync --daemon --helpto see the daemon-modecommand-line options.Please see thersync(1) and rsyncd.conf(5)manpagesforfull documentation.See http://rsync.samba.org/forupdates, bug reports, and answers[rootkibana 245]#