使用 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
用您的域名、主机名或 IP 地址替换“source.domain.com”。
使其可执行
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
这意味着在每天、每月和每周的 23 时 00 分运行此命令。使用以下方法保存您的 crontab
条目:
Shift+:+w+q+!
可选标志¶
-n : Dry-Run to see what files would be transferred
-v : list out all the files which 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