hcoop-git-maint: Improve detection of bad permissions.
[clinton/scripts.git] / hcoop-all-db-backup
1 #!/bin/bash
2
3 #
4 # This script backs up every database to its own file, gzipped.
5 # docelic@hcoop.net, Tue Aug 28 13:38:26 EDT 2007
6 #
7
8 BACKUP_ROOT=/var/backups/databases
9 BACKUP_DAYS=2
10
11 if ! test -d "$BACKUP_ROOT"; then
12 echo "Backup root directory '$BACKUP_ROOT' inaccessible."
13 exit 1
14 fi
15
16 MYSQL_DBS=`sudo -H mysqlshow | tail -n +4 | head -n -1 | cut -d' ' -f 2 | xargs`
17 echo "Mysql Databases are: $MYSQL_DBS"
18 PGSQL_DBS=`sudo -u postgres psql template1 -c '\l' | tail -n +4 | head -n -2 | cut -d' ' -f 2 | xargs`
19 echo "Postgres Databases are: $PGSQL_DBS"
20
21 # Delete oldest
22 rm -rf "$BACKUP_ROOT/mysql.$BACKUP_DAYS"
23 rm -rf "$BACKUP_ROOT/postgres.$BACKUP_DAYS"
24
25 # Rotate abckup dirs
26 for day in `seq $BACKUP_DAYS -1 1`; do
27 to=".$day"
28 let day_before=" $day - 1"
29 if test "$day" = "1"; then
30 from=''
31 else
32 from=".$day_before"
33 fi
34
35 mv -f "$BACKUP_ROOT/mysql$from" "$BACKUP_ROOT/mysql$to" 2>/dev/null
36 mv -f "$BACKUP_ROOT/postgres$from" "$BACKUP_ROOT/postgres$to" 2>/dev/null
37 done
38
39 # Create current dump dirs
40 mkdir --mode=770 "$BACKUP_ROOT/mysql" "$BACKUP_ROOT/postgres"
41
42 # Perform MYSQL backup
43 for db in $MYSQL_DBS; do
44 sudo -H mysqldump "$db" | gzip - > "$BACKUP_ROOT/mysql/$db.gz"
45 done
46
47 # Perform PGSQL backup
48 for db in $PGSQL_DBS; do
49 sudo -H -u postgres pg_dump "$db" | gzip - > "$BACKUP_ROOT/postgres/$db.gz"
50 done
51