This is an old revision of the document!
You can modify this script to only backup MySQL or only PostgreSQL.
You must edit the script, at least for the MySQL root password.
#!/bin/sh # # Script to dump mysql and postgresql databases before nightly backup # # Use chmod 700 on this file so only root (or sudo) can view contents # Backup destination dir BACKUP="/root/sql_backup" # Mysql password MYSQLPW="mysqlpassword" # Mysql username MYSQLUSER="root" # Location of the backup logfile. logfile="$BACKUP/sql_backup.log" ################### end of configuration variables ################### # Create the backup destination if it doesn't exist if [ ! -d $BACKUP ]; then mkdir -p $BACKUP echo "Created backup destination directory..." >> $logfile echo "" >> $logfile fi # # PostgreSQL # if pgrep postmaster > /dev/null; then echo "Begin Backing up PostgreSQL Databases... `date`" >> $logfile echo "" >> $logfile # Dump all databases at once /usr/bin/pg_dumpall --clean -h localhost -U postgres | gzip > "$BACKUP/postgresql-all-databases.sql.gz" echo "Backup complete for all PostreSQL databases at once at `date +%H:%M`" >> $logfile echo "" >> $logfile # Individual databases databases=`psql -h localhost -U postgres -q -c "\l" | sed -n 4,/\eof/p | grep -v rows\) | awk {'print $1'}` for i in $databases; do /usr/bin/vacuumdb -z -h localhost -U postgres $i >/dev/null 2>&1 /usr/bin/pg_dump $i -h localhost -U postgres | gzip > "$BACKUP/postgresql-$i-database.sql.gz" echo "Backup and Vacuum complete for database: $i at `date +%H:%M`" >> $logfile done else echo "PostgreSQL is not running. Skipping..." >> $logfile fi echo "" >> $logfile # # Mysql # if pgrep mysqld > /dev/null; then echo "Begin Backing up MySQL Databases... `date`" >> $logfile echo "" >> $logfile # One big dump /usr/bin/mysqldump -u $MYSQLUSER -p$MYSQLPW --add-locks --add-drop-table --events --ignore-table=mysql.event --skip-lock-tables --ignore-table=performance_schema.* -A -Q | gzip > "$BACKUP/mysql-all-databases.sql.gz" echo "Backup complete for all Mysql databases at once at `date +%H:%M`" >> $logfile echo "" >> $logfile # Individual databases for db in `mysql -u $MYSQLUSER -p$MYSQLPW -BNre "show databases;"` do if [ "$db" != "information_schema" ] && [ "$db" != "performance_schema" ] then mysqldump -u $MYSQLUSER -p$MYSQLPW --add-drop-table --events --ignore-table=mysql.event --skip-lock-tables --ignore-table=performance_schema.* -QB "$db" | gzip > "$BACKUP/mysql-$db-database.sql.gz" echo "Backup complete for database: $db at `date +%H:%M`" >> $logfile fi done else echo "MySQL not running. Skipping..." >> $logfile fi echo "" >> $logfile exit