Skip to content

Instantly share code, notes, and snippets.

@tzengerink
Created June 20, 2012 09:21
Show Gist options
  • Save tzengerink/2959003 to your computer and use it in GitHub Desktop.
Save tzengerink/2959003 to your computer and use it in GitHub Desktop.
Automated MySQL Dump
#!/bin/bash
#
# MYSQL BACKUP
# ------------
# Usage: Setup a cronjob to execute the script and your done.
#
# Copyright (c) 2012, T. Zengerink
# Licensed under MIT License.
# See: https://raw.github.com/gist/3151357/9e8e01df4ee12b1f04cd61e0ecee3ea8bd6f617b/mit-license.txt
DIR="$(dirname $BASH_SOURCE)"
HOST="hostname"
USER="username"
PASS="password"
DBASE="database"
# Create backup directory
mkdir -p "$DIR/$DBASE"
# Dump MySQL (Engine = InnoDB)
mysqldump -u$USER -p$PASS -h$HOST $DBASE --single-transaction > "$DIR/$DBASE/$DBASE-$(date +%Y%m%d-%H%M%S).sql"
# Remove old backups
find "$DIR/$DBASE/" -type f -name "$DBASE-*.sql" -mtime +7 -delete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment