background
Inotify +rsync supports incremental backup of server CentOS files to Windows on a regular basis
Server Configuration
The IP address | system | function |
---|---|---|
192.168.1.100 | CentOS7.x | Rsync server |
192.168.1.101 | CentOS7.x | Rsync client |
Rsync server
1. Install rsync
yum install -y rsync
Copy the code
2. Configure the/etc/rsyncd. Conf
transfer logging = yes log file = /var/log/rsyncd.log pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock uid = nobody gid = nobody use chroot = no ignore errors read only = no [web1] path = /root/test auth users = myuser secrets file = /etc/rsyncd.secrets host allow = * hosts deny = * list = falseCopy the code
3. New/etc/rsyncd. Secrets
echo "myuser:mypassword" > /etc/rsyncd.secrets
chmod +x /etc/rsyncd.secrets
Copy the code
4. Start the service
systemctl restart rsyncd
Copy the code
Rsync client
1. Install rsync
yum install rsync -y
Copy the code
2. Create /etc/rsync.passwd with the following content. Note that the rsync client requires only the password
mypassword
Copy the code
3. Change the permission
chmod 600 /etc/rsync.passwd
Copy the code
4. Installation way
The inotify-tools tool monitors file additions, deletions, and modifications while synchronizing them to the backup server on Windows
yum install inotify-tools -y
Copy the code
5. Start the inotify_start.sh script
#! /bin/bashHost =192.168.1.101 SRC =/home/backup des=web1 user=myuser inotifywait-mrq --timefmt'%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src | while read files
do
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsync.passwd $src $user@$host: :$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
Copy the code
Test 6.
# test command
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsync.passwd /root/testmyuser@192.168.1.101: : web1Copy the code
7. Run the startup script in the background
inotify_start.sh &
Copy the code
Service startup script
1. CentOS7
- Copy the inotifyd.service file to /usr/lib/systemd/system/
- Start the service: systemctl start inotifyd.service
- Automatic startup: systemctl enable inotfyd.service
- Run the systemctl daemon-reload command
[Unit]
Description=inotify rsync script
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=root
Group=root
PIDFile=/var/run/inotify.pid
ExecStart=/usr/bin/nohup /root/inotify_start.sh > /root/inotify.log 2>&1 &
ExecReload=
ExecStop=/usr/bin/ps -ef | /usr/bin/grep inotify | /usr/bin/grep -v grep | /usr/bin/awk '{print $2}' | /usr/bin/xargs /usr/bin/kill -9
PrivateTmp=true
[Install]
WantedBy=multi-user.target
Copy the code
2. CentOS6
- Copy inotifyd to /etc/init.d/
- To start the service: /etc/init.d/inotifyd start
- Automatic startup: chkconfig inotifyd on
#! /bin/sh
# chkconfig: - 91 35
# description: inotifyd
# Source function library.
if [ -f /etc/init.d/functions];then
. /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions];then
. /etc/rc.d/init.d/functions
else
exit 1
fi
# Avoid using root's TMPDIR
unset TMPDIR
# Source networking configuration.
. /etc/sysconfig/network
EXEC_FILE="/root/inotify_start.sh"
LOCK_FILE="/var/lock/subsys/inotifyd"
OPTIONS="> /root/inotify.log 2>&1 &"
RETVAL=0
start() {
echo -n $"Starting $(basename $EXEC_FILE) services: "
# daemon $EXEC_FILE $OPTIONS
daemon $EXEC_FILE $OPTIONS
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch $LOCK_FILE || \
RETVAL=1
return $RETVAL
}
stop() {
echo -n $"Shutting down $(basename $EXEC_FILE) services: "
# killproc $(basename $EXEC_FILE)
ps -ef | grep inotify | grep -v grep | awk '{print $2}' | xargs kill -9
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $LOCK_FILE
return $RETVAL
}
restart() {
stop
start
}
reload() {
stop
start
}
rhstatus() {
status $(basename $EXEC_FILE)
RETVAL=$?
if [ $RETVAL -ne0];then
return $RETVAL
fi
}
# Allow status as non-root.
if [ "The $1" = status ]; then
rhstatus
exit $?
fi
# Check that we can write to it... so non-root users stop here
# [ -w $CONF_FLIE ] || exit 4
case "The $1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload)
reload
;;
status)
rhstatus
;;
condrestart)
[ -f $LOCK_FILE ] && restart || :
;;
*)
echo $"Usage: $0 {start|stop|restart|reload|status|condrestart}"
exit 2
esac
exit $?
Copy the code
Inotify +rsync backup server CentOS files periodically incremental to another CentOS