[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: remote backup



Thank you very much,
Jerome

Tobias Kraus wrote:
[...]

I was planning to do something similar:
backing up my laptop with `backup2l' and synchronizing the related files.

Yes, please send your shell script.

@ML:
I turned of automatic line break for this mail. Please don't complain about lines longer than 80 characters.
Thanks.

Thanks for inspiration to:
http://www.heinlein-support.de/web/projekte/rsync-backup/


Hi Jerome,
on server-side (desktop) I'm running a rsync-server (see rsync(1), rsyncd.conf(5)) On client-side (notebook) every hour I run the rsync-backup script. It is configured
with 5 environment variables (see below). If more than $RSYNC_TIME seconds have passed
after last backup, it checks whether rsync-server is reachable. If it is, it calls
the rsync_func() bash function to do the actual backup.


On server-side, the rsync-rotate script is called hourly by cron.
If no rsync-process is running and the creation time of the lockfile of $ROTATE_DIR
is younger than $ROTATE_DIR.0 it locks the rsync-port using iptables (if you don't
know how iptables works, drop me a line and I'll give you a _short_ introduction
- I don't have very much spare time now :-( ). After that it deletes the oldest backup and rotates all others (8->9, 7->8, and so on).
After that, it makes a backup of $ROTATE_DIR using hard
links. So unchanged files don't use any hard disk space, except inodes for the hard
links. Finally it copies "$ROTATE_DIR" to "$ROTATE_DIR"- (if an important file should get
corrupted - remember: if a file did not change for the last 10 rsync-rotate runs, or so
(see $NUM_BACKUPS), there is exactly one copy of the file on the hdd!)

BTW, sorry for my bad english.

Tobias



/usr/local/sbin/rsync-backup: (client = notebook)
----- 8X ------------------------------------------------------------------------------------
#!/bin/bash
#DNS-name/IP-address of rsync-server
RSYNC_SRV="server"
#rsync-share to backup to
RSYNC_SERVICE="system"
#lockfile, also as indicator when last backup was successful
RSYNC_LOCK="/var/local/rsync-backup.lock"
#logfile
RSYNC_LOG="/var/local/rsync-backup.log"
#minium time (seconds) which have to pass for anoter backup
RSYNC_TIME=$((60*60*24*1))

rsync_func () {
  echo "Backing up $1" >>$RSYNC_LOG
  nice -19 rsync -ax --numeric-ids --delete $2 $1 rsync://"$RSYNC_SRV"/"$RSYNC_SERVICE" >> $RSYNC_LOG 2>&1

  if ! [ $? = 24 -o $? = 0 ] ; then
        rm "$RSYNC_LOCK";
        echo "Error at $1. EXIT!" >>$RSYNC_LOG;

  fi
}


ps -e |grep -v grep |grep -v $$ |grep `basename $0` >/dev/null 2>&1 && exit 0

echo >>$RSYNC_LOG
date +"%s  =   %c" >>$RSYNC_LOG


[ -f "$RSYNC_LOCK" ] && [ $(( `date +%s` - `date -r "$RSYNC_LOCK" +%s` )) -lt $RSYNC_TIME ] && { echo "No backup. End of program!" $RSYNC_LOG ; exit 0 ; }
rsync rsync://$RSYNC_SRV >/dev/null 2>&1 || { echo "RSYNC-Server not available. End of program!" >>$RSYNC_LOG ; exit 0 ; }

rm -f "$RSYNC_LOCK"
#create lockfile, creation time shows last backup
touch "$RSYNC_LOCK"


#do actual backup
rsync_func /home
rsync_func /usr
rsync_func / "--exclude /home --exclude /usr"

echo "End of program!">>$RSYNC_LOG
----- 8X ------------------------------------------------------------------------------------





/usr/local/sbin/rsync-rotate: (server = desktop)
----- 8X ------------------------------------------------------------------------------------
#!/bin/bash
#iptables-chain used to block rsync port
IPTBL_CHAIN="rsync"
#name or # of port rsync runs on
RSYNC_PORT="rsync"
#iptables executable
IPTABLES="/sbin/iptables"
#rotate which directory. must be the directory rsync shares on the network
ROTATE_DIR="/pub/backup/system"
#lockfile
LOCKFILE="var/local/rsync-backup.lock"
#number of copies stored
NUM_BACKUPS=9

for i in `pidof -x $0`; do
        [ x$i != x$$ ] && exit
done

[ `date -r "$ROTATE_DIR"/"$LOCKFILE" +%s` -gt `date -r "$ROTATE_DIR".0/"$LOCKFILE" +%s` ] || exit 0
[ -d "$ROTATE_DIR" ] || exit 0
[ -x "$IPTABLES" ] || exit 0
[ $NUM_BACKUPS -lt 1 ] && NUM_BACKUPS=1
[ $NUM_BACKUPS -gt 10 ] && NUM_BACKUPS=10

$IPTABLES -L "$IPTBL_CHAIN" >/dev/null 2>&1 ||
        $IPTABLES -N "$IPTBL_CHAIN"
$IPTABLES -L INPUT |grep "$IPTBL_CHAIN.*$RSYNC_PORT" >/dev/null 2>&1 ||
        $IPTABLES -I INPUT -p tcp --dport "$RSYNC_PORT" -j "$IPTBL_CHAIN"
$IPTABLES -L OUTPUT |grep "$IPTBL_CHAIN.*$RSYNC_PORT" >/dev/null 2>&1 ||
        $IPTABLES -I OUTPUT -p tcp --sport "$RSYNC_PORT" -j "$IPTBL_CHAIN"

netstat -t |grep ":$RSYNC_PORT.*ESTABLISHED" >/dev/null 2>&1 && exit 0

$IPTABLES -A "$IPTBL_CHAIN" -j REJECT



if [ -d "$ROTATE_DIR".$NUM_BACKUPS ] ; then
        NUM_BACKUPS=$(($NUM_BACKUPS  - 1))
        rm -rf "$ROTATE_DIR".$NUM_BACKUPS
fi

for OLD in 8 7 6 5 4 3 2 1 0 ; do
        if [ -d "$ROTATE_DIR".$OLD ] ; then
                        NEW=$[ $OLD + 1 ]
                        [ -d "$ROTATE_DIR".$NEW ] && rm -rf "$ROTATE_DIR".$NEW
                        mv "$ROTATE_DIR".$OLD "$ROTATE_DIR".$NEW
        fi
done

if [ -d "$ROTATE_DIR" ] ; then
        cp -al "$ROTATE_DIR" "$ROTATE_DIR".0
        [ -d "$ROTATE_DIR"~ ] && rm -r "$ROTATE_DIR"~
        cp -a "$ROTATE_DIR" "$ROTATE_DIR"~
fi



$IPTABLES -F "$IPTBL_CHAIN"

----- 8X ------------------------------------------------------------------------------------






Reply to: