Last active
August 29, 2015 14:22
-
-
Save pajtai/de2315fafde61e82ac17 to your computer and use it in GitHub Desktop.
Backup mysql data to a directory and clear directory of old files.
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
#!/usr/bin/env bash | |
set -e | |
#set -x # for debugging | |
MYSQL_PW="" | |
TTL=365 | |
while test $# -gt 0; do | |
case "$1" in | |
-h|--help) | |
echo "Database backup." | |
echo " " | |
echo "Creates a mysql dump in ~/backups and packages it in a tar.gz" | |
echo " " | |
echo "options:" | |
echo "-h, --help show help" | |
echo "-u, --user mysql user" | |
echo "-p, --password mysql user password" | |
echo " to be prompted for a password, do not include this flag" | |
echo "-d, --database mysql database - defaults to theme if not provided" | |
echo "-t, --ttl time to live of files in days" | |
echo " files older than ttl days will be deleted" | |
exit 0 | |
;; | |
-u|--user) | |
shift | |
MYSQL_USER=$1 | |
shift | |
;; | |
-p|--password) | |
shift | |
MYSQL_PW=$1 | |
shift | |
;; | |
-d|--database) | |
shift | |
MYSQL_DB=$1 | |
shift | |
;; | |
-t|--ttl) | |
shift | |
TTL=$1 | |
shift | |
;; | |
*) | |
break | |
;; | |
esac | |
done | |
echo " " | |
THE_THEME="$MYSQL_DB" | |
THE_DATE=$(date +'%Y-%m-%d-%T' | sed 's/:/_/g') | |
THE_DIR="$THE_THEME-$THE_DATE" | |
echo "the dir: $THE_DIR" | |
cd ~/backups | |
mkdir $THE_DIR | |
cd $THE_DIR | |
echo "starting mysql backup u:$MYSQL_USER d:$MYSQL_DB" | |
mysqldump -u$MYSQL_USER -p$MYSQL_PW $MYSQL_DB --debug-info > $THE_THEME.sql | |
echo " " | |
cd ~/backups | |
TAR_FILE="$THE_THEME-$THE_DATE.tar.gz" | |
tar --remove-files -zcvf $TAR_FILE $THE_DIR | |
echo "Done backuping up $THE_THEME" | |
echo " " | |
echo "Checking for old files to delete" | |
find ~/backups -type f -mtime +$TTL -delete | |
echo "--------------------------------------------" | |
echo " " | |
echo " " | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment