banner
ECSS11

Kita ☆ Kita ☆

试试 Xlog 做博客(并把好久之前写的老文章搬过来)之每年都要换一个网站,也不知道何时是个头。

Use rsync for scheduled data synchronization

First, we need to know the basic usage of rsync, which can be briefly understood through man rsync.

Local: rsync [options...] source... [destination]

Remote connection via SSH:

Pull: rsync [options...] [user@]address:source... [destination]

Push: rsync [options...] source... [user@]address:destination

Through rsync listening service:

Pull: 

  rsync [options...] [user@]address::source... [destination]
  rsync [options...] rsync://[user@]address[:port]/source... [destination]

Push: 

  rsync [options...] source... [user@]address::destination
  rsync [options...] source... rsync://[user@]address[:port]/destination

Providing only the SRC parameter without the DEST parameter will list the source files instead of copying.

Quick Start#

Using platform: ubuntu 20

How to achieve: By configuring the rsync listening service on the server side and actively pushing from the client side, and setting up a crontab scheduled task.

Configure rsync Listening#

On the remote server, which is the server where you want to save the synchronized files, execute the commands in sequence to check:

root@ubuntu:~# sudo apt update
root@ubuntu:~# sudo apt install rsync

Check if the rsync environment is installed.

root@ubuntu:~# service rsync status

View rsync service information.

● rsync.service - fast remote file copy program daemon
     Loaded: loaded (/lib/systemd/system/rsync.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2022-05-09 00:19:13 MSK; 29min ago
       Docs: man:rsync(1)
             man:rsyncd.conf(5)
   Main PID: 6216 (rsync)
      Tasks: 1 (limit: 2279)
     Memory: 13.0M
     CGroup: /system.slice/rsync.service
             └─6216 /usr/bin/rsync --daemon --no-detach

Use any editor or cat to open the rsync.service service file.

[Unit]
Description=fast remote file copy program daemon
Documentation=man:rsync(1) man:rsyncd.conf(5)
ConditionPathExists=/etc/rsyncd.conf
After=network.target

[Service]
ExecStart=/usr/bin/rsync --daemon --no-detach

[Install]
WantedBy=multi-user.target

Through ConditionPathExists=/etc/rsyncd.conf, we can know that rsync reads the configuration file rsyncd.conf.

root@ubuntu:~# vim /etc/rsyncd.conf

Use any editor to open the configuration file; if it does not exist, create one. Don't ask how to create it; it's recommended to search it yourself.

Configuration Options#

[custom1]
pid file = /var/run/rsyncd1.pid
...

[custom2]
pid file = /var/run/rsyncd2.pid
...

[custom3]
pid file = /var/run/rsyncd3.pid
...

Must: Create a custom module, which will be used when connecting later.

uid = user1
gid = group1

Must: The user for the rsync module; root is not recommended. It is suggested to create one using adduser --disabled-login [username].

path = /path/to/backup
use chroot = yes

Must: Use path to define the path of this module and use use chroot to enhance security.

read only = no
write only = no

Optional: Whether to enable reading or writing, configurable according to needs.

hosts allow = 154.53.59.237

# Maximum connections
max connections = 4

# Enable transfer logging
transfer logging = yes

# Specify log format
log format = %t %a %m %f %b

list = no

Additional: Restrict IP access through hosts allow, and hide the used modules with list.

Client Connection#

On the client side, also download rsync and use the following command to test if the configuration is successful.

root@vmi819506:~# rsync -avzrn /tmp/test/ rsync://[user]@[address][:port]/[module]

Troubleshooting#

Q1: Unable to connect to the server.
A1: The server's port may not be open; rsync listens on port 873 by default.

Set Scheduled Tasks#

Use crontab to run automatically after the time is up. Here is an example (run rsync every 5 minutes):

5 * * * * [command]

Summary#

This article ends here, and there may be some additions or modifications to the content later.

Writing rsync configuration always feels not elegant enough, and there is no md highlighting support, sigh.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.