|
|
楼主 |
发表于 2024-5-23 13:57:26
|
显示全部楼层
1.5.0 同步时删除多余文件9 g# m Y6 \; g- T5 }: a; q
在服务器A上同步/opt/work目录到远程服务器B的/tmp/work目录下同时删除B服务器/work/jira/data/目录下相比于A服务器/tmp/work中多余的文件,最近在处理策划资源文件的时候需要将目录A的文件全部同步到目录B的文件,并且把目录B内多余的文件全部删除掉。所以,就想到了使用rsync的--delete参数来实现功能
* D. p+ t; |( R# t) ?
. |& i0 F# ?. g1 b& |! i1)A服务器
3 n' p7 a3 o3 B[root@serverA ~]# cd /tmp/work
) A0 b* _2 Z. j7 J[root@serverA work]# ls
/ o, J3 }! p3 e; I3 w9 ma b c d 11/ W1 A* G* x" o B7 o$ s% v g, R
1 P+ i% ~, W4 L" V7 [; g9 a% U$ _: [
2)B服务器& z h; j* P4 L
[root@serverB ~]# cd /tmp/work% x9 q0 V9 k' }
[root@serverB work]# ls& k# [3 i- n: c( C7 D
c d 11 12 13 fg 5t
/ M ?" V9 C; y- x. r0 C" V+ B6 i, \
$ n1 n) N+ A& @8 M! C, S3)从A服务器同步到B服务器(假设B服务器ip是192.168.130.11)) o" M! P/ f( T& I; d
[root@serverA work]#rsync -avzP --delete rsync -avzhP --delete itadmin@192.168.130.11:/opt/data/ /work/jira/data/ #注意,--delete参数要放在源目录和目标目录前,并且两个目录结构一定要一致!不能使用./*
. q0 t. }: z4 M' Dsending incremental file list3 v/ L7 D! [' ]) J5 a
./
( {9 C" n1 c5 M9 ndeleting fg
9 E r& E1 {8 }! Wdeleting 5t: G& ]( H. r# C3 G* C$ {6 M
deleting 13' X- X) H, K; P J* m3 y
deleting 12
( @+ E- e2 ] G, U11& [6 K: I; z% G8 b7 e! H
a6 y* M: N' N4 Y9 T. p
b
/ H0 V9 i% _5 m9 A: M! h. H% Lc" ^2 u1 r& n* ~" m
d
) [: m d# a% _+ ~: Z6 h; ]1.5.1 保留同步文件权限、属主、属组等信息7 m e1 r s% B# q# I8 |! h3 b
rsync -avz /path/to/dirA/ /path/to/dirB/9 f! C- E1 q5 S* v% Z' ]7 m6 B' g
1.5.2 不同步一些文件和目录4 S N# g1 I3 A' p3 y) i
2 O6 t5 B0 i: d; g b6 \8 g
rsync -avz --exclude="fileA1.txt" --exclude="/path/to/dirA/test/" /path/to/dirA/ /path/to/dirB/
" t" P, q# m! U2 p% d8 Rrsync -avz --exclude 'fileA1.txt' --exclude '/path/to/dirA/test/' /path/to/dirA/ /path/to/dirB/( C' Q6 m* _* o3 ^/ Z
1.5.3 拷贝本地文件
/ i) M/ }6 F [rsync -ahvz backup.tar.gz dirA/ # DESC 不存在时自动创建
' L; N/ [: ~ m$ t) L& Xrsync -ahvz backup.tar.gz /path/to/dirA/ /path/to/dirB/
; G' t& i" x, d7 s5 \6 z2 @1.5.4 shell拷贝到远程(推送)+ V/ f8 V/ k7 z9 E
rsync -avz /path/to/dirA/ user@remoteip:/path/to/dirB/
D( K- m* @5 f; x1 V& S* _" ~9 y1.5.5 shell拷贝到本地(拉取) l7 K4 T$ C% F" N5 W+ `1 [
rsync -avz user@remoteip:/path/to/dirA/ /path/to/dirB/1 e! g; X5 j6 P" E& Y' {) l, j1 ~
1.5.6 远程rsync服务器拷贝到本地- V8 s9 c3 E% |1 Z6 B; f3 _5 r
rsync -av user@remoteip::www /path/to/dirB/ #www为rsync模块命令
% m3 `& M& |6 i( g1.5.7 拷贝本地文件到rsync服务器
+ g; b: p- p! ]6 w0 b! i3 _rsync -av /path/to/dirB/ user@remoteip::www # y/ R8 T5 h4 C' k n( ]4 q
1.5.8 rsync 使用非默认端口传输
0 e% e# B- X* B6 \! Irsync -avz -e "ssh -p $port"/path/to/dirA/ user@remoteip:/path/to/dirB/
3 b! R- o; w; w) e1.5.9 显示备份进度
4 Q9 ~8 t) ~7 P可以使用 --progress或者P 选项来显示进度0 O4 U3 ^0 s9 `; S% x
3 c3 n5 r7 T H- c* {% Irsync -avzhe ssh --progress /path/to/dirA/ root@remoteip:/path/to/dirB/ |
|