使用 rsync 进行同步
先决条件¶
本指南涵盖了您理解和学习所需的所有内容
- 一台运行 Rocky Linux 的计算机
- 熟悉从命令行修改配置文件
- 了解如何使用命令行编辑器(这里以 vi 为例,但您可以使用您喜欢的编辑器)
- 您需要 root 访问权限或
sudo
权限 - 公钥和私钥 SSH 密钥对
- 能够使用
vi
或您喜欢的编辑器创建 bash 脚本并进行测试。 - 能够使用
crontab
来自动化脚本的运行
简介¶
通过 SSH 使用 rsync
的功能不如 lsyncd(它允许您监视目录或文件变化并实时保持同步),也不如 rsnapshot(它提供从单台计算机备份多个目标的能力)灵活。然而,它允许您按照您定义的计划保持两台计算机的最新状态。
如果您需要使目标计算机上的一组目录保持最新,并且您不关心实时同步作为一项功能,那么通过 SSH 使用 rsync
可能是最佳解决方案。
在此过程中,您将以 root 用户身份工作。要么以 root 用户登录,要么使用 sudo -s
命令在终端中切换到 root 用户。
安装 rsync
¶
虽然 rsync
可能已经安装。为了确保 rsync
是最新版本,请在两台计算机上执行以下操作
dnf install rsync
如果软件包未安装,dnf
会要求您确认安装。如果已安装,dnf
会查找更新并提示您进行安装。
准备环境¶
本示例将使用目标计算机上的 rsync
从源拉取,而不是从源推送到目标。您需要为此设置一个 SSH 密钥对。创建 SSH 密钥对后,验证从目标计算机到源计算机的无密码访问。
rsync 参数和脚本设置¶
在脚本设置继续之前,您必须决定要与 rsync
一起使用的参数。存在许多可能性。查阅 rsync 手册 以获取完整列表。最常用的 rsync
使用方式是使用 -a
选项,因为 -a
,或“归档”,结合了几个常用选项。-a
包含哪些内容?
-r
,递归处理目录-l
,将符号链接保持为符号链接-p
,保留权限-t
,保留修改时间-g
,保留组-o
,保留所有者-D
,保留设备文件
在此示例中,您需要指定的其他选项是
-e
,指定要使用的远程 shell--delete
,表示如果目标目录中存在源计算机上不存在的文件,则将其删除
接下来,在目标计算机上通过创建一个文件来设置脚本(同样,如果您不熟悉 vi
,请使用您喜欢的编辑器)。要创建文件,请使用此命令
vi /usr/local/sbin/rsync_dirs
添加内容
#!/usr/bin/env bash
/usr/bin/rsync -ae ssh --delete root@source.domain.com:/home/your_user /home
将“source.domain.com”替换为您的域名、主机名或 IP 地址。
使其可执行
chmod +x /usr/local/sbin/rsync_dirs
测试¶
脚本编写确保您可以无忧地进行测试。
警告
在这种情况下,假定您的主目录在目标计算机上不存在。如果它存在,您可能想在执行脚本之前备份它!
运行脚本
/usr/local/sbin/rsync_dirs
如果一切正常,您将在目标计算机上获得一个同步的主目录副本。请确保是这种情况。
假设所有这些都有效,请在源计算机的主目录中创建一个新文件
touch /home/your_user/testfile.txt
重新运行脚本
/usr/local/sbin/rsync_dirs
验证目标计算机是否收到了新文件。如果是,下一步是验证删除过程。删除您刚刚在源计算机上创建的文件
rm -f /home/your_user/testfile.txt
重新运行脚本
/usr/local/sbin/rsync_dirs
验证该文件是否已从目标计算机中删除。
最后,在目标计算机上创建一个源计算机上不存在的文件
touch /home/your_user/a_different_file.txt
最后一次运行脚本
/usr/local/sbin/rsync_dirs
您在目标计算机上创建的文件应该不再存在,因为它在源计算机上也不存在。
假设所有这些都按预期工作,请更改脚本以同步您所有期望的目录。
自动化所有流程¶
您可能不想每次都手动运行此脚本进行同步。可以使用 crontab
按计划自动执行此操作。要每晚 11 点运行此脚本
crontab -e
这将打开一个类似以下的界面
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
信息
示例 crontab
显示了一个空的但被注释掉的文件。注释在每个计算机实例上都不会显示,并且可能是一个空文件。在活动的计算机上,您可能会看到其他条目。
crontab
使用 24 小时制。您需要在文件末尾添加您的条目
00 23 * * * /usr/local/sbin/rsync_dirs
这表示每天、每月、每周的 00 分和 23 点运行此命令。使用以下命令保存您的 crontab
条目
Shift+:+w+q+!
可选参数¶
-n: Dry-Run to see what files would be transferred
-v: list out all the files that are being transferred
-vvv: to provide debug info while transferring files
-z: to enable compression during the transfer
结论¶
尽管 rsync
的灵活性和健壮性不如其他工具,但它提供了文件同步功能,这始终是有益的。
作者:Steven Spencer
贡献者:Ezequiel Bruni, tianci li, Ganna Zhyrnova