• rsync同步基本用法


                                                                                                               rsync基本用法

    1案例1rsync基本用法

    1.1问题

    本例要求掌握远程同步的基本操作,使用rsync命令完成下列任务:

    将目录/boot同步到目录/todir

    将目录/boot下的文档同步到目录/todir

    在目录/boot下新增文件a.txt,删除/todir下的子目录grub2,再次同步使/todir/boot一致

    验证-a-n-v--delete选项的含义

    1.2方案

    本地同步操作:

    rsync[选项...]本地目录1本地目录2

    rsync[选项...]本地目录1/本地目录2

    rsync同步工具的常用选项:

    -n:测试同步过程,不做实际修改

    --delete:删除目标文件夹内多余的文档

    -a:归档模式,相当于-rlptgoD

    -v:显示详细操作信息

    -z:传输过程中启用压缩/解压

    1.3步骤

    实现此案例需要按照如下步骤进行。

    步骤一:rsync同步基本操作

    1)将目录/boot同步到目录/todir

    [root@svr7~]#ls -l /todir //同步前

    ls:无法访问/todir:没有那个文件或目录

    [root@svr7~]#rsync -a /boot /todir  //将目录1作为目录2的子目录

    [root@svr7~]#ls -l /todir   //检查同步结果

    总用量4

    dr-xr-xr-x.4 root root 4096 1130 18:50 boot

    2)将目录/boot下的文档同步到目录/todir

    [root@svr7~]#rm -rf /todir //清理掉目录2

    [root@svr7~]#rsync -a /boot/ /todir  //将目录1下的文档同步到目录2

    [root@svr7~]#ls -l /todir //检查同步结果

    总用量126708

    -rw-r--r--.1 root root 126426 1030 2015 config-3.10.0-327.el7.x86_64

    drwxr-xr-x.2 root root 4096 1130 18:50 extlinux

    drwx------.6 root root 104 129 09:58 grub2

    ....

    3)同步效果测试

    ① 在目录/boot下新增文件a.txt,删除/todir下的子目录grub2

    [root@svr7~]#touch /boot/a.txt

    [root@svr7~]#rm -rf /todir/grub2/

    ② 现在目录/boot/todir目录下的内容已经不一致了:

    [root@svr7~]#ls -ld /boot/a.txt /todir/a.txt

    ls:无法访问/todir/a.txt:没有那个文件或目录

    -rw-r--r--.1 root root 0 111 21:09/boot/a.txt

    [root@svr7~]#ls -ld /boot/grub2 /todir/grub2

    ls:无法访问/todir/grub2:没有那个文件或目录

    drwx------.6 root root 104 129 09:58/boot/grub2

    ③ 再次同步使/todir/boot一致

    [root@svr7~]#rsync -a  /boot/ /todir/

    确认同步结果:

    [root@svr7~]#ls -ld /boot/a.txt/ todir/a.txt

    -rw-r--r--.1 root root 0 111 21:09/boot/a.txt

    -rw-r--r--.1 root root 0 111 21:09/todir/a.txt

    [root@svr7~]#ls-ld/boot/grub2/todir/grub2

    drwx------.6 root root 104 129 09:58/boot/grub2

    drwx------.6 root root 104 129 09:58/todir/grub2


    步骤二:验证-a-v-n--delete选项的含义

    1)验证-a选项

    ④ 当目录1包含文件夹时,若缺少-a-r选项则文件夹会被忽略:

    [root@svr7~]#rsync /home /testa

    skipping directory home

    [root@svr7~]#ls -ld /testa

    ⑤ ls:无法访问/testa:没有那个文件或目录

    添加-a后才会执行同步:

    [root@svr7~]#rsync -a /home/ /testa

    [root@svr7~]#ls -ld /testa

    drwxr-xr-x.4 root root 31 16 17:33/testa

    类似的情况,当目录1中的数据出现权限、归属、修改时间等变化时,若文件内容不变默认不会同步,若希望目录2也同步这些变化,也需要-a选项。

    2)验证-v选项

    a. 创建测试目录及文档:

    [root@svr7~]#mkdir /fdir

    [root@svr7~]#touch /fdir/1.txt

    b.添加-v选项时,可以看到操作细节信息,比如第一次同步时:

    [root@svr7~]#rsync -av /fdir/ /tdir

    sending incremental file list

    created directory/tdir

    ./

    c. 1.txt//传输文档列表

    sent 82 bytes received 34 bytes 232.00 bytes/sec

    total size is 0 speedup is 0.00

    d. 在目录/fdir/添加文件2.txt,再次跟踪同步信息:

    [root@svr7~]#touch /fdir/2.txt

    sending incremental file list

    ./

    2.txt//传输文档列表

    sent 100 bytes received 34 bytes 268.00 bytes/sec

    total size is 0 speedup is 0.00

    e. 确认目录1和目录2的内容已经一致

    [root@svr7~]#ls /fdir/ /tdir/

    /fdir/:

    1.txt 2.txt

    /tdir/:

    1.txt 2.txt

    f. 再次跟踪同步信息,已经无需传输文件:

    [root@svr7~]#rsync -av /fdir/ /tdir

    sending incremental file list

    sent 58 bytes received 12 bytes 140.00 bytes/sec

    total size is 0 speedup is 0.00

    3)验证-n选项

    ① -n-v选项合用,可以模拟同步过程,显示需要做哪些操作(但并不真的同步)。     在目录/fdir下新建文件3.txt,测试同步操作

    [root@svr7~]#touch /fdir/3.txt

    [root@svr7~]#rsync -avn /fdir/ /tdir/

    sending incremental file list

    ./

    ② 3.txt//提示同步时会传输哪些文件

    sent 78 bytes received 18 bytes 192.00 bytes/sec

    total size is 0 speedup is 0.00(DRY RUN)

    [root@svr7~]#ls -l /tdir/3.txt//但实际并未真的同步

    ③ ls:无法访问/tdir/3.txt:没有那个文件或目录

    去掉-n选项才会真正同步:

    [root@svr7~]#rsync -av /fdir/ /tdir/

    sending incremental file list

    ./

    3.txt

    sent 114 bytes received 34 bytes 296.00 bytes/sec

    total size is 0 speedup is 0.00

    [root@svr7~]#ls -l /tdir/3.txt

    -rw-r--r--.1 root root 0 111 21:46/tdir/3.txt

    4)验证--delete选项

    rsync同步操作默认只是将目录1的数据同步到目录2,但如果目录2存在多余的文件却并不会去除,除非添加—delete选项。

    在目录/fdir/tdir已经完成同步后,删除/tdir/2.txt文件,再次同步:

    [root@svr7~]#rm -rf /fdir/2.txt

    [root@svr7~]#rsync -a /fdir/ /tdir/

    A.检查发现目标文件夹/tdir下的2.txt文件还在:

    [root@svr7~]#ls /fdir//tdir/

    /fdir/:

    1.txt 3.txt

    /tdir/:

    1.txt 2.txt 3.txt

    B. 这种情况下添加--delete选项再次执行同步,两个目录的内容就一致了

    [root@svr7~]#rsync -a --delete /fdir/ /tdir/

    [root@svr7~]#ls /fdir/ /tdir/

    /fdir/:

    1.txt 3.txt

    /tdir/:

    1.txt 3.txt

  • 相关阅读:
    day29
    day28
    day27
    查询区间内有多少个不同的数(线段树/树状数组)
    树状数组变形:带区间修改的树状数组
    特征提取:PCA主成分分析法和NMF非负矩阵分解法
    Trie树
    Logistic&Softmax回归
    高斯贝叶斯分类器(GNB实战)
    朴素贝叶斯分类器(伯努利贝叶斯+高斯贝叶斯+多项式贝叶斯)
  • 原文地址:https://www.cnblogs.com/qingbai/p/11912214.html
Copyright © 2020-2023  润新知