#!/bin/sh
#
# MySQL backup script for rsnapshot
# Copyright (c) 2006 Pieter Hollants <suserpms@hollants.com>
# This file is free software licensed under the GNU Public License (GPL) v2.
#
# By contrast to the backup_mysql.sh that ships in rsnapshot's utils/ directory,
# this script backs up each database into invidual files in the current directory.
# For this to work, there must be a directory in /var/lib/mysql for each database.
#
# The MySQL user specified should have global SELECT and LOCK TABLES privileges.

USER=$1
PASS=$2

if [ -z "$USER" -o -z "$PASS" ] ; then
  echo "Usage: $0 <mysql user> <mysql password>"
  exit
fi

DATABASES=`find /var/lib/mysql -mindepth 1 -type d ! -name ".*" -printf "%f " | sort`
if [ -n "${DATABASES}" ] ; then
	for DB in ${DATABASES}; do
		mysqldump --user=$USER \
		          --password=$PASS \
		          --add-drop-table \
		          --extended-insert \
		          --quote-names \
		          --add-locks \
		          --lock-tables \
		          ${DB} \
		          >${DB}.sql
		chmod 0600 ${DB}.sql
	done
fi
