don't backup the huge monitor db
[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                 ;;
47             monitor)
48                 # only once every third day; this has reached the 10Bg mark already !
49                 #(( $(date +%d) % 3 )) || backup_db $1 $2 $3 $DBNAME $DBUSER ;;
50                 # Thierry - dec 2015 - skip this one entirely
51                 # too big and not really interesting
52                 ;;
53             *)
54                 backup_db $1 $2 $3 $DBNAME $DBUSER
55                 ;;
56         esac
57     done
58 }
59
60 # backup the ejabberd database on this node
61 # $1: Destination
62 # $2: Date
63 function backup_ejabberd () {
64     dest=$1; shift
65     date=$1; shift
66     fullname=$dest/ejabberd-$date.backup
67     mkdir -p $dest
68     echo -n "Backup ejabberd database ..."
69     ejabberdctl backup $fullname
70     RES=$?
71     echo -n " ($(stat --printf="%s" $fullname) bytes)"
72     [ $RES -eq 0 ] && echo " [ OK ]" || { echo " [ KO ]" ; return 1 ; }
73     return 0
74 }
75
76 ########################################
77 # Thierry - 30 June 2011
78 # this now runs at INRIA
79 # we don't need to push this onto another box anymore, as we have the central backup system
80 # fetch this on a nightly basis
81 # The policy is thus
82 # /db-backup contains the snapshots of the databases
83 # /db-backup /etc /root /var/log and /var/www/html are then fetched
84 # directly from the central backup server
85 # we don't need to handle these anymore from here
86 # NOTE: the central backup server at INRIA has snapshots for moving back in time
87 # 30 last days
88 # 3 last months
89     
90 DATE=$(date +%Y%m%d-%H%M)
91 HOST=$(hostname -s)
92 BACKUP_DIR=/db-backup
93
94
95 # save all databases, either postgresql (currently on www) or ejabberd (currently on boot)
96 function backup () { 
97     echo "backup script on $HOST"
98     echo "Please do not change this script directly, use git and related makefile at"
99     echo "but http://git.onelab.eu/?p=infrastructure.git;a=blob;f=scripts/ple-backup-cleanup.cron"
100     # postgresql
101     ps -C postmaster >& /dev/null || ps -C postgres >& /dev/null && backup_all_dbs $BACKUP_DIR $HOST $DATE \
102         || echo "No postgresql database to backup on $HOST"
103     # ejabberd
104     type -p ejabberdctl >& /dev/null && backup_ejabberd $BACKUP_DIR $DATE \
105         || echo "No ejabberd database to backup on $HOST"
106 }
107
108 # trash old files in $BACKUP_DIR
109 # formerly in /etc/cron.weekly/clean-backup.sh
110 function cleanup () {
111     echo "cleanup script on $HOST"
112     echo "Please do not change this script directly, use git and related makefile at"
113     echo "but http://git.onelab.eu/?p=infrastructure.git;a=blob;f=scripts/ple-backup-cleanup.cron"
114     DATE=$(date +%s)
115     ONE_WEEK=$((7*24*60*6))
116     DATE_OLD=$(($DATE - $ONE_WEEK))
117
118     for file in $BACKUP_REPO/*; do
119         [ -f $file ] || continue
120         STAT=$(stat --printf="%Y" $file)
121         [ $STAT -lt $DATE_OLD ] || continue
122         echo "ple-cleanup.cron: Remove old $file"
123         rm -f "$file"
124     done
125 }
126
127 case $0 in 
128     *backup*) backup ;;
129     *cleanup*) cleanup ;;
130     *) echo "WARNING : invoked crontab script under a wrong name $0" ;;
131 esac