menu Chancel's blog
rss_feed
Chancel's blog
有善始者实繁,能克终者盖寡。

Rsync增量同步文件

作者:Chancel Yang, 创建:2021-07-01, 字数:2120, 已阅:69, 最后更新:2024-03-10

假设有两台机器,分别如下

  • A机器(服务器):10.0.0.100
  • B机器(备份):192.168.11.11

备份步骤如下

  1. 开启B机器的Rsync服务
  2. 更改B机器的Rsync配置文件(/etc/rsyncd.conf)
  3. 在A机器上添加B机器的配置信息,尝试同步文件

首先检查B机器的Rsync服务情况,一般是默认开启的

Bash
➜  ~ sudo systemctl status rsync
● rsync.service - fast remote file copy program daemon
   Loaded: loaded (/lib/systemd/system/rsync.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2021-06-30 16:15:49 UTC; 14h ago
 Main PID: 8218 (rsync)
    Tasks: 1 (limit: 4655)
   CGroup: /system.slice/rsync.service
           └─8218 /usr/bin/rsync --daemon --no-detach

Jun 30 16:15:49 ubuntu1804-server systemd[1]: Started fast remote file copy program daemon.

可以看到B机器的Rsync正常运行中,接下来编辑(或添加)Rsync的配置文件

Bash
sudo vim /etc/rsyncd.conf

# /etc/rsyncd: configuration file for rsync daemon mode
# See rsyncd.conf man page for more options.
# configuration example:
uid = root
gid = root
use chroot = no
max connections = 10
strict modes = no
port = 41000
# pid文件,如无法再次启动可删除
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/run/rsyncd.log

[rsync-backup]
path = /mnt/sdb/rsync-server
comment = rsync backup
ignore errors
read only = no
write only = no
hosts allow = 10.0.0.0/24
list = false
uid = chancel
gid = chancel
auth users = rsync
# 用户认证配置文件
secrets file = /etc/rsync-user.pass

创建一个配置文件中的 用户认证配置文件(/etc/rsync-user.pass)

Bash
sudo vim /etc/rsync-user.pass

user:password

注意rsync-user.pass文件为400权限,最后重启下B机器的Rsync服务即可

Bash
sudo chmod 400 /etc/rsync-user.pass
sudo systemctl restart rsync.service

接下来我们登录A机器,同样创建 /etc/rsync-user.pass 文件,存入密码即可

Bash
password

还是需要注意rsync-user.pass文件的权限为400

Bash
sudo chmod 400 rsync-user.pass

接下来就可以尝试进行同步

Bash
rsync -vzrtp --delete --progress --password-file=/etc/rsync_user.pass --port=41000 /srv rsync@192.168.11.11::rsync-backup/A

参数解释

  • --vzrtp:同步的参数,详细可以参考rsync - man
  • --delete:同步删除
  • --password-file:密码文件,与B机器中/etc/rsync_user.pass
  • --port:B机器配置文件中的端口
  • /srv:备份的目录
  • rsync@192.168.11.11:B机器的Rsync用户名与地址
  • rsync-backup/A:配置文件节点名称(将采用这个节点的配置),A则是次级文件夹

[[replyMessage== null?"发表评论":"发表评论 @ " + replyMessage.m_author]]

account_circle
email
web_asset
textsms

评论列表([[messageResponse.total]])

还没有可以显示的留言...
[[messageItem.m_author]] [[messageItem.m_author]]
[[messageItem.create_time]]
[[getEnviron(messageItem.m_environ)]]
[[subMessage.m_author]] [[subMessage.m_author]] @ [[subMessage.parent_message.m_author]] [[subMessage.parent_message.m_author]]
[[subMessage.create_time]]
[[getEnviron(messageItem.m_environ)]]