MySQL Backup Shell Script
Tagged:  •  
The following shell script will backup all mySql databases on the localhost to a directory and maintain 10 days worth of backups.
#!/bin/sh
# Copyright (C) 2003 Andrew Loree
# $Id: backupmysql.sh,v 1.2 2003/10/28 04:12:18 andy Exp $
############################################################################
#
# backupmysql.sh - Runs a mysqldump for all databases, and keeps backups
#		of 10 days
#
############################################################################
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#############################################################################
# Version History:
#   1.0  - Initial Release
#############################################################################

# Maintain backups for ten days
backup_dir=/var/backups/mysql
ext=bak
num_days=10

db_user=XXXXXX
db_pass=XXXXXX

# Generate filename in the form mysql-DD-Mon-YYYY.$ext
NOW=`date +%d-%b-%Y`
backup_file=$backup_dir/mysql-$NOW.$ext

# Delete backup file if it already exists
if [ -e $backup_file ]
then
	rm $backup_file
fi

# Backup using
/usr/local/bin/mysqldump --user=$db_user --password=$db_pass --all-databases --quick --result-file=$backup_file

# Delete old backups
i=0
for file in `/bin/ls -l1t $backup_dir/mysql-*.$ext`
do
	i=`expr $i + 1`
	if [ $i -gt $num_days ]
	then
		rm $file
	fi
done
AttachmentSize
backupmysql.sh1.82 KB