wok-6.x rev 5649

mirror-tools: add backup.sh
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat May 29 13:48:15 2010 +0200 (2010-05-29)
parents 2c7074daece0
children e67ccc51f7c0
files mirror-tools/receipt mirror-tools/stuff/home/backups/mirror/backup.sh
line diff
     1.1 --- a/mirror-tools/receipt	Sat May 29 13:32:42 2010 +0200
     1.2 +++ b/mirror-tools/receipt	Sat May 29 13:48:15 2010 +0200
     1.3 @@ -6,7 +6,7 @@
     1.4  SHORT_DESC="Mirrors toolset."
     1.5  MAINTAINER="pascal.bellard@slitaz.org"
     1.6  WEB_SITE="http://www.slitaz.org/"
     1.7 -DEPENDS="rrdtool"
     1.8 +DEPENDS="rrdtool rgzip"
     1.9  
    1.10  # Rules to gen a SliTaz package suitable for Tazpkg.
    1.11  genpkg_rules()
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/mirror-tools/stuff/home/backups/mirror/backup.sh	Sat May 29 13:48:15 2010 +0200
     2.3 @@ -0,0 +1,67 @@
     2.4 +#!/bin/sh
     2.5 +
     2.6 +KEEP=1
     2.7 +EXTRA="monthly:30:2"
     2.8 +BACKUP_USER=bellard
     2.9 +REMOTE_USER=bellard
    2.10 +
    2.11 +backup_data()
    2.12 +{
    2.13 +while read file dirs; do
    2.14 +	find $dirs  | cpio -o -H newc | rgzip -9 > $file.cpio.gz 2> /dev/null
    2.15 +done <<EOT
    2.16 +etc		/etc /home/$BACKUP_USER/.ssh
    2.17 +www		/var/www/mirror-info /var/www/pizza
    2.18 +packages	/var/lib/tazpkg/installed
    2.19 +rrd		/var/spool/rrd
    2.20 +crontabs	/var/spool/cron/crontabs
    2.21 +awstats		/var/lib/awstats
    2.22 +EOT
    2.23 +}
    2.24 +
    2.25 +#
    2.26 +# The following should be kept untouched.
    2.27 +#
    2.28 +
    2.29 +SSH="ssh -i /home/$BACKUP_USER/.ssh/id_rsa"
    2.30 +
    2.31 +cd $(dirname $0)
    2.32 +[ $(id -u) == 0 ] || exit 1
    2.33 +[ $(hostname) == $(basename $PWD) ] || exit 1
    2.34 +
    2.35 +rotate()
    2.36 +{
    2.37 +	local i
    2.38 +	local j
    2.39 +	for j in $(seq $(($1 - 1)) -1 1); do
    2.40 +		for i in *.$2.$(($j - 1)) ; do mv -f $i ${i%.$2.*}.$2.$j; done
    2.41 +	done
    2.42 +}
    2.43 +
    2.44 +[ -n "$EXTRA" ] && for x in $EXTRA ; do
    2.45 +	IFS=':' ; set -- $x ; unset IFS
    2.46 +	suffix=$1
    2.47 +	days=$2
    2.48 +	keep=$3
    2.49 +	for i in *.gz ; do
    2.50 +		mtime=$(( $(stat -c %Y $i) - ($days * 24 * 3600) ))
    2.51 +		j=$i.$suffix.0
    2.52 +		[ -e $j ] && [ $(stat -c %Y $j) -gt $mtime ] && continue
    2.53 +		rotate $keep gz.$suffix
    2.54 +		ln $i $j
    2.55 +	done
    2.56 +done
    2.57 +if [ 0$KEEP -gt 0 ]; then
    2.58 +	[ $KEEP -gt 1 ] && rotate $KEEP gz
    2.59 +	for i in *.gz ; do mv -f $i $i.0; done
    2.60 +fi
    2.61 +
    2.62 +backup_data
    2.63 +
    2.64 +chown $BACKUP_USER *
    2.65 +chmod 700 *
    2.66 +
    2.67 +for i in $(cd .. ; ls); do
    2.68 +	[ $i == $(hostname) ] && continue
    2.69 +	rsync -aH -e "$SSH" --bwlimit=50 $REMOTE_USER@$i.slitaz.org:/home/backups/$i/. ../$i/.
    2.70 +done