Check in new hcoop-all-db-backup script
authorbigmike160 <bigmike160>
Wed, 29 Aug 2007 18:49:23 +0000 (18:49 +0000)
committerbigmike160 <bigmike160>
Wed, 29 Aug 2007 18:49:23 +0000 (18:49 +0000)
hcoop-all-db-backup [new file with mode: 0755]

diff --git a/hcoop-all-db-backup b/hcoop-all-db-backup
new file mode 100755 (executable)
index 0000000..9db1b1d
--- /dev/null
@@ -0,0 +1,51 @@
+#!/bin/bash
+
+#
+# This script backs up every database to its own file, gzipped.
+# docelic@hcoop.net, Tue Aug 28 13:38:26 EDT 2007
+#
+
+BACKUP_ROOT=/var/backups/databases
+BACKUP_DAYS=7
+
+if ! test -d "$BACKUP_ROOT"; then
+       echo "Backup root directory '$BACKUP_ROOT' inaccessible."
+       exit 1
+fi
+
+MYSQL_DBS=`sudo -H mysqlshow | tail -n +4 | head -n -1 | cut -d' ' -f 2 | xargs`
+echo "Mysql Databases are: $MYSQL_DBS"
+PGSQL_DBS=`sudo -u postgres psql template1 -c '\l' | tail -n +4 | head -n -2 | cut -d' ' -f 2 | xargs`
+echo "Postgres Databases are: $PGSQL_DBS"
+
+# Delete oldest
+rm -rf "$BACKUP_ROOT/mysql.$BACKUP_DAYS"
+rm -rf "$BACKUP_ROOT/postgres.$BACKUP_DAYS"
+
+# Rotate abckup dirs
+for day in `seq $BACKUP_DAYS -1 1`; do
+       to=".$day"
+       let day_before=" $day - 1"
+       if test "$day" = "1"; then
+               from=''
+       else
+               from=".$day_before"
+       fi
+
+       mv -f "$BACKUP_ROOT/mysql$from" "$BACKUP_ROOT/mysql$to" 2>/dev/null
+       mv -f "$BACKUP_ROOT/postgres$from" "$BACKUP_ROOT/postgres$to" 2>/dev/null
+done
+
+# Create current dump dirs
+mkdir --mode=770 "$BACKUP_ROOT/mysql" "$BACKUP_ROOT/postgres"
+
+# Perform MYSQL backup
+for db in $MYSQL_DBS; do 
+       sudo -H mysqldump "$db" | gzip - > "$BACKUP_ROOT/mysql/$db.gz"
+done
+
+# Perform PGSQL backup
+for db in $PGSQL_DBS; do 
+       sudo -H -u postgres pg_dump "$db" | gzip - > "$BACKUP_ROOT/postgres/$db.gz"
+done
+