Skip to content

Instantly share code, notes, and snippets.

@jeroenbourgois
Created February 9, 2011 21:25
Show Gist options
  • Save jeroenbourgois/819319 to your computer and use it in GitHub Desktop.
Save jeroenbourgois/819319 to your computer and use it in GitHub Desktop.
Backup scripts that goes through all the mysql dbs and backups them to a backup folder, by date
#!/bin/sh
set -o errexit
nopass=0
if [ $# -eq 0 ]; then
echo "-----> No parameters passed, assuming username 'root' and empty password"
DBS=`mysql -uroot -e "show databases;"`
nopass=1
elif [ $# -eq 1 ]; then
echo "Enter mysql password:"
stty -echo
read password
stty echo
echo ""
DBS=`mysql -u ${1} -p${password} -e "show databases;"`
fi
echo "-----> Backing up mysql dbs..."
for db in $DBS; do
datedir=$(date +%F)
path="~/.backups/mysql/${datedir}"
if [ ! -d "$path" ]; then
mkdir -p ~/.backups/mysql/$datedir
fi
if [ $db != "Database" -a $db != "information_schema" ]; then
echo "\t-----> Backing up $db"
if [ $nopass -eq 1 ]; then
mysqldump -u ${1} $db > ~/.backups/mysql/$db-$datedir.sql
else
mysqldump -u ${1} -p${password} $db > ~/.backups/mysql/$datedir/$db-$datedir.sql
fi
tar -czPf ~/.backups/mysql/$datedir/$db-$datedir.sql.tar.gz ~/.backups/mysql/$datedir/$db-$datedir.sql
echo "\t\t -> compressed it!"
#we compressed the sql backups, let's delete those large files
rm -f ~/.backups/mysql/$datedir/$db-$datedir.sql
fi
done
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment