From: Thierry Parmentelat Date: Tue, 18 Nov 2014 15:21:00 +0000 (+0100) Subject: created from what was running on www.planet-lab.eu in etc/cron.daily X-Git-Url: http://git.onelab.eu/?p=infrastructure.git;a=commitdiff_plain;h=f7cd066345ff388d4386e4084eabc6a0e1e91d1d created from what was running on planet-lab.eu in etc/cron.daily --- diff --git a/scripts/ple-backup.cron b/scripts/ple-backup.cron new file mode 100755 index 0000000..bb7a89f --- /dev/null +++ b/scripts/ple-backup.cron @@ -0,0 +1,122 @@ +#!/bin/sh + +# Thierry : 30 June 2011 +# simplify distribution - backup-helper is used only here anyways - embed it right here +# also removed unused functions - see backup-helper-deprecated for further reference if needed +# backup_dir +# backup_vserver_db +# backup_vserver_offline +# backup_vserver_online +# backup_vserver +# backup_all_vservers +# fetch_backup +######################################## formerly in backup-helper +# 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 + fullname=$DEST/$2-db-$DBNAME-$3.sql + mkdir -p $DEST + echo -n "Backup postgresql database $DBNAME..." + pg_dump -i -U postgres --user=$DBUSER -F c -f $fullname $DBNAME + RES=$? + echo -n " ($(stat --printf="%s" $fullname) bytes)" + [ $RES -eq 0 ] && echo " [ OK ]" || { echo " [ KO ]" ; return 1 ; } + 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 2> /dev/null) ; do + NF=$(echo $i | awk -F '|' '{print NF}') + if [ $NF -ne 6 ]; then continue; fi + DBNAME=$(echo $i | cut -d '|' -f 1) + DBUSER=$(echo $i | cut -d '|' -f 2) + case $DBNAME in + template*[01]) ;; + *) backup_db $1 $2 $3 $DBNAME $DBUSER + esac + done +} + +# backup the ejabberd database on this node +# $1: Destination +# $2: Date +function backup_ejabberd () { + dest=$1; shift + date=$1; shift + fullname=$dest/ejabberd-$date.backup + mkdir -p $dest + echo -n "Backup ejabberd database ..." + ejabberdctl backup $fullname + RES=$? + echo -n " ($(stat --printf="%s" $fullname) bytes)" + [ $RES -eq 0 ] && echo " [ OK ]" || { echo " [ KO ]" ; return 1 ; } + return 0 +} + +######################################## +# Thierry - 30 June 2011 +# this now runs at INRIA +# we don't need to push this onto another box anymore, as we have the central backup system +# fetch this on a nightly basis +# The policy is thus +# /db-backup contains the snapshots of the databases +# /db-backup /etc /root /var/log and /var/www/html are then fetched +# directly from the central backup server +# we don't need to handle these anymore from here +# NOTE: the central backup server at INRIA has snapshots for moving back in time +# 30 last days +# 3 last months + +DATE=$(date +%Y%m%d-%H%M) +HOST=$(hostname -s) +BACKUP_DIR=/db-backup + + +# save all databases, either postgresql (currently on www) or ejabberd (currently on boot) +function backup () { + echo "backup script on $HOST" + echo "Please do not change this script directly, use git and related makefile at" + echo "but http://git.onelab.eu/?p=infrastructure.git;a=blob;f=scripts/ple-backup-cleanup.cron" + # postgresql + ps -C postmaster >& /dev/null && backup_all_dbs $BACKUP_DIR $HOST $DATE \ + || echo "No postgresql database to backup on $HOST" + # ejabberd + type -p ejabberdctl >& /dev/null && backup_ejabberd $BACKUP_DIR $DATE \ + || echo "No ejabberd database to backup on $HOST" +} + +# trash old files in $BACKUP_DIR +# formerly in /etc/cron.weekly/clean-backup.sh +function cleanup () { + echo "cleanup script on $HOST" + echo "Please do not change this script directly, use git and related makefile at" + echo "but http://git.onelab.eu/?p=infrastructure.git;a=blob;f=scripts/ple-backup-cleanup.cron" + DATE=$(date +%s) + ONE_WEEK=$((7*24*60*6)) + DATE_OLD=$(($DATE - $ONE_WEEK)) + + for file in $BACKUP_REPO/*; do + [ -f $file ] || continue + STAT=$(stat --printf="%Y" $file) + [ $STAT -lt $DATE_OLD ] || continue + echo "ple-cleanup.cron: Remove old $file" + rm -f "$file" + done +} + +case $0 in + *backup*) backup ;; + *cleanup*) cleanup ;; + *) echo "WARNING : invoked crontab script under a wrong name $0" ;; +esac