Last active
December 20, 2015 23:09
-
-
Save miglen/6210722 to your computer and use it in GitHub Desktop.
Mysql db dump and www backup script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Backup mysql dbs and web directory | |
# Date: 12-08-2013 | |
# Author: Miglen Evlogiev <[email protected]> | |
# Mysql credentials | |
MYSQLUSER="root" | |
MYSQLPASS="password" | |
# Additional variables | |
OUTPUTDIR="/backup/"$(date +'%Y/%m') | |
WEBDIR="/var/www/html" | |
MD5STRING=`date +%s | md5sum | cut -d' ' -f1` | |
TEMPDIR="/tmp/"${MD5STRING} | |
GZIP_ENABLED=1 | |
MYSQLDUMP="/usr/bin/mysqldump" | |
MYSQL="/usr/bin/mysql" | |
TITEMSTAMPFL=$(date '+%H-%M_%d-%m-%Y') | |
# make the output directory | |
if [ ! -d "$OUTPUTDIR" ]; then | |
mkdir -p $OUTPUTDIR | |
fi | |
# make the temp directory | |
if [ ! -d $TEMPDIR ]; then | |
mkdir -p $TEMPDIR | |
fi | |
# get a list of databases | |
MYSQLDBS=`/usr/bin/mysql --user=$MYSQLUSER --password=$MYSQLPASS -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema)"` | |
# Stop the http server | |
/etc/init.d/httpd stop | |
# dump each database in turn | |
for DBS in $MYSQLDBS; do | |
echo "Dumping database: ${DBS}" | |
if [ $GZIP_ENABLED == 1 ]; then | |
/usr/bin/mysqldump --force --opt --user=$MYSQLUSER --password=$MYSQLPASS --databases $DBS | gzip > "$TEMPDIR/$DBS.sql.gz" | |
else | |
/usr/bin/mysqldump --force --opt --user=$MYSQLUSER --password=$MYSQLPASS --databases $DBS > "$TEMPDIR/$DBS.sql.sql" | |
fi | |
done | |
# Backup the web directory | |
/bin/tar -zcvf $OUTPUTDIR/www-${TITEMSTAMPFL}.tar.gz --exclude='*.jpg' --exclude='*.gz' --exclude='*.zip' $WEBDIR/* 1>/dev/null | |
# Start the apache again | |
# echo "Starting the Apache HTTP Server " | |
/etc/init.d/httpd start | |
# Merge all files into one and move to output | |
/bin/tar -zcvf $OUTPUTDIR/mysqlbackup-${TITEMSTAMPFL}.tar.gz $TEMPDIR/* 1>/dev/null | |
# Remove all temp files | |
rm -rf $TEMPDIR | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment