Skip to content

Instantly share code, notes, and snippets.

@iteman
Created August 24, 2009 08:56
Show Gist options
  • Save iteman/173766 to your computer and use it in GitHub Desktop.
Save iteman/173766 to your computer and use it in GitHub Desktop.
ITEMAN MySQL Backup - A command to backup specified MySQL databases
#!/bin/bash
# ITEMAN MySQL Backup - A command to backup specified MySQL databases
# Copyright (c) 2009 ITEMAN, Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
MYSQL_USER=root
MYSQL_PASSWORD=PASSWORD
BACKUP_DIRECTORY=
BACKUP_EXPIRATION=3
DATABASES=(
)
if [ "x$BACKUP_DIRECTORY" = "x" ]; then
echo "$0: BACKUP_DIRECTORY must be specified, exit"
exit 1
fi
for DATABASE in ${DATABASES[@]}; do
BACKUP_DATE=`/bin/date +%Y%m%d%H%M%S`
BACKUP_FILE="$BACKUP_DIRECTORY/$DATABASE-$BACKUP_DATE"
echo -n "$0: Creating the backup file [ $BACKUP_FILE ] ... "
mysqldump --user=$MYSQL_USER --password=$MYSQL_PASSWORD $DATABASE > $BACKUP_FILE
RETVAL=$?
if [ $RETVAL -ne 0 ]; then
echo "failed, skip this project"
rm -f $BACKUP_FILE
continue
fi
echo "done"
echo -n "$0: Compressing the backup file [ $BACKUP_FILE ] ... "
bzip2 $BACKUP_FILE
RETVAL=$?
if [ $RETVAL -ne 0 ]; then
echo "failed, skip this project"
continue
fi
echo "done"
COMPRESSED_BACKUP_FILE="$BACKUP_FILE.bz2"
echo -n "$0: Changing the permission [ $COMPRESSED_BACKUP_FILE ] ... "
chmod 400 $COMPRESSED_BACKUP_FILE
RETVAL=$?
if [ $RETVAL -ne 0 ]; then
echo "failed, skip this project"
continue
fi
echo "done"
echo "$0: Backup successfully completed"
done
echo -n "$0: Removing the expired backup files ... "
find $BACKUP_DIRECTORY -maxdepth 1 -type f -mtime +$BACKUP_EXPIRATION -exec rm -f '{}' \;
RETVAL=$?
if [ $RETVAL -ne 0 ]; then
echo "failed, exit"
exit 1
fi
echo "done"
exit 0
# Local Variables:
# mode: shell-script
# coding: iso-8859-1
# tab-width: 4
# indent-tabs-mode: nil
# End:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment