bb7a89f081e433b64dee54666d582030dead1161
[infrastructure.git] / scripts / ple-backup-cleanup.cron
1 #!/bin/sh
2
3 # Thierry : 30 June 2011
4 # simplify distribution - backup-helper is used only here anyways - embed it right here
5 # also removed unused functions - see backup-helper-deprecated for further reference if needed
6 # backup_dir
7 # backup_vserver_db
8 # backup_vserver_offline
9 # backup_vserver_online
10 # backup_vserver
11 # backup_all_vservers
12 # fetch_backup
13 ######################################## formerly in backup-helper
14 # Goal: backup a PostgreSQL database
15 # $1: Destination
16 # $2: Server name (i.e.: onelab2)
17 # $3: Date
18 # $4: Database name
19 # $5: Database owner
20 function backup_db {
21     DEST=$1
22     DBNAME=$4
23     DBUSER=$5
24     fullname=$DEST/$2-db-$DBNAME-$3.sql
25     mkdir -p $DEST
26     echo -n "Backup postgresql database $DBNAME..."
27     pg_dump -i -U postgres --user=$DBUSER -F c -f $fullname $DBNAME
28     RES=$?
29     echo -n " ($(stat --printf="%s" $fullname) bytes)"
30     [ $RES -eq 0 ] && echo " [ OK ]" || { echo " [ KO ]" ; return 1 ; }
31     return 0
32 }
33
34 # Goal: backup all PostgreSQL databases on the machine
35 # $1: Destination
36 # $2: Server name (i.e.: onelab2)
37 # $3: Date
38 function backup_all_dbs {
39     for i in $(psql -qtAF '|' -U postgres -l 2> /dev/null) ; do
40         NF=$(echo $i | awk -F '|' '{print NF}')
41         if [ $NF -ne 6 ]; then continue; fi
42         DBNAME=$(echo $i | cut -d '|' -f 1)
43         DBUSER=$(echo $i | cut -d '|' -f 2)
44         case $DBNAME in 
45             template*[01]) ;;
46             *) backup_db $1 $2 $3 $DBNAME $DBUSER
47         esac
48     done
49 }
50
51 # backup the ejabberd database on this node
52 # $1: Destination
53 # $2: Date
54 function backup_ejabberd () {
55     dest=$1; shift
56     date=$1; shift
57     fullname=$dest/ejabberd-$date.backup
58     mkdir -p $dest
59     echo -n "Backup ejabberd database ..."
60     ejabberdctl backup $fullname
61     RES=$?
62     echo -n " ($(stat --printf="%s" $fullname) bytes)"
63     [ $RES -eq 0 ] && echo " [ OK ]" || { echo " [ KO ]" ; return 1 ; }
64     return 0
65 }
66
67 ########################################
68 # Thierry - 30 June 2011
69 # this now runs at INRIA
70 # we don't need to push this onto another box anymore, as we have the central backup system
71 # fetch this on a nightly basis
72 # The policy is thus
73 # /db-backup contains the snapshots of the databases
74 # /db-backup /etc /root /var/log and /var/www/html are then fetched
75 # directly from the central backup server
76 # we don't need to handle these anymore from here
77 # NOTE: the central backup server at INRIA has snapshots for moving back in time
78 # 30 last days
79 # 3 last months
80     
81 DATE=$(date +%Y%m%d-%H%M)
82 HOST=$(hostname -s)
83 BACKUP_DIR=/db-backup
84
85
86 # save all databases, either postgresql (currently on www) or ejabberd (currently on boot)
87 function backup () { 
88     echo "backup script on $HOST"
89     echo "Please do not change this script directly, use git and related makefile at"
90     echo "but http://git.onelab.eu/?p=infrastructure.git;a=blob;f=scripts/ple-backup-cleanup.cron"
91     # postgresql
92     ps -C postmaster >& /dev/null && backup_all_dbs $BACKUP_DIR $HOST $DATE \
93         || echo "No postgresql database to backup on $HOST"
94     # ejabberd
95     type -p ejabberdctl >& /dev/null && backup_ejabberd $BACKUP_DIR $DATE \
96         || echo "No ejabberd database to backup on $HOST"
97 }
98
99 # trash old files in $BACKUP_DIR
100 # formerly in /etc/cron.weekly/clean-backup.sh
101 function cleanup () {
102     echo "cleanup script on $HOST"
103     echo "Please do not change this script directly, use git and related makefile at"
104     echo "but http://git.onelab.eu/?p=infrastructure.git;a=blob;f=scripts/ple-backup-cleanup.cron"
105     DATE=$(date +%s)
106     ONE_WEEK=$((7*24*60*6))
107     DATE_OLD=$(($DATE - $ONE_WEEK))
108
109     for file in $BACKUP_REPO/*; do
110         [ -f $file ] || continue
111         STAT=$(stat --printf="%Y" $file)
112         [ $STAT -lt $DATE_OLD ] || continue
113         echo "ple-cleanup.cron: Remove old $file"
114         rm -f "$file"
115     done
116 }
117
118 case $0 in 
119     *backup*) backup ;;
120     *cleanup*) cleanup ;;
121     *) echo "WARNING : invoked crontab script under a wrong name $0" ;;
122 esac