Last active
November 20, 2015 11:44
-
-
Save starkers/b1fe79b12da2895a7356 to your computer and use it in GitHub Desktop.
Very crude, very simple mysql dumps.. relies on ~/.my.cnf grants allowing full access to dump/lock etc..
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
| #!/usr/bin/env bash | |
| PATH=$PATH:/usr/bin/ | |
| # how to long to keep backups for (in days) | |
| KEEP=150 | |
| # Where to keep them | |
| BACKUP_PATH="/root/mysql_backups/sql" | |
| host="localhost" | |
| MONTH="$(date +%m)" | |
| YEAR="$(date +%Y)" | |
| DAY="$(date +%d)" | |
| # Function to retrieve grants in SQL format | |
| grants(){ | |
| mysql -B -N $@ -e "SELECT DISTINCT CONCAT( | |
| 'SHOW GRANTS FOR ''', user, '''@''', host, ''';' | |
| ) AS query FROM mysql.user" | \ | |
| mysql $@ | \ | |
| sed 's/\(GRANT .*\)/\1;/;s/^\(Grants for .*\)/## \1 ##/;/##/{x;p;x;}' | |
| } | |
| umask 177 | |
| # | seperated list of databases to exclude (egrep -v format) | |
| DB_EXCLUDE="^information_schema$|^mysql$|^performance_schema$" | |
| ## Itterate a list of databases | |
| DB_LIST_FULL="$(mysql -Bse "show databases" | egrep -v "$DB_EXCLUDE")" | |
| echo "### $HOSTNAME backing up databases for $YEAR-$MONTH-$DAY ####" | |
| for a in $DB_LIST_FULL ; do | |
| DB=$a | |
| echo -n "## backup on database: $DB" | |
| mkdir -p "$BACKUP_PATH/$YEAR-$MONTH" | |
| mysqldump "$DB" > "$BACKUP_PATH/$YEAR-$MONTH/$DAY-$DB.sql" 2>&1 | |
| RESULT=$? | |
| if [ "$RESULT" -gt 0 ] ; then | |
| echo " shit fuck warning error.. SOMETHING WEIRD HAPPENED" | |
| else | |
| echo " went ok" | |
| fi | |
| done | |
| # Backup the mysql.users table into nice SQL syntax | |
| grants > "$BACKUP_PATH/$YEAR-$MONTH/$DAY-mysql.user.sql" | |
| sync | |
| sleep 60 #sync can't be trusted.. 'its a GNU' thing :-/ -typical | |
| cd "$BACKUP_PATH/$YEAR-$MONTH/" | |
| echo "####### Summary ##########" | |
| echo "Mbyes Database" ; du -sm $DAY-*.sql | column -t | |
| # Delete files older than $KEEP days | |
| find $BACKUP_PATH/ -type f -mtime +${KEEP} -exec rm -vf {} \; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment