Created
January 7, 2013 13:52
-
-
Save berkayunal/4475150 to your computer and use it in GitHub Desktop.
This file contains 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 | |
# Enter the names of the dbs to be backup | |
# Example: DBONLY=("database1" "cphulkd") | |
# If you want to backup all | |
# Example: DBONLY=() | |
DBONLY=("database1" "cphulkd") | |
# Enter minutes for file deletetion older than X | |
# Example 10 Minutes: DELFILESOLDERTHANMIN="10" | |
# If you do not want to delete | |
# Example: DELFILESOLDERTHANMIN="0" | |
DELFILESOLDERTHANMIN="36000" | |
DATE=`date +%Y%m%d_%H%M%S` | |
USER="root" | |
PASSWORD="" | |
OUTPUTDIR="/mysql_backup" | |
MYSQLDUMP="mysqldump" | |
MYSQL="mysql" | |
# ----------------------------------- | |
# END CONFIGURATION DO NOT EDIT BELOW | |
# ----------------------------------- | |
databases=`$MYSQL --user=$USER --password=$PASSWORD -e "SHOW DATABASES;" | tr -d "| " | grep -v Database` | |
lendbonly=${#DBONLY[*]} | |
# dump each database in turn | |
echo "BACKING UP DATABASES…" | |
for db in $databases; do | |
ISDUMP="0" | |
if [ "$lendbonly" -gt "0" ] | |
then | |
if containsElement $db "${DBONLY[@]}" = 1 | |
then | |
ISDUMP="1" | |
fi | |
else | |
ISDUMP="1" | |
fi | |
if [ "$ISDUMP" -eq "1" ] | |
then | |
echo $db " -> " "$OUTPUTDIR/$db-$DATE.sql.gz" | |
$MYSQLDUMP --force --opt --user=$USER --password=$PASSWORD --databases $db | gzip > "$OUTPUTDIR/$db-$DATE.sql.gz"; | |
fi | |
done | |
echo | |
echo | |
if [ "$DELFILESOLDERTHANMIN" -gt "0" ] | |
echo "DELETING FILES OLDER THAN " $DELFILESOLDERTHANMIN " MINUTES…" | |
then | |
# clean up any old backups - save space | |
find "$OUTPUTDIR"/*.gz -type f -mmin "+$DELFILESOLDERTHANMIN" | while read FILENAME | |
do | |
rm -f "$FILENAME" | |
echo "Deleting file: " $FILENAME | |
done | |
fi | |
echo "DONE" | |
echo | |
containsElement () { for e in "${@:2}"; do [[ "$e" = "$1" ]] && return 0; done; return 1; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment