Created
June 22, 2009 11:20
-
-
Save vjt/133930 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 | |
# Database Dump utility for MySQL. Put in cron like this: | |
# | |
# DUMPDIR='/home/example/DatabaseDumps' USER='dbuser' PASS='dbpass' DB='dbname' /path/to/dumpdb.sh | |
# | |
# (C) 2009 vjt@openssl.it | |
# | |
[ -z "$MAXAGE" ] && MAXAGE=744 | |
[ -z "$DUMPDIR" -o -z "$USER" -o -z "$PASS" -o -z "$DB" ] && { | |
echo "Please pass correct environment: DUMPDIR ($DUMPDIR), USER ($USER), PASS ($PASS) and DB ($DB) [MAXAGE ($MAXAGE)]" | |
exit 1 | |
} | |
set -e | |
current_md5() { | |
md5sum $DUMPDIR/`ls -t1 $DUMPDIR | head -1` 2>/dev/null | awk '{print $1}' | |
} | |
# Dump | |
previous_md5=`current_md5` | |
dump="$DUMPDIR/`whoami`-`date +%Y%m%d`.sql.bz2" | |
[ -f "$dump" ] && exit 0 | |
mysqldump -ec -u $USER -p$PASS $DB | grep -v '^-- Dump' | bzip2 -9 > $dump | |
chmod 640 $dump | |
#chgrp dropbox $dump | |
# Cleanup | |
[ "$previous_md5" = "`current_md5`" ] && rm -f $dump | |
find $DUMPDIR -mtime $MAXAGE -exec rm -f {} \; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment