Systemd Rsync file timing synchronization

1. Environment

  • Operating system: CentOS 7

Host:

  • master 192.168.0.98
  • backup 192.168.0.166

Goal: Incrementally copy from online to online at 3 am every day .master/wwwbackup

2. Configure the password-free login of the host

Configure the secret-free login of the host on Backup, please refer to SSH public key secret-free login[1]

3. Rsync service

Install Rsync on both master and slave

yum install -y rsync

Note that the following steps only need to be configured on the slave backup, not the master.

Create a file synchronization service on the slave Backup

vi /lib/systemd/system/filesync.service

Fill in the following content, pay attention to modify the host IP part of the source address of rsync synchronization

[Unit]
Description=rsync file sync service.

[Service]
User=root
Type=oneshot
ExecStart=/usr/bin/bash -c 'rsync -av --delete [email protected]:/www/ /www'


[Install]
WantedBy=multi-user.target

Note that please complete the configuration of the slave backup to log in to the master without password first, otherwise the service will be invalid.

reload service

systemctl daemon-reload

The above service implements: incremental replication from masterup to up./wwwbackup

4. Timer

Note that this step only needs to be configured on the slave backup, not the master.

In order to achieve: every day at 3 am, a timer needs to be created.

The timer service of Systemd is used here

Create the following file, the file name can be customized, and the suffix is ​​fixed as.timer

vi /lib/systemd/system/filesync.timer

Fill in the following content

[Unit]
Description=filesync.service timer.

[Timer]
Unit=filesync.service
OnCalendar=*-*-* 03:00:00

[Install]
WantedBy=multi-user.target
  • [Timer]The value in Unitneeds to be the same as the name of the service file created in the previous step.
  • OnCalendarFor parameter configuration, please refer to freedesktop. systemd.timer.html

reload service

systemctl daemon-reload

Set the timer to start automatically

systemctl enable filesync.timer

start timer

systemctl start filesync.timer

View the currently running timer and check whether the timer is valid

systemctl list-timers

insert image description here

5. Check the file synchronization log

When the timer expires filesync.service, the service will be automatically started. After the service is running, you can view the file synchronization information through the following command:

journalctl -f -u filesync.service

insert image description here

references

[1]. csdn. SSH public key password-free login. cliven. 2022.10.13. https://blog.csdn.net/q1009020096/article/details/125166499
[2]. Ruan Yifeng Blog. rsync usage tutorial. Ruan Yifeng . 2020.8.26. https://www.ruanyifeng.com/blog/2020/08/rsync.html
[3]. Ruan Yifeng Blog. Systemd Timer Tutorial. Ruan Yifeng. 2018.3.30 . https://www. ruanyifeng.com/blog/2018/03/systemd-timer.html
[4]. freedesktop. systemd.timer. https://www.freedesktop.org/software/systemd/man/systemd.timer.html#

Guess you like

Origin blog.csdn.net/q1009020096/article/details/127904407