User Tools

Site Tools


computing:linux:mysql_backup

This is an old revision of the document!


MySQL and PostgreSQL Backup Script

You can modify this script to only backup MySQL or only PostgreSQL.

db_backup.sh

:!: 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="bad1egg!!"
# Mysql username
MYSQLUSER="root"

# PostgreSQL backup username
PGSQLUSER="postgres"

# 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 postgres > /dev/null; then

echo "Begin Backing up PostgreSQL Databases...         `date`" >> $logfile
echo "" >> $logfile

# Dump all databases at once
/usr/bin/pg_dumpall -w --clean -h localhost -U $PGSQLUSER | 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
LIST_PG_DB="select datname from pg_database where not datistemplate and datallowconn order by datname;"

databases=`psql -h localhost -U $PGSQLUSER -At -c "$LIST_PG_DB" postgres`
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
computing/linux/mysql_backup.1477779904.txt.gz · Last modified: 2016/10/29 16:25 by gcooper