ssh linux 同步,rsync工具介绍/rsync通过ssh同步

rsync工具介绍

数据备份是必不可少,在Linux系统下数据备份的工具很多,其中重点介绍就是rsync工具,rsync不仅可以远程同步数据,还可以本地同步数据,且不会覆盖以前的数据在已经存在的数据情况下,而是先判断已经存在的数据和新的数据差异,只有不同的时候才会把不同的部分覆盖。

将/etc/passwd文件同步到/tmp/目录下并改名为passwd_bak,如下代码所示:

[root@yolks-001 ~]# rsync -av /etc/passwd /tmp/passwd_bak

sending incremental file list

passwd

sent 2,044 bytes received 35 bytes 4,158.00 bytes/sec

total size is 1,952 speedup is 0.94

常用命令选项:

rsync [OPTION]... SRC DEST //将数据同步(复制)到本地指定的路径下。

rsync [OPTION]... SRC [USER@]HOST:DEST //将文件复制远程同步到指定的用户的指定路径下,上面的例子没有指定user默认就是root。

rsync [OPTION]... [USER@]HOST:SRC DEST //从远程目录同步数据到本地。

rsync [OPTION]... [USER@]HOST::SRC DEST //从远程目录同步数据到本地,加了两个冒号验证方式不同。

rsync [OPTION]... SRC [USER@]HOST::DEST //将文件复制远程同步到指定的用户的指定路径下,加了两个冒号验证方式不同。

rsync常用选项

选项:

-a:这是归档模式,表示已递归方式传输文件,并保持所有属性,它等同于 -rlptgoD。-a选项后面可以跟一个 --no-OPTION,表示关闭 -rlptgoD中的某一个,比如 -a--no-l等同 -rlptgoD。

-r:表示已递归模式处理子目录。它主要是针对目录来说的,如果单独传一个文件不需要加-r选项,但是传输目录时必须加。

-v:表示打印一些信息,比如文件列表、文件数量等。

-l:表示保留软链接。

-L:表示像对待常规文件一样处理软连接。如果是SRC中有软连接文件,则加上该选项后,将会把软连接指向的目标文件复制到DST。

-p:表示保持文件权限。

-o:表示保持文件属主信息。

-g:表示保持文件属组信息。

-D:表示保持设备文件信息(/dev/sdb1 这样的设备文件有它的特殊性,如果不加-D 可能拷贝过去就是一个非常普通的文件,不能当设备来用。)

-t:表示保持文件时间信息。

--delete:表示删除DST中SRC没有的文件。

--exclude=PATTERN:表示指定排除不需要传输的文件,等号后面跟文件名,可以是通用字符模式(比如*.txt)。

-P(--progress):表示在同步的过程中可以看到同步的过程状态,比如统计要同步的文件数量、同步的文件传输速度等。

-u:表示把DST中比SRC还新的文件排除掉,不会覆盖。

-z:加上该选项,将会在传输过程中压缩。

常用选项演示:

首先创建模拟数据

[root@yolks-001 test_rsync]# ls

1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7 8

使用 -av 选项

[root@yolks-001 test_rsync]# rsync -av /root/test_rsync/ /tmp/test_rsync_dest/

sending incremental file list

created directory /tmp/test_rsync_dest

./

1.txt

2.txt

3.txt

4.txt

5.txt

6.txt

7/

8/

sent 441 bytes received 184 bytes 1,250.00 bytes/sec

total size is 0 speedup is 0.00

[root@yolks-001 test_rsync]# tree /tmp/test_rsync_dest/

/tmp/test_rsync_dest/

├── 1.txt

├── 2.txt

├── 3.txt

├── 4.txt

├── 5.txt

├── 6.txt

├── 7

└── 8

2 directories, 6 files

建议操作目录的话结尾都加上斜杠‘/’,如果不加的话则会将同步的文件到目标子目录去。

-L选项 : 把SRC中软连接的目标文件复制到DST

[root@yolks-001 test_rsync]# ll

总用量 0

-rw-r--r-- 1 root root 0 7月 18 21:37 1.txt

-rw-r--r-- 1 root root 0 7月 18 21:37 2.txt

-rw-r--r-- 1 root root 0 7月 18 21:37 3.txt

-rw-r--r-- 1 root root 0 7月 18 21:37 4.txt

-rw-r--r-- 1 root root 0 7月 18 21:37 5.txt

-rw-r--r-- 1 root root 0 7月 18 21:37 6.txt

drwxr-xr-x 2 root root 6 7月 18 21:38 7

drwxr-xr-x 2 root root 6 7月 18 21:38 8

lrwxrwxrwx 1 root root 5 7月 18 22:30 test_ln -> 1.txt

[root@yolks-001 test_rsync]# rsync -avL /root/test_rsync/ /tmp/test_rsync_dest/

sending incremental file list

created directory /tmp/test_rsync_dest

./

1.txt

2.txt

3.txt

4.txt

5.txt

6.txt

test_ln

7/

8/

sent 506 bytes received 203 bytes 1,418.00 bytes/sec

total size is 0 speedup is 0.00

[root@yolks-001 test_rsync]# ls /tmp/test_rsync_dest/

1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7 8 test_ln

--delete : 删除掉就会删除新增的文件

[root@yolks-001 test_rsync]# touch /tmp/test_rsync_dest/new_fiel.txt

[root@yolks-001 test_rsync]# ls !$

ls /tmp/test_rsync_dest/new_fiel.txt

/tmp/test_rsync_dest/new_fiel.txt

[root@yolks-001 test_rsync]# rsync -avL --delete /root/test_rsync/ /tmp/test_rsync_dest/

sending incremental file list

deleting new_fiel.txt

./

test_ln

sent 266 bytes received 56 bytes 644.00 bytes/sec

total size is 0 speedup is 0.00

--exclude : 过滤掉不想同步的文件,可写多个exclude

[root@yolks-001 test_rsync]# touch 666.txt

[root@yolks-001 test_rsync]# rsync -avL --exclude="666*" /root/test_rsync/ /tmp/test_rsync_dest/

sending incremental file list

./

sent 228 bytes received 21 bytes 498.00 bytes/sec

total size is 0 speedup is 0.00

[root@yolks-001 test_rsync]# ls /tmp/test_rsync_dest/

1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7 8 test_ln

-P(--progress): 显示同步状态

[root@yolks-001 test_rsync]# rsync -avP --exclude="666*" /root/test_rsync/ /tmp/test_rsync_dest/

sending incremental file list

test_ln -> 1.txt

sent 233 bytes received 21 bytes 508.00 bytes/sec

total size is 5 speedup is 0.02

-u : 可以看到目标文件的心内容不会被源目录文件的覆盖

[root@yolks-001 test_rsync]# rsync -avPu --exclude="666*" /root/test_rsync/ /tmp/test_rsync_dest/

sending incremental file list

./

sent 233 bytes received 21 bytes 508.00 bytes/sec

total size is 5 speedup is 0.02

rsync通过ssh同步

rsync的5种命令格式中,第二种和三种(使用一个冒号)就属于通过ssh的方式备份数据,这种方式其实就是让用户登录到远程服务器上执行rsync的任务。

A机器:192.168.248.128 B机器:192.168.248.129 两台机器可以互相ping通

同步之前必须保证两台机器都安装rsync

1.使用A机器同步文件到B提示B机器不存在rsync

45008671.jpg

2.在B机器上安装rsync命令

yum install -y rsync

3.A机器同步文件到B机器

[root@yolks-001 test_rsync]# rsync /etc/passwd 192.168.248.129:/tmp/yolks_rsync #因为做了免密钥登录,一般需要输入密码

[root@yolks2 tmp]# ls -lh yolks* #使用B机器查看文件

-rw-r--r-- 1 root root 2.0K 7月 18 23:26 yolks_rsync

4.A机器上同步B机器的操作

rsync -avP 192.168.248.129:/tmp/yolks_rsync /etc/passwd

如果你的另外一台服务器端口并不是22,那么你可以加个选项来操作, -e 选项后面跟远程命令设定端口

rsync -avP -e "ssh -p 22" /etc/passwd/ 192.168.248.129:/tmp/yolks_rsync

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值