Linux 中 Rsync 备份数据使用实例

系统 Linux
rsync工具用于将文件和目录从一个位置同步到另一个位置。同步的位置可以在本地服务器或远程服务器上。

[[404604]]

rsync工具用于将文件和目录从一个位置同步到另一个位置。同步的位置可以在本地服务器或远程服务器上。

在Centos中使用下面命令安装rsync:

  1. [root@localhost ~]# yum -y install rsync 

实例一:本机中的两个目录进行同步

要同步本地计算机中的两个目录,使用rsync -zvr命令:

  1. [root@localhost ~]# rsync -zvr /var/log/ /root/temp
  2. sending incremental file list 
  3. btmp 
  4. dnf.librepo.log 
  5. ... 
  6. sssd/sssd_implicit_files.log 
  7. sssd/sssd_nss.log 
  8. tuned/tuned.log 
  9.  
  10. sent 516,136 bytes  received 605 bytes  1,033,482.00 bytes/sec 
  11. total size is 5,451,242  speedup is 10.55 

参数解释:

  • -z 启用压缩
  • -v 输出详细信息
  • -r 表示递归

查看一下/root/temp目录,发现rsync在同步期间未保留时间戳。

实例二:使用rsync -a在同步期间保留时间戳

rsync命令的-a选项表示存档模式。-a选项递归同步、保留符号链接、保留权限、保留时间戳、保留所有者和组。

现在,执行以下命令,然后查看文件的时间:

  1. [root@localhost ~]# rsync -azv /var/log/ /root/temp
  2. sending incremental file list 
  3. ./ 
  4. btmp 
  5. dnf.librepo.log 
  6. dnf.log 
  7. dnf.rpm.log 
  8. ... 
  9. sssd/sssd_nss.log 
  10. tuned/ 
  11. tuned/tuned.log 
  12.  
  13. sent 516,231 bytes  received 629 bytes  1,033,720.00 bytes/sec 
  14. total size is 5,451,789  speedup is 10.55 

如下所示,rsync在同步期间保留了时间戳。

实例三:将文件从本地同步到远程目录

rsync允许在本地和远程系统之间同步文件/目录,前提是本地和远程系统都要安装rsync才行,否则会提示如下信息:

  1. [root@localhost ~]# rsync -avz /root/temp/ root@192.168.43.137:/root/temp 
  2. root@192.168.43.137's password:  
  3. sending incremental file list 
  4. created directory /root/temp 
  5. ./ 
  6. btmp 
  7. dnf.librepo.log 
  8. dnf.log 
  9. dnf.rpm.log 
  10. ... 
  11. sssd/sssd_nss.log 
  12. tuned/ 
  13. tuned/tuned.log 
  14.  
  15. sent 516,231 bytes  received 662 bytes  206,757.20 bytes/sec 
  16. total size is 5,451,789  speedup is 10.55 

下面是在远程系统里面查看已同步的目录:

上面可以看到同步时需要输入密码,有时候不希望将文件从本地服务器备份到远程服务器时输入密码,可以在两台主机间设置免密要登录。

实例四:将文件从远程目录同步到本地

要将文件从远程系统同步到本地时,如下所示,在源中指定远程路径,在目标中指定本地路径即可:

  1. [root@localhost ~]# rsync -avz root@192.168.43.137:/root/temp /root/temp 
  2. root@192.168.43.137's password:  
  3. receiving incremental file list 
  4. temp
  5. temp/btmp 
  6. temp/dnf.librepo.log 
  7. temp/dnf.log 
  8. ... 
  9. temp/tuned/ 
  10. temp/tuned/tuned.log 
  11.  
  12. sent 634 bytes  received 516,247 bytes  206,752.40 bytes/sec 
  13. total size is 5,451,789  speedup is 10.55 

实例五:不要覆盖目标位置上已修改的文件

如果在目标位置修改了文件,我们可能不想用源位置的旧文件覆盖该文件。使用-u选项就可以做到这一点。在下面的示例中,在本地将test.txt文件修改了内容。它不会被远程系统的test.txt文件所覆盖:

  1. # 查看一下远程系统temp目录下的test.txt文件大小 
  2. [root@localhost ~]# ssh root@192.168.43.137 ls -l /root/temp 
  3. root@192.168.43.137's password:  
  4. total 4 
  5. -rw-r--r--. 1 root root 7 Apr  7  2021 test.txt 
  6. # 查看一下本机的temp目录下的test.txt文件大小,本机的test.txt文件已修改,所以比远程系统里面的test.txt文件大 
  7. [root@localhost ~]# ll /root/temp
  8. total 4 
  9. -rw-r--r--. 1 root root 77 Apr  7 21:10 test.txt 
  10. # 执行rsync -avzu同步一下 
  11. [root@localhost ~]# rsync -avzu root@192.168.43.137:/root/temp /root/ 
  12. root@192.168.43.137's password:  
  13. receiving incremental file list 
  14.  
  15. sent 25 bytes  received 76 bytes  40.40 bytes/sec 
  16. total size is 7  speedup is 0.07 

下面查看一下本机的/root/temp目录里面的test.txt是否被覆盖:

发现并没有被覆盖。

实例六:在传输过程中查看rsync进度

使用--progress选项显示rsync执行的详细进度,如下所示:

  1. [root@localhost ~]# rsync -avz --progress /root/temp/ root@192.168.43.137:/root/temp 

实例七:在目标目录中删除源目录不存在的文件

如果文件不在源中而是在目标中存在,则可能希望在rsync同步期间删除目标上的文件。在这种情况下,请使用--delete选项:

  1. # 查看一下源目录里面的文件 
  2. [root@localhost ~]# ll /root/temp
  3. total 0 
  4. -rw-r--r--. 1 root root 0 Apr  7 21:46 name.csv 
  5. # 查看一下目标目录里面的文件 
  6. [root@localhost ~]# ssh root@192.168.43.137 ls -l /root/temp 
  7. root@192.168.43.137's password:  
  8. total 944 
  9. drwxr-xr-x. 2 root root      6 Apr  7  2021 anaconda 
  10. drwx------. 2 root root      6 Apr  7  2021 audit 
  11. -rw-------. 1 root root      0 Apr  7  2021 btmp 
  12. -rw-------. 1 root root      0 Apr  7  2021 btmp-20210406 
  13. drwxr-xr-x. 2 root root      6 Apr  7  2021 chrony 
  14. -rw-------. 1 root root   8432 Apr  7  2021 cron 
  15. -rw-------. 1 root root  12200 Apr  7  2021 cron-20210221 
  16. -rw-------. 1 root root  48130 Apr  7  2021 cron-20210228 
  17. -rw-------. 1 root root   3910 Apr  7  2021 cron-20210308 
  18. -rw-------. 1 root root  22455 Apr  7  2021 cron-20210406 
  19. -rw-------. 1 root root 383369 Apr  7  2021 dnf.librepo.log 
  20. -rw-------. 1 root root 476949 Apr  7  2021 dnf.librepo.log-20210221 
  21. # rsync使用--delete选项删除目标目录中不包含源目录的文件 
  22. [root@localhost ~]# rsync -avz --delete /root/temp root@192.168.43.137:/root 
  23. root@192.168.43.137's password:  
  24. sending incremental file list 
  25. deleting temp/chrony/ 
  26. deleting temp/audit/ 
  27. deleting temp/anaconda/ 
  28. deleting temp/dnf.librepo.log-20210221 
  29. deleting temp/dnf.librepo.log 
  30. deleting temp/cron-20210406 
  31. deleting temp/cron-20210308 
  32. deleting temp/cron-20210228 
  33. deleting temp/cron-20210221 
  34. deleting temp/cron 
  35. deleting temp/btmp-20210406 
  36. deleting temp/btmp 
  37. temp
  38. temp/name.csv 
  39.  
  40. sent 123 bytes  received 281 bytes  161.60 bytes/sec 
  41. total size is 0  speedup is 0.00 

在查看一下目标目录是否删除:

实例八:文件传输过程中的include和exclude模式

rsync允许在进行同步时提供要包括和排除文件或目录的模式。

  1. [root@localhost ~]# rsync -avz --include 'P*' --exclude '*' root@192.168.43.137:/var/lib/rpm/ /root/temp/ 

在上面的示例中,它仅包括以'P'开头的文件或目录,并排除所有其他文件。

实例九:不传输大文件

可以使用rsync --max-size选项告诉rsync不要传输大于指定大小的文件。

  1. [root@localhost ~]# rsync -avz --max-size='1M' root@192.168.43.137:/var/lib/rpm/ /root/temp/ 

--max-size=1M使rsync仅传输小于或等于1M的文件。单位可以是K,M,G等。

还可以使用--min-size=参数,指定传输最小文件的大小。

本文转载自微信公众号「Linux就该这么学」,可以通过以下二维码关注。转载本文请联系Linux就该这么学公众号。

 

责任编辑:武晓燕 来源: Linux就该这么学
相关推荐

2021-06-18 10:28:56

Linuxrsync命令

2017-03-01 12:19:17

rsync Linux系统

2010-09-14 09:15:03

RsyncLinux备份远程数据同步

2011-08-22 16:03:30

linuxVPS备份数据库

2021-09-13 15:31:28

戴尔

2010-05-26 09:01:43

mysqldump备份

2011-03-17 16:42:00

2015-08-21 18:58:05

亚马逊EBS备份增量备份

2009-03-09 20:57:28

linuxrsync文件同步备份

2017-02-10 10:40:29

macOSTime MachinGitlab

2010-06-07 14:09:12

mysqldump备份

2010-10-26 10:02:05

oracle备份命令

2010-03-02 09:47:03

Fedora MySQ

2021-03-01 09:40:54

数据安全软件

2010-09-13 16:46:10

SQL Server触

2019-05-17 08:24:11

LinuxLinux备份rsync命令

2015-07-27 09:33:26

备份数据加密工具

2020-11-12 09:38:31

安全数据勒索软件

2014-12-23 09:37:09

Linuxrsync

2022-02-08 12:19:36

LinuxJQ命令
点赞
收藏

51CTO技术栈公众号