#! /usr/bin/env bash # will back up all mySQL and PostgreSQL databases on server # Use backup_mysql.initialize in for greater control and multiple backup copies VERSION=1.0.1 MYSQLDATAPATH=/var/lib/mysql/ DBSAVEPATH=/home/dbbackup/ POSTGRES_BACKUP_NAME=posgres.dmp if [ ! -e $DBSAVEPATH ] then mkdir $DBSAVEPATH chown postgres:root $DBSAVEPATH fi if which pg_dumpall > /dev/null # don't do this if pg_dumpall not installed then # get all postgres databases, just do a db dump su postgres -c 'pg_dumpall -d -c ' > $DBSAVEPATH$POSTGRES_BACKUP_NAME if [ -e $DBSAVEPATH$POSTGRES_BACKUP_NAME.gz ] then rm -f $DBSAVEPATH$POSTGRES_BACKUP_NAME.gz fi gzip -9q $DBSAVEPATH$POSTGRES_BACKUP_NAME fi # back up all MySQL databases if [ -e $MYSQLDATAPATH ] then for db in `mysqlshow | grep -v ^+ | grep -v Databases` do if [ "$db" != "|" ] then if [ -d $MYSQLDATAPATH$db ] then mysqldump -c --add-drop-table $db > $DBSAVEPATH$db.dmp if [ -e $DBSAVEPATH$db.dmp.gz ] then rm -f $DBSAVEPATH$db.dmp.gz fi gzip -9q $DBSAVEPATH$db.dmp echo Database $db backed up to $DBSAVEPATH$db.dmp.gz fi fi done fi