Backing Up MySQL Databases
This is a really simple script that can be used to backup all the databases in a MySQL server. Thought I’d put it here as it shows some basic scripting techniques that maybe will help people see how to write scripts. Basically all this does is save me from having to type out the date. It could be automated but that would require storing the user name and password somewhere which normally isn’t too smart an idea.
#!/bin/sh # GNU GPL version 3.0 or above # Copyright (c) 2009 Mark Sangster if [ -z "$3" ]; then echo 'mybackup <user> <password> <host>' >&2 exit 1 fi MYUSER=$1; MYPASS=$2; MYHOST=$3 mysqldump -u $MYUSER -p $MYPASS -h $MYHOST -A | \ bzip2 -9 > mysql-`date +%Y-%m-%d`.bz2
The script usage is very striaght-forward:
# mybackup user pass hostAt which point you will find a file called “mysql-2009-01-27.bz2″ or similar. Nothing to difficult!
Alternatively you can use this script: http://sourceforge.net/projects/automysqlbackup
Which also does a pretty good job!