hcoop-all-db-backup: update backup script
authorClinton Ebadi <clinton@hcoop.net>
Sun, 31 Mar 2019 19:46:51 +0000 (15:46 -0400)
committerClinton Ebadi <clinton@hcoop.net>
Sun, 31 Mar 2019 19:46:51 +0000 (15:46 -0400)
 * Use /srv/backup/database instead of /var/backups
 * Retain 7 days of backups instead of only 2
 * Use pigz for faster compression of mysql backups
 * Use --single-transaction to avoid locks during mysql backups
 * Use native postgres dump format instead of sql dumps for postgres
 * Tidy up commands used to list databases to backup
 * Change postgres dir name in preparation for postgres-10 backups

hcoop-all-db-backup

index e4367e5..a3ba510 100755 (executable)
@@ -5,24 +5,24 @@
 # docelic@hcoop.net, Tue Aug 28 13:38:26 EDT 2007
 #
 
-BACKUP_ROOT=/var/backups/databases
-BACKUP_DAYS=2
+BACKUP_ROOT=/srv/backup/database
+BACKUP_DAYS=7
 
 if ! test -d "$BACKUP_ROOT"; then
        echo "Backup root directory '$BACKUP_ROOT' inaccessible."
        exit 1
 fi
 
-MYSQL_DBS=`sudo -H mysqlshow --defaults-file=/root/.my.fritz.cnf | tail -n +4 | head -n -1 | cut -d' ' -f 2 | xargs`
+MYSQL_DBS=`sudo -H mysql -Bse 'show databases' | xargs`
 echo "Mysql Databases are: $MYSQL_DBS"
-PGSQL_DBS=`sudo -u postgres psql -h postgres template1 -c '\l' | tail -n +4 | head -n -2 | cut -d' ' -f 2 | xargs`
+PGSQL_DBS=`sudo -u postgres psql template1 -tAc 'SELECT datname FROM pg_database WHERE datistemplate = false;' | xargs`
 echo "Postgres Databases are: $PGSQL_DBS"
 
 # Delete oldest
 rm -rf "$BACKUP_ROOT/mysql.$BACKUP_DAYS"
-rm -rf "$BACKUP_ROOT/postgres.$BACKUP_DAYS"
+rm -rf "$BACKUP_ROOT/postgres-9.$BACKUP_DAYS"
 
-# Rotate abckup dirs
+# Rotate backup dirs
 for day in `seq $BACKUP_DAYS -1 1`; do
        to=".$day"
        let day_before=" $day - 1"
@@ -33,19 +33,21 @@ for day in `seq $BACKUP_DAYS -1 1`; do
        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
+       mv -f "$BACKUP_ROOT/postgres-9$from" "$BACKUP_ROOT/postgres-9$to" 2>/dev/null
 done
 
+# backups should not be world-readable, mask all o permissions
+umask 0027
+
 # Create current dump dirs
-mkdir --mode=770 "$BACKUP_ROOT/mysql" "$BACKUP_ROOT/postgres"
+mkdir --mode=770 "$BACKUP_ROOT/mysql" "$BACKUP_ROOT/postgres-9"
 
 # Perform MYSQL backup
-for db in $MYSQL_DBS; do 
-       sudo -H mysqldump --defaults-file=/root/.my.fritz.cnf "$db" | gzip - > "$BACKUP_ROOT/mysql/$db.gz"
+for db in $MYSQL_DBS; do
+       sudo -H mysqldump --single-transaction "$db" | pigz - > "$BACKUP_ROOT/mysql/$db.gz"
 done
 
 # Perform PGSQL backup
-for db in $PGSQL_DBS; do 
-       sudo -H -u postgres pg_dump -h postgres "$db" | gzip - > "$BACKUP_ROOT/postgres/$db.gz"
+for db in $PGSQL_DBS; do
+       sudo -H -u postgres pg_dump -Fc "$db" > "$BACKUP_ROOT/postgres-9/$db.pg"
 done
-