background

The company has a requirement to periodically increment the files of the server CentOS to the Windows server. The Windows server is connected to the disk array of the storage server and has large space. Based on this requirement, we adopted the inotify+ Rsync incremental backup solution.

Server Configuration

The IP address system
192.168.1.100 CentOS7.x
192.168.1.101 Windows Server 2012 r2

Windows

1. Install cwRsyncServer

  • Download address:www.backupassist.com/rsync/
  • Double click on the install
  • Set the service name and password

Note: The server name and password are used in the later configuration project. The default user name is SvcCWRSYNC and the password is set to admin123

  • The configuration file
use chroot = false
strict modes = false
hosts allow = *
log file = rsyncd.log
uid = 0 You need to configure this item, otherwise the connection error is reported
gid = 0 You need to configure this item, otherwise the connection error is reported

# Module definitions
# Remember cygwin naming conventions : c:\work becomes /cygwin/c/work
#
#[test]
#path = /cygdrive/c/work
#read only = false
#transfer logging = yes[rsyncdata] path = / cygdrive/e/cdbid - pro1.0 - backup / 57read only = false The read-only property is false
list = no
hosts allow = *
auth users = SvcCWRSYNC # Configure the user nameSecrets file = / cygdrive/e/cdbid pro1.0 - backup/rsync passwdCopy the code
  • Create the rsync.passwd file and fill in the following information
SvcCWRSYNC:admin123
Copy the code
  • Add directory cdbid-pro1.0-backup directory access permission, if you do not have SvcCWRSYNC user, click Advanced Add
  • Start the service

Linux

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

admin123
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 des=rsyncdata user=SvcCWRSYNC 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/test[email protected]: : rsyncdataCopy 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 supports regular incremental backup of CentOS files to Windows