Skip to content

Instantly share code, notes, and snippets.

@netkiller
Created September 3, 2013 06:39
Show Gist options
  • Save netkiller/6420375 to your computer and use it in GitHub Desktop.
Save netkiller/6420375 to your computer and use it in GitHub Desktop.
MySQL Backup Script
#!/bin/bash
###################################
# $Id: backup 381 2012-04-03 09:49:08Z netkiller $
# Author: netkiller@msn.com
# Home: http://netkiller.github.com
###################################
BACKUP_HOST="localhost"
BACKUP_USER="root"
BACKUP_PASS=""
BACKUP_DIR=/opt/backup
BACKUP_DBNAME="test neo"
#Number of copies
COPIES=7
####################################
MYSQLDUMP="mysqldump"
#TIMEPOINT=$(date -u +%Y-%m-%d)
TIMEPOINT=$(date -u +%Y-%m-%d.%H:%M:%S)
MYSQLDUMP_OPTS="-h $BACKUP_HOST -u$BACKUP_USER -p$BACKUP_PASS"
####################################
umask 0077
test ! -d "$BACKUP_DIR" && mkdir -p "$BACKUP_DIR"
test ! -w $BACKUP_DIR && echo "Error: $BACKUP_DIR is un-writeable." && exit 0
for dbname in $BACKUP_DBNAME
do
test ! -d "$BACKUP_DIR/$dbname" && mkdir -p "$BACKUP_DIR/$dbname"
$MYSQLDUMP $MYSQLDUMP_OPTS $dbname | gzip > $BACKUP_DIR/$dbname/$dbname.$TIMEPOINT.sql.gz
done
find $BACKUP_DIR -type f -mtime +$COPIES -delete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment