About a month ago I made the mistake of deleting a backup script from the server I was maintaining. This was my only copy of it and I hated myself two seconds after i typed rm -f backupscript.
Now I have quite a few more servers than I did a couple of months ago and they are all functioning in a production environment. Therefore I need a more organized way to back stuff up. Sure we do tape backups and what not, but I like to have everything all in one place in case i need to port it somewhere or other (for testing and dev purposes).
Anyway the thing backs up my MySQL databases, app directories, and some key config files (apache and php for now). It also prints out a lovely log just in case something goes wrong (in the crontab i have it print things to the log). Eventually I’d like the log to do basic error checking beyond the minimal it does now (ie listing out the files it just created).
Below you’ll find my backupscript. Could it be worse? Yes. Could it be better? Hell yes. Do I want to answer questions about it? NO! In other words, use at your own risk. (Disclaimer: this blog is for me and my awful memory, the stuff here is not intended for use outside of that).
#!/bin/bash
#first define some variables that are used throughout the script
MYSQL=`which mysql`;
MYSQLDUMP=`which mysqldump`;
GZIP=`which gzip`;
TAR=`which tar`;
DATE=`date`;
#have the user of the script define variables for their system
#list the directories you want backed up (leave out final slash)
APPDIRS=( /dir/to/app1 /dir/to/app2 );
#list the above directories corresponding application name
APPNAMES=( app1 app2 );
#list root mysql user information
USER="root";
PWD="*****";
#list what directory you want things backed up to
DEST="/dir/to/backup";
echo " ";
echo " ";
echo "-------------------------------------------------";
echo "BEGIN BACK UP LOG FOR $DATE";
echo "-------------------------------------------------";
echo " ";
echo " ";
echo " ";
echo "-------------------------------------------------";
echo "PROCESSING DATABASE FILES";
echo " ";
#call the databases from mysql that will be backed up
#do not include the test database or the information schema
DBS=(`$MYSQL -u $USER -p$PWD -Bse 'show databases' | grep -v "test" | grep -v "information_schema"`);
#now display the databases that are being backed up
#so that they will be printed in the log
echo "The following databases will be backed up...";
for db in ${DBS[@]};
do
echo " $db";
done;
#begin backing up the database
#show that you are backing up the database for benefit of the log
echo " ";
echo " ";
echo "Backing up databases...";
for db in ${DBS[@]};
do
echo -n " backing up $db.....";
# a mysqldump of the database and then gzip it up
`$MYSQLDUMP --default-character-set=utf8 --set-charset -u $USER -p$PWD $db | $GZIP -9 > $DEST/$db.sql.gz`
echo "DONE";
done;
echo " ";
echo " ";
echo "Created the following backup files...";
set $DEST/*.sql.gz
for file in ${@};
do
echo " $file";
done;
echo " ";
echo "FINISHED PROCESSING DATABASE FILES";
echo "-------------------------------------------------";
echo " ";
echo " ";
echo " ";
echo "-------------------------------------------------";
echo "PROCESSING CONFIGURATION FILES";
echo " ";
#now lets get other important files and back them up here too
cp /etc/php.ini $DEST/php.ini.bak
cp /etc/httpd/conf/httpd.conf $DEST/httpd.conf.bak
#display the files that you just copied over
echo "Copied the following configuration files...";
set $DEST/*.bak
for file in ${@};
do
echo " $file";
done;
#now we need to tar them up
echo " ";
echo " ";
echo "Tarring up the configuration files...";
#and then zip them up
$TAR cvzf conf.tgz $DEST/*.bak
#and delete the unneeded .bak files
rm -f $DEST/*.bak
echo "DONE";
echo " ";
echo "FINISHED PROCESSING CONFIGURATION FILES";
echo "-------------------------------------------------";
echo " ";
echo " ";
echo " ";
echo "-------------------------------------------------";
echo "PROCESSING APPLICATION FILES";
echo " ";
echo "Backing up the following application directories...";
#take the directories that are in the array and print them
for appdir in ${APPDIRS[@]};
do
echo " $appdir";
done;
echo " ";
echo " ";
echo "Tarring up the directories...";
for (( i = 0 ; i < ${#APPDIRS[@]} ; i++ ));
do
echo "creating tar archive for ${APPNAMES[$i]}";
tar cvzf ${APPNAMES[$i]}.tgz ${APPDIRS[$i]}/;
echo "DONE";
echo " ";
done;
echo " ";
echo "Created the following backup files...";
for name in ${APPNAMES[@]};
do
ls $name.tgz;
done;
echo " ";
echo "FINISHED PROCESSING APPLICATION FILES";
echo "-------------------------------------------------";
echo " ";
echo " ";
echo " ";
echo "-------------------------------------------------";
echo "END BACKUP LOG FOR $DATE";
echo "-------------------------------------------------";
Isn’t it a little ironic that you deleted the only copy of your backup script? Didn’t you have a backup? :)
I use this for mysql – it’s nice:
http://sourceforge.net/projects/automysqlbackup/
its very ironic. i thought that after i went “doh!”. i mean its a backup script. shouldn’t i have a backup of it. now i do.