--- /dev/null
+#!/bin/sh
+
+source /etc/backup-helper
+
+DATE=`date +%Y%m%d-%H%M`
+HOST=`hostname -s`
+
+ps -C postmaster 2>&1 >/dev/null
+if [ $? -eq 0 ]; then
+ backup_all_dbs /mnt/backup $HOST $DATE
+fi
+
+# thierry : tmp backup solution on gorillaz
+# note that in addition to the db dumps in /mnt/backup, this also keeps a backup of
+# (*) /etc/ and in particular /etc/planetlab + all other crontabs and backup-helper
+# (*) /var/www/html for the drupal themes and other stuff
+# (*) /root with various tweaks in particular in /root/thierry
+# in general this is not intended to be restored as-is (as it might overwrite too much stuff)
+# but can be helpful so, just in case ...
+
+# baris: create archive files in /mnt/backup for /etc /var/www/html and /root respectively and sync only /mnt/backup
+# bicudo: added 'cd /' to stop with crontab huge mails
+for d in etc var/www/html root
+do
+ cd /
+ dir_name=`echo ${d} | sed 's/\//_/g'`_
+ tar cjf /mnt/backup/${HOST}_${dir_name}${DATE}.tar.bz2 $d
+done
+
+# baris: copy all files from today to the "latest" directory.
+BACKUP=/mnt/backup
+LATESTDATE=`date +%Y%m%d`
+LATEST=/mnt/backup/latest
+TMP=${LATEST}.temp
+mkdir -p ${TMP} && cp -r $BACKUP/*${LATESTDATE}*.bak $TMP && rm -rf $LATEST && mv $TMP $LATEST
+
+for d in etc var/www/html root
+do
+ mkdir -p $LATEST/$d
+ rsync --archive --verbose --relative --delete /$d/ $LATEST/$d/
+done
+
+rsync --archive --verbose --relative --delete /mnt/backup/ --exclude '*~' root@gorillaz:/backup-ple-www
--- /dev/null
+#!/bin/sh
+
+# Goal: backup a directory
+# $1: Destination
+# $2: Server name (i.e.: onelab2)
+# $3: Backup name (i.e.: twiki)
+# $4: Date
+# $5: Directory to backup
+function backup_dir {
+ DEST=$1
+ BACKUP_NAME=$1/$2-$3-$4.tar.bz2
+ DIR=$5
+ mkdir -p $DEST
+ if [ ! -d $DIR ]; then
+ echo " (No such directory) [KO]"
+ return 1
+ fi
+ cd $DIR
+ echo -n "Backup directory $DIR..."
+ tar cfjp $BACKUP_NAME *
+ RES=$?
+ echo -n " (`stat --printf="%s" $BACKUP_NAME`)"
+ if [ $RES -eq 0 ]; then
+ echo " [ OK ]"
+ else
+ echo " [ KO ]"
+ fi
+
+ return 0
+}
+
+# Goal: backup a PostgreSQL database
+# $1: Destination
+# $2: Server name (i.e.: onelab2)
+# $3: Date
+# $4: Database name
+# $5: Database owner
+function backup_db {
+ DEST=$1
+ DBNAME=$4
+ DBUSER=$5
+ BACKUP_NAME=$DEST/$2-db-$DBNAME-$3.bak
+ mkdir -p $DEST
+ echo -n "Backup database $DBNAME..."
+ pg_dump -i -U postgres --user=$DBUSER -F c -f $BACKUP_NAME $DBNAME
+ RES=$?
+ echo -n " (`stat --printf="%s" $BACKUP_NAME`)"
+ if [ $RES -eq 0 ]; then
+ echo " [ OK ]"
+ else
+ echo " [ KO ]"
+ return 1
+ fi
+
+ return 0
+}
+
+# Goal: backup all PostgreSQL databases on the machine
+# $1: Destination
+# $2: Server name (i.e.: onelab2)
+# $3: Date
+function backup_all_dbs {
+ for i in `psql -qtAF '|' -U postgres -l`; do
+ DBNAME=`echo $i | cut -d '|' -f 1`
+ DBUSER=`echo $i | cut -d '|' -f 2`
+ if [ $DBNAME != template0 -a $DBNAME != template1 ]; then
+ backup_db $1 $2 $3 $DBNAME $DBUSER
+ fi
+ done
+}
+
+# Goal: backup a database in a VServer
+# $1: Destination
+# $2: VServer name (i.e.: onelab2)
+# $3: Backup name (i.e.: twiki)
+# $4: Date
+# $5: Database name
+# $6: Database owner
+function backup_vserver_db {
+ DEST=$1
+ VSERVER=$2
+ BACKUP_NAME=$1/$2-$3-$4.bak
+ DBNAME=$5
+ DBUSER=$6
+ mkdir -p $DEST
+ mkdir -p /vservers/$VSERVER/$DEST
+ echo -n "Backup database $DBNAME..."
+ vserver $VSERVER exec \
+ pg_dump -i -U postgres --user=$DBUSER -F c -v -f $BACKUP_NAME \
+ -- $DBNAME >/dev/null
+ RES=$?
+ mv /vservers/$VSERVER/$BACKUP_NAME $BACKUP_NAME
+ echo -n " (`stat --printf="%s" $BACKUP_NAME`)"
+ if [ $RES -eq 0 ]; then
+ echo " [ OK ]"
+ else
+ echo " [ KO ]"
+ return 1
+ fi
+
+ return 0
+}
+
+# Goal: backup an offline vserver instance
+# $1: Destination (where to save the backup)
+# $2: VServer name (i.e.: plc)
+# $3: Date
+function backup_vserver_offline {
+ DEST=$1
+ VSERVER=$2
+ BACKUP_NAME=$1/$2-vs-$3.off.tar.bz2
+ BACKUP_CONF_NAME=$1/$2-vsconf-$3.off.tar.bz2
+ mkdir -p $DEST
+ echo -n "Backup vserver $VSERVER..."
+ if [ ! -d /etc/vservers/$VSERVER ]; then
+ echo " (No such vserver) [ KO ]"
+ return 1
+ fi
+
+ cd /etc/vservers/$VSERVER &&
+ tar cfjp $BACKUP_CONF_NAME * 2>&1 >/dev/null &&
+ cd /vserver/$VSERVER &&
+ tar cfjp $BACKUP_NAME * 2>&1 >/dev/null
+ RES=$?
+
+ echo -n " (`stat --printf="%s" $BACKUP_NAME`)"
+ if [ $RES -eq 0 ]; then
+ echo " [ OK ]"
+ else
+ echo " [ KO ]"
+ return 1
+ fi
+
+ return 0
+}
+
+# Goal: backup an online vserver instance
+# $1: Destination (where to save the backup)
+# $2: VServer name (i.e.: plc)
+# $3: Date
+# $4: LVM dev on which the VServer is mounted (default to /dev/vg/$2 if empty)
+function backup_vserver_online {
+ DEST=$1
+ VSERVER=$2
+ BACKUP_NAME=$1/$2-vs-$3.on.tar.bz2
+ BACKUP_CONF_NAME=$1/$2-vsconf-$3.on.tar.bz2
+ DEV=$4
+ if [ -z $DEV ]; then
+ DEV="/dev/vg/$VSERVER"
+ fi
+
+ echo -n "Backup vserver $VSERVER..."
+ mkdir -p $DEST >/dev/null
+
+ SNAPDEV=${DEV}bkp
+ if [ ! -d /etc/vservers/$VSERVER ]; then
+ echo " (No such vserver) [ KO ]"
+ return 1
+ fi
+
+ # Create a snapshot of the running vserver filesystem...
+ SNAPVG=`echo $SNAPDEV | cut -f 3 -d '/'`
+ SNAPLV=`echo $SNAPDEV | cut -f 4 -d '/'`
+ SNAPMOUNT=/tmp/$SNAPLV
+ lvcreate -L20G -s -n$SNAPLV $DEV >/dev/null
+ if [ $? -ne 0 ]; then
+ echo " (Can't create snapshot volume: $SNAPDEV) [ KO ]"
+ return 1
+ fi
+
+ # ... mount it and backup it...
+ cd /etc/vservers/$VSERVER &&
+ tar cfjp $BACKUP_CONF_NAME * &&
+ mkdir -p $SNAPMOUNT &&
+ mount -t ext3 $SNAPDEV $SNAPMOUNT -o ro &&
+ cd $SNAPMOUNT &&
+ tar cfjp $BACKUP_NAME * 2>/dev/null >/dev/null
+ RES=$?
+
+ # ... and remove the snapshot
+ cd &&
+ umount $SNAPMOUNT &&
+ lvremove -f $SNAPDEV >/dev/null &&
+ rmdir $SNAPMOUNT
+ if [ $? -ne 0 ]; then
+ echo " (Can't delete snapshot volume: $SNAPDEV) [ KO ]"
+ return 1
+ fi
+
+ echo -n " (`stat --printf="%s" $BACKUP_NAME`)"
+ if [ $RES -eq 0 ]; then
+ echo " [ OK ]"
+ else
+ echo " [ KO ]"
+ return 1
+ fi
+
+ return 0
+}
+
+# Goal: backup a vserver instance (online or offline)
+# $1: Destination (where to save the backup)
+# $2: VServer name (i.e.: onelab2)
+# $3: Date
+function backup_vserver {
+ DEST=$1
+ VSERVER=$2
+ DATE=$3
+ if [ ! -d /etc/vservers/$VSERVER ]; then
+ echo "Backup vserver $VSERVER... (No such vserver) [ KO ]"
+ fi
+
+ ctx=`cat /etc/vservers/$VSERVER/context`
+ vserver-stat | grep $ctx 2>&1 > /dev/null
+ offline=$?
+
+ if [ $offline -eq 0 ]; then
+ backup_vserver_online $DEST $VSERVER $DATE
+ else
+ backup_vserver_offline $DEST $VSERVER $DATE
+ fi
+ return $?
+}
+
+# Goal: backup all vserver instances (online or offline) on the machine
+# $1: Destination (where to save the backup)
+# $2: Date
+function backup_all_vservers {
+ for i in /etc/vservers/*; do
+ VSERVER=`basename $i`
+ backup_vserver $1 $VSERVER $2
+ done
+}
+
+# Fetch all backups from a server
+# $1: destination
+# $2: hostname
+# $3: backup user
+# $4: backup location
+function fetch_backup {
+ DEST=$1
+ BACKUP_HOST=$2
+ BACKUP_USER=$3
+ BACKUP_LOC=$4
+ echo -n "Fetch backups from $BACKUP_HOST..."
+ su $BACKUP_USER -c "mkdir -pv $DEST"
+ su $BACKUP_USER -c "rsync --exclude="lost+found" -a $BACKUP_USER@$BACKUP_HOST:$BACKUP_LOC/ $DEST"
+ RES=$?
+ if [ $RES -eq 0 ]; then
+ echo " [OK]"
+ else
+ echo " [KO]"
+ fi
+
+ return 0
+}
+