使用rsync备份数据具体方法

rsync是一款实现远程同步功能的软件,在同步文件的同时,可以保持原来文件的权限,时间,软硬链接等附加信息。Rsync是使用rsync算法提供一个客户机和远程文件服务器的文件同步的快速算法,而且可以同步ssh的方式来传输,本篇文章将重点讲解一下使用rsync备份数据具体方法。

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

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

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

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

[root@localhost ~]# rsync -zvr /var/log/ /root/temp/
sending incremental file list
btmp
dnf.librepo.log
...
sssd/sssd_implicit_files.log
sssd/sssd_nss.log
tuned/tuned.log

sent 516,136 bytes  received 605 bytes  1,033,482.00 bytes/sec
total size is 5,451,242  speedup is 10.55

参数解释:

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

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

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

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

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

[root@localhost ~]# rsync -azv /var/log/ /root/temp/
sending incremental file list
./
btmp
dnf.librepo.log
dnf.log
dnf.rpm.log
...
sssd/sssd_nss.log
tuned/
tuned/tuned.log

sent 516,231 bytes  received 629 bytes  1,033,720.00 bytes/sec
total size is 5,451,789  speedup is 10.55

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

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

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

[root@localhost ~]# rsync -avz /root/temp/ [email protected]:/root/temp[email protected]'s password: sending incremental file list created directory /root/temp ./ btmp dnf.librepo.log dnf.log dnf.rpm.log ... sssd/sssd_nss.log tuned/ tuned/tuned.log sent 516,231 bytes  received 662 bytes  206,757.20 bytes/sec total size is 5,451,789  speedup is 10.55 

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

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

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

[root@localhost ~]# rsync -avz [email protected]:/root/temp /root/temp[email protected]'s password: receiving incremental file list temp/ temp/btmp temp/dnf.librepo.log temp/dnf.log ... temp/tuned/ temp/tuned/tuned.log sent 634 bytes  received 516,247 bytes  206,752.40 bytes/sec total size is 5,451,789  speedup is 10.55 

Linux中rsync备份数据使用实例Linux中rsync备份数据使用实例

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

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

# 查看一下远程系统temp目录下的test.txt文件大小
[root@localhost ~]# ssh [email protected] ls -l /root/temp[email protected]'s password: total 4 -rw-r--r--. 1 root root 7 Apr  7  2021 test.txt # 查看一下本机的temp目录下的test.txt文件大小,本机的test.txt文件已修改,所以比远程系统里面的test.txt文件大 [root@localhost ~]# ll /root/temp/ total 4 -rw-r--r--. 1 root root 77 Apr  7 21:10 test.txt # 执行rsync -avzu同步一下 [root@localhost ~]# rsync -avzu [email protected]:/root/temp /root/ [email protected]'s password:
receiving incremental file list

sent 25 bytes  received 76 bytes  40.40 bytes/sec
total size is 7  speedup is 0.07

下面查看一下本机的/root/temp目录里面的test.txt是否被覆盖: 发现并没有被覆盖。

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

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

[root@localhost ~]# rsync -avz --progress /root/temp/ [email protected]:/root/temp

Linux中rsync备份数据使用实例Linux中rsync备份数据使用实例

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

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

# 查看一下源目录里面的文件
[root@localhost ~]# ll /root/temp/
total 0
-rw-r--r--. 1 root root 0 Apr  7 21:46 name.csv
# 查看一下目标目录里面的文件
[root@localhost ~]# ssh [email protected] ls -l /root/temp[email protected]'s password: total 944 drwxr-xr-x. 2 root root      6 Apr  7  2021 anaconda drwx------. 2 root root      6 Apr  7  2021 audit -rw-------. 1 root root      0 Apr  7  2021 btmp -rw-------. 1 root root      0 Apr  7  2021 btmp-20210406 drwxr-xr-x. 2 root root      6 Apr  7  2021 chrony -rw-------. 1 root root   8432 Apr  7  2021 cron -rw-------. 1 root root  12200 Apr  7  2021 cron-20210221 -rw-------. 1 root root  48130 Apr  7  2021 cron-20210228 -rw-------. 1 root root   3910 Apr  7  2021 cron-20210308 -rw-------. 1 root root  22455 Apr  7  2021 cron-20210406 -rw-------. 1 root root 383369 Apr  7  2021 dnf.librepo.log -rw-------. 1 root root 476949 Apr  7  2021 dnf.librepo.log-20210221 # rsync使用--delete选项删除目标目录中不包含源目录的文件 [root@localhost ~]# rsync -avz --delete /root/temp [email protected]:/root [email protected]'s password:
sending incremental file list
deleting temp/chrony/
deleting temp/audit/
deleting temp/anaconda/
deleting temp/dnf.librepo.log-20210221
deleting temp/dnf.librepo.log
deleting temp/cron-20210406
deleting temp/cron-20210308
deleting temp/cron-20210228
deleting temp/cron-20210221
deleting temp/cron
deleting temp/btmp-20210406
deleting temp/btmp
temp/
temp/name.csv

sent 123 bytes  received 281 bytes  161.60 bytes/sec
total size is 0  speedup is 0.00

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

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

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

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

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

实例九:不传输大文件

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

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

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

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

总结

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

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/220979.html<

(0)
运维的头像运维
上一篇2025-04-14 19:57
下一篇 2025-04-14 19:58

相关推荐

  • 个人主题怎么制作?

    制作个人主题是一个将个人风格、兴趣或专业领域转化为视觉化或结构化内容的过程,无论是用于个人博客、作品集、社交媒体账号还是品牌形象,核心都是围绕“个人特色”展开,以下从定位、内容规划、视觉设计、技术实现四个维度,详细拆解制作个人主题的完整流程,明确主题定位:找到个人特色的核心主题定位是所有工作的起点,需要先回答……

    2025-11-20
    0
  • 社群营销管理关键是什么?

    社群营销的核心在于通过建立有温度、有价值、有归属感的社群,实现用户留存、转化和品牌传播,其管理需贯穿“目标定位-内容运营-用户互动-数据驱动-风险控制”全流程,以下从五个维度展开详细说明:明确社群定位与目标社群管理的首要任务是精准定位,需明确社群的核心价值(如行业交流、产品使用指导、兴趣分享等)、目标用户画像……

    2025-11-20
    0
  • 香港公司网站备案需要什么材料?

    香港公司进行网站备案是一个涉及多部门协调、流程相对严谨的过程,尤其需兼顾中国内地与香港两地的监管要求,由于香港公司注册地与中国内地不同,其网站若主要服务内地用户或使用内地服务器,需根据服务器位置、网站内容性质等,选择对应的备案路径(如工信部ICP备案或公安备案),以下从备案主体资格、流程步骤、材料准备、注意事项……

    2025-11-20
    0
  • 如何企业上云推广

    企业上云已成为数字化转型的核心战略,但推广过程中需结合行业特性、企业痛点与市场需求,构建系统性、多维度的推广体系,以下从市场定位、策略设计、执行落地及效果优化四个维度,详细拆解企业上云推广的实践路径,精准定位:明确目标企业与核心价值企业上云并非“一刀切”的方案,需先锁定目标客户群体,提炼差异化价值主张,客户分层……

    2025-11-20
    0
  • PS设计搜索框的实用技巧有哪些?

    在PS中设计一个美观且功能性的搜索框需要结合创意构思、视觉设计和用户体验考量,以下从设计思路、制作步骤、细节优化及交互预览等方面详细说明,帮助打造符合需求的搜索框,设计前的规划明确使用场景:根据网站或APP的整体风格确定搜索框的调性,例如极简风适合细线条和纯色,科技感适合渐变和发光效果,电商类则可能需要突出搜索……

    2025-11-20
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注