-
-
Save sorohan/5174870 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/sh | |
# | |
# Usage: mysql-backup.sh dbname <daily|hourly|weekly|monthly> <backup dir> <dump args> | |
# | |
if [ -z $1 ]; then | |
echo "Missing required dbname." | |
exit 1 | |
fi | |
# Get Db. | |
DB_NAME="$1" | |
shift | |
if [ -z $1 ]; then | |
echo "Missing required period name." | |
exit 1 | |
fi | |
# Get period. | |
PERIOD_NAME="$1" | |
shift | |
# Get backup dir. | |
BACKUP_DIR="$1" | |
if [ -z $BACKUP_DIR ]; then | |
BACKUP_DIR="${HOME}/backups/mysql" | |
fi | |
# Set format. | |
case $PERIOD_NAME in | |
"hourly") | |
DATE_FMT="H" | |
;; | |
"daily") | |
DATE_FMT="a" | |
;; | |
"weekly") | |
DATE_FMT="d" | |
;; | |
"monthly") | |
DATE_FMT="b" | |
;; | |
*) | |
echo "Invalid period name: ${PERIOD_NAME}" | |
exit 1 | |
;; | |
esac | |
# Default conf file (for default dump args). | |
CONF_FILE="${HOME}/.my.cnf" | |
# Default dump args. | |
DUMP_ARGS="$@" | |
if [ -z "$DUMP_ARGS" ]; then | |
DUMP_ARGS="--defaults-file=${CONF_FILE} --opt" | |
fi | |
# Configure. | |
DUMP_NAME="${BACKUP_DIR}/${PERIOD_NAME}/${DB_NAME}.`date +%${DATE_FMT}`.sql" | |
# Auto mkdir. | |
DIR=`dirname $DUMP_NAME` | |
mkdir -p `dirname $DUMP_NAME` | |
if [ ! -d $DIR ]; then | |
echo "Coudn\'t create backup dir." | |
exit 1 | |
fi | |
DUMP_CMD="nice mysqldump ${DUMP_ARGS} | gzip -1 --rsyncable > \"$DUMP_NAME.gz\"" | |
$DUMP_CMD |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment