Skip to content

Instantly share code, notes, and snippets.

@rzbrk
Created November 29, 2013 22:31
Show Gist options
  • Save rzbrk/7712809 to your computer and use it in GitHub Desktop.
Save rzbrk/7712809 to your computer and use it in GitHub Desktop.
Performs mysql backup (via mysql_dump). The database password is retrieved from /etc/mysql/debian.cnf. Therefore, run this script as root
#!/bin/bash
mysql_conf_file="/etc/mysql/debian.cnf"
mysqldump_opts="--all-databases --events --add-drop-table --add-locks --create-options --disable-keys --lock-tables --quick --set-charset"
now=$(date '+%Y%m%d-%H%M%S')
#Extract password for MySQL user debian-sys-maint from /etc/mysql/debian.cnf. Two
#code lines are necessary to cope with files that include to identical lines
#defining the password
temp=$(cat $mysql_conf_file | grep "password =" | sed -e '{:q;N;s/\n/ /g;t q}')
password=$(echo $temp | sed -e 's/.*\n//;s/password = //g;s/.* //')
# Create MySQL dump
dfile=~/mysql_backup_$HOSTNAME"_"$now.sql.gz
mysqldump $mysqldump_opts -u debian-sys-maint -p$password | gzip > $dfile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment