Posted by: kezhong | December 1, 2010

rsync backup in daemon mode

rsync is the best choice to backup files and directories from one server to another. The daemon mode is one of the ways. In my environment, I had two servers, one worked as rsync server (192.168.6.5), and the other as client (192.168.6.6).

Server Configuration
# mkdir /etc/rsyncd
# touch /etc/rsyncd/rsyncd.conf
# touch /etc/rsyncd/rsyncd.motd
# touch /etc/rsyncd/rsyncd.secrets
# chmod 600 /etc/rsyncd/rsyncd.secrets 

Edit the /etc/rsyncd/rsyncd.conf file, and append the below in it.
pid file=/var/run/rsyncd.pid
port=873
address=192.168.6.5
uid=root
gid=root
use chroot=yes
read only=no 

# limit access to from allowed hosts only
hosts allow=192.168.6.1 192.168.6.6
hosts deny=* 

max connections=2
motd file=/etc/rsyncd/rsyncd.motd 

log format=%t%a%m%f%b
syslog facility=local3
timeout=300 

[backup]
path=/var/backup
list=yes
ignore errors
auth users=syncuser
secrets file=/etc/rsyncd/rsyncd.secrets
comment=backup for 192.168.6.6 

# vim /etc/rsyncd/rsyncd.secrets
syncuser:qwer1234 

Create init start up script named /etc/init.d/rsyncd

#!/bin/sh
#
# Start up script for rsync daemon
#
# chkconfig: 35 90 10
# description: Server data for sync to other server
# processname rsync 
#Source function library
. /etc/rc.d/init.d/functions 

prog=rsync
conf=/etc/rsyncd/rsyncd.conf 

case “$1” in
   start)
      echo -n “Starting $prog: ”
      daemon /usr/bin/$prog –daemon –config=$conf
      RETVAL=$?
      echo
      [ $RETVAL = 0 ] && touch /var/lock/subsys/$prog
      ;;
   stop)
      echo -n “Shutting down $prog: ”
      killproc -d 60 $prog
      RETVAL=$?
      echo
      [ $RETVAL = 0 ] && rm -f /var/lock/subsys/$prog
      ;;
   status)
      status $prog
      ;;
   restart)
      $0 stop
      $0 start
      ;;
   *)
      echo “Usage: $0 {start|stop|restart|status}”
      exit 1
esac
exit 0

# chmod 755 /etc/init.d/rsyncd

Make the service boot up automatically and start it
# chkconfig rsyncd on
# service rsyncd start 

In my server, I installed the APF package as firewall, so I need to modify the /etc/apf/allow_hosts.rules file, and append the following the two lines.
     # Rsync backup
     d=873:s=192.168.6.6

Restart the apf service.
# apf –r 

Client configuration
Edit the /usr/local/bin/backup.sh file, add the following.
#!/bin/sh
SERVER=”192.168.26.5:873″
USER=syncuser
SECRET=/etc/rsync/secrets
LOCALDIR=/var/backup
URL=”rsync://$USER@$SERVER/backup”
RSYNC=”rsync -azvqC –delete –delete-after “ 

$RSYNC –password-file=$SECRET $LOCALDIR/ $URL/
if [ $? -eq 0 ]; then
     logger -t backup ‘backup successfully’
     echo ‘done’
else
    logger -t backup ‘backup failed’
    echo ‘failed’
    echo ‘failed’ | mail -s ‘192.168.6.6 backup failed’ steven_lkz@yahoo.ca
    exit 1
fi 

# chmod 755 /usr/local/bin/backup.sh
# mkdir /etc/rsync
# touch /etc/rsync/secrets
# chmod 600 /etc/rsync/secrets
# vim /etc/rsync/secrets
qwer1234 

Test
Create the directory /var/backup on both the server and the client, copy something from anywhere to the directory on the client. Then execute /usr/local/bin/backup.sh to check if it works. 

If it works, you can create cron job as below.
1 23 * * * /usr/local/bin/backup.sh > /dev/null 2>&1 

Finally, thank Jeffye for helping me to finish it.


Responses

  1. Although I have not tested it yet but I am sure it should work for me. You have mentioned everything very clearly.

    I was looking for how to create rsyn process so that it doesnt prompt for password. I need it to keep copying the files from my production Linux server to the DR server. And on top of that I am not Linux expert .. I just do normal stuff with a few daily use commands.

    If I get any issues hope you wont say No to answer them :)

    Thanks for the lovely information!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: