hcoop-backup: Tidy up some UUOC.
[clinton/scripts.git] / hcoop-all-db-backup
CommitLineData
bf301371 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
8BACKUP_ROOT=/var/backups/databases
9BACKUP_DAYS=7
10
11if ! test -d "$BACKUP_ROOT"; then
12 echo "Backup root directory '$BACKUP_ROOT' inaccessible."
13 exit 1
14fi
15
16MYSQL_DBS=`sudo -H mysqlshow | tail -n +4 | head -n -1 | cut -d' ' -f 2 | xargs`
17echo "Mysql Databases are: $MYSQL_DBS"
18PGSQL_DBS=`sudo -u postgres psql template1 -c '\l' | tail -n +4 | head -n -2 | cut -d' ' -f 2 | xargs`
19echo "Postgres Databases are: $PGSQL_DBS"
20
21# Delete oldest
22rm -rf "$BACKUP_ROOT/mysql.$BACKUP_DAYS"
23rm -rf "$BACKUP_ROOT/postgres.$BACKUP_DAYS"
24
25# Rotate abckup dirs
26for 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
37done
38
39# Create current dump dirs
40mkdir --mode=770 "$BACKUP_ROOT/mysql" "$BACKUP_ROOT/postgres"
41
42# Perform MYSQL backup
43for db in $MYSQL_DBS; do
44 sudo -H mysqldump "$db" | gzip - > "$BACKUP_ROOT/mysql/$db.gz"
45done
46
47# Perform PGSQL backup
48for db in $PGSQL_DBS; do
49 sudo -H -u postgres pg_dump "$db" | gzip - > "$BACKUP_ROOT/postgres/$db.gz"
50done
51