1.创建自定义脚本
文中以inotify脚本为例
ubuntu@root# vi /home/test/***.sh
#!/bin/bash
host1=192.168.0.2 ##同步节点IP地址
src=/home/test ##同步目录
dst1=backup ##模块名称
user1=test ##用户
/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,delete,create,attrib $src \
| while read files
do
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsync.password $src $user1@$host1::$dst1 > /dev/null 2>&1
echo "${files} was rsynced." >> /tmp/rsync.log 2>&1
done
2.赋权
chmod 755 inotifyrsync.sh
3.创建service
ubuntu@root#vi /etc/systemd/system/inotifyrsync.service
[Unit]
Description=inotifyrsync Service
After=network.target
[Service]
Type=simple
##Environment=“PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin”
ExecStart=/home/inotifyrsync.sh
[Install]
WantedBy=multi-user.target
4.service赋权
chmod +x /etc/systemd/system/inotifyrsync.service
5.启动service,查看service状态,执行的脚本是否有进程
6.链接service使其开机启动
ubuntu@root#systemctl daemon-reload ##重加载systemctl配置
ubuntu@root#systemctl enable inotifyrsync.service