a unique standalone cron script
[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         DBNAME=$(echo $i | cut -d '|' -f 1)
41         DBUSER=$(echo $i | cut -d '|' -f 2)
42         case $DBNAME in 
43             template*[01]) ;;
44             *) backup_db $1 $2 $3 $DBNAME $DBUSER
45         esac
46     done
47 }
48
49 # backup the ejabberd database on this node
50 # $1: Destination
51 # $2: Date
52 function backup_ejabberd () {
53     dest=$1; shift
54     date=$1; shift
55     fullname=$dest/ejabberd-$date.backup
56     mkdir -p $dest
57     echo -n "Backup ejabberd database ..."
58     ejabberdctl backup $fullname
59     RES=$?
60     echo -n " ($(stat --printf="%s" $fullname) bytes)"
61     [ $RES -eq 0 ] && echo " [ OK ]" || { echo " [ KO ]" ; return 1 ; }
62     return 0
63 }
64
65 ########################################
66 # Thierry - 30 June 2011
67 # this now runs at INRIA
68 # we don't need to push this onto another box anymore, as we have the central backup system
69 # fetch this on a nightly basis
70 # The policy is thus
71 # /db-backup contains the snapshots of the databases
72 # /db-backup /etc /root /var/log and /var/www/html are then fetched
73 # directly from the central backup server
74 # we don't need to handle these anymore from here
75 # NOTE: the central backup server at INRIA has snapshots for moving back in time
76 # 30 last days
77 # 3 last months
78     
79 DATE=$(date +%Y%m%d-%H%M)
80 HOST=$(hostname -s)
81 BACKUP_DIR=/db-backup
82
83
84 # save all databases, either postgresql (currently on www) or ejabberd (currently on boot)
85 function backup () { 
86     # postgresql
87     ps -C postmaster >& /dev/null && backup_all_dbs $BACKUP_DIR $HOST $DATE \
88         || echo "No postgresql database to backup on $HOST"
89     # ejabberd
90     type -p ejabberdctl >& /dev/null && backup_ejabberd $BACKUP_DIR $DATE \
91         || echo "No ejabberd database to backup on $HOST"
92 }
93
94 # trash old files in $BACKUP_DIR
95 # formerly in /etc/cron.weekly/clean-backup.sh
96 function cleanup () {
97     DATE=$(date +%s)
98     ONE_WEEK=$((7*24*60*6))
99     DATE_OLD=$(($DATE - $ONE_WEEK))
100
101     for file in $BACKUP_REPO/*; do
102         [ -f $file ] || continue
103         STAT=$(stat --printf="%Y" $file)
104         [ $STAT -lt $DATE_OLD ] || continue
105         echo "ple-cleanup.cron: Remove old $file"
106         rm -f "$file"
107     done
108 }
109
110 case $0 in 
111     *backup*) backup ;;
112     *cleanup*) cleanup ;;
113     *) echo "WARNING : invoked crontab script under a wrong name $0" ;;
114 esac