tomws
08-03-2009, 07:09 PM
This is probably not useful at all for seasoned developers, but might help newer users. Here's a Bash shell script to help you dump (backup) multiple databases. I have used a simpler form in tandem with a code backup script and an ftp script to send it all offsite. All of these are called up by a cron job.
I'm not a shell scripter, so use this at your own risk. Quick-tested as root, so perms tests may be screwy.
#!/bin/sh
#
# 20090803 Heavily modified from http://wiki.dreamhost.com/Automatic_Backup
#
# Backs up the mysql for each site in the alldomains backup script, if applicable.
#
# Results: dumps a targa for each configured database into the configured archive directory.
#
###############################################
# THIS FILE STORES PASSWORDS! #
# REMOVE WORLD rwx PERMISSIONS FROM THIS FILE #
###############################################
###############
# CONFIG HERE #
###############
# backup home directory - CREATE THIS FIRST
HOMEDIR="/var/www/backups/"
# archives subdir name - CREATE THIS FIRST
ARCHIVEDIR="archives"
# domain = domain name:
# - used only as the loop array
# - helps to organize your db info
#
# dbsrvr = database server name:
# - used as the hostname selector for the dump command
#
# dbname = database name:
# - used to select the db to dump
#
# dbuser = database user:
# - used as part of login credentials
#
# dbpswd = database password:
# - used as part of login credentials
# - REMOVE WORLD rwx PERMISSIONS FROM THIS FILE
# copy and paste this structure to add a new database
#domains=("${domains[@]}" "example.com")
#dbsrvrs=("${dbsrvrs[@]}" "mysql.example.com")
#dbnames=("${dbnames[@]}" "example-db111")
#dbusers=("${dbusers[@]}" "example-user111")
#dbpswds=("${dbpswds[@]}" "example-password")
domains=("${domains[@]}" "simple.example.org")
dbsrvrs=("${dbsrvrs[@]}" "localhost")
dbnames=("${dbnames[@]}" "simpleexampledb")
dbusers=("${dbusers[@]}" "simpleexampleuser")
dbpswds=("${dbpswds[@]}" "simpleexamplepass")
domains=("${domains[@]}" "tom.example.net")
dbsrvrs=("${dbsrvrs[@]}" "mysql.example.net")
dbnames=("${dbnames[@]}" "tomdb")
dbusers=("${dbusers[@]}" "tomuser")
dbpswds=("${dbpswds[@]}" "tompass")
#####################################
# END CONFIG - DON'T EDIT PAST HERE #
#####################################
if [ ! -d "$HOMEDIR" ];then
echo "Backup problem: $HOMEDIR is not a directory"
exit
elif [ ! -e "$HOMEDIR" ];then
echo "Backup problem: $HOMEDIR does not exist - create it first"
exit
elif [ ! -r "$HOMEDIR" ];then
echo "Backup problem: you do not have read permissions to $HOMEDIR"
exit
elif [ ! -w "$HOMEDIR" ];then
echo "Backup problem: you do not have write permissions to $HOMEDIR"
exit
fi
cd $HOMEDIR
if [ ! -d "$ARCHIVEDIR" ];then
echo "Backup problem: $ARCHIVEDIR is not a directory"
exit
elif [ ! -e "$ARCHIVEDIR" ];then
echo "Backup problem: $ARCHIVEDIR does not exist - create it first"
exit
elif [ ! -r "$ARCHIVEDIR" ];then
echo "Backup problem: you do not had read permissions to $ARCHIVEDIR"
exit
elif [ ! -w "$ARCHIVEDIR" ];then
echo "Backup problem: you do not had write permissions to $ARCHIVEDIR"
exit
fi
DUMPWORKDIR="mysql"
mkdir $DUMPWORKDIR
if [ ! -e "$DUMPWORKDIR" ];then
echo "Backup problem: failed to create temp working directory"
exit
elif [ ! -r "$DUMPWORKDIR" ];then
echo "Backup problem: your account does not had read permissions to the temp working directory"
exit
elif [ ! -w "$DUMPWORKDIR" ];then
echo "Backup problem: your account does not had write permissions to the temp working directory"
exit
fi
for (( i = 0 ; i < ${#domains[@]} ; i++ ))
do
suffix=$(date +%Y-%m-%d_%H%m%S)
mysqldump --opt -u${dbusers[$i]} --password=${dbpswds[$i]} -h${dbsrvrs[$i]} ${dbnames[$i]} > $DUMPWORKDIR/${dbnames[$i]}.$suffix.sql
tar -czf $ARCHIVEDIR/${dbnames[$i]}.$suffix.sql.$suffix.tar.gz $DUMPWORKDIR/${dbnames[$i]}.$suffix.sql
done
rm -r $DUMPWORKDIR/
I'm not a shell scripter, so use this at your own risk. Quick-tested as root, so perms tests may be screwy.
#!/bin/sh
#
# 20090803 Heavily modified from http://wiki.dreamhost.com/Automatic_Backup
#
# Backs up the mysql for each site in the alldomains backup script, if applicable.
#
# Results: dumps a targa for each configured database into the configured archive directory.
#
###############################################
# THIS FILE STORES PASSWORDS! #
# REMOVE WORLD rwx PERMISSIONS FROM THIS FILE #
###############################################
###############
# CONFIG HERE #
###############
# backup home directory - CREATE THIS FIRST
HOMEDIR="/var/www/backups/"
# archives subdir name - CREATE THIS FIRST
ARCHIVEDIR="archives"
# domain = domain name:
# - used only as the loop array
# - helps to organize your db info
#
# dbsrvr = database server name:
# - used as the hostname selector for the dump command
#
# dbname = database name:
# - used to select the db to dump
#
# dbuser = database user:
# - used as part of login credentials
#
# dbpswd = database password:
# - used as part of login credentials
# - REMOVE WORLD rwx PERMISSIONS FROM THIS FILE
# copy and paste this structure to add a new database
#domains=("${domains[@]}" "example.com")
#dbsrvrs=("${dbsrvrs[@]}" "mysql.example.com")
#dbnames=("${dbnames[@]}" "example-db111")
#dbusers=("${dbusers[@]}" "example-user111")
#dbpswds=("${dbpswds[@]}" "example-password")
domains=("${domains[@]}" "simple.example.org")
dbsrvrs=("${dbsrvrs[@]}" "localhost")
dbnames=("${dbnames[@]}" "simpleexampledb")
dbusers=("${dbusers[@]}" "simpleexampleuser")
dbpswds=("${dbpswds[@]}" "simpleexamplepass")
domains=("${domains[@]}" "tom.example.net")
dbsrvrs=("${dbsrvrs[@]}" "mysql.example.net")
dbnames=("${dbnames[@]}" "tomdb")
dbusers=("${dbusers[@]}" "tomuser")
dbpswds=("${dbpswds[@]}" "tompass")
#####################################
# END CONFIG - DON'T EDIT PAST HERE #
#####################################
if [ ! -d "$HOMEDIR" ];then
echo "Backup problem: $HOMEDIR is not a directory"
exit
elif [ ! -e "$HOMEDIR" ];then
echo "Backup problem: $HOMEDIR does not exist - create it first"
exit
elif [ ! -r "$HOMEDIR" ];then
echo "Backup problem: you do not have read permissions to $HOMEDIR"
exit
elif [ ! -w "$HOMEDIR" ];then
echo "Backup problem: you do not have write permissions to $HOMEDIR"
exit
fi
cd $HOMEDIR
if [ ! -d "$ARCHIVEDIR" ];then
echo "Backup problem: $ARCHIVEDIR is not a directory"
exit
elif [ ! -e "$ARCHIVEDIR" ];then
echo "Backup problem: $ARCHIVEDIR does not exist - create it first"
exit
elif [ ! -r "$ARCHIVEDIR" ];then
echo "Backup problem: you do not had read permissions to $ARCHIVEDIR"
exit
elif [ ! -w "$ARCHIVEDIR" ];then
echo "Backup problem: you do not had write permissions to $ARCHIVEDIR"
exit
fi
DUMPWORKDIR="mysql"
mkdir $DUMPWORKDIR
if [ ! -e "$DUMPWORKDIR" ];then
echo "Backup problem: failed to create temp working directory"
exit
elif [ ! -r "$DUMPWORKDIR" ];then
echo "Backup problem: your account does not had read permissions to the temp working directory"
exit
elif [ ! -w "$DUMPWORKDIR" ];then
echo "Backup problem: your account does not had write permissions to the temp working directory"
exit
fi
for (( i = 0 ; i < ${#domains[@]} ; i++ ))
do
suffix=$(date +%Y-%m-%d_%H%m%S)
mysqldump --opt -u${dbusers[$i]} --password=${dbpswds[$i]} -h${dbsrvrs[$i]} ${dbnames[$i]} > $DUMPWORKDIR/${dbnames[$i]}.$suffix.sql
tar -czf $ARCHIVEDIR/${dbnames[$i]}.$suffix.sql.$suffix.tar.gz $DUMPWORKDIR/${dbnames[$i]}.$suffix.sql
done
rm -r $DUMPWORKDIR/