Web Hosting Talk







View Full Version : MySQL Backup?


Dim8
06-01-2001, 03:43 PM
Hello.

Not sure if this is the right forum to ask this, but i'll give it a try.

I'm looking for a script that will run on cron and will make a backup copy of all my MySQL databases and possibly email it to me? Possible zip em up or tar em up and send it to me?

Any help is appreciated.

(SH)Saeed
06-01-2001, 03:55 PM
This question has been asked plenty of times before. I suggest you do a search for "mysql backup".

VetteMan
06-03-2001, 09:02 AM
Here is a script that I use to back up MySQL, I think this is exactly what you are looking for, it will compress and email it to you if you want. You will need to change some of the variables:

#!/bin/sh

# List all of the MySQL databases that you want to backup in here,
# each seperated by a space
databases="name of your database"
# Directory where you want the backup files to be placed
backupdir=/path/of/directory

# MySQL dump command, use the full path name here
mysqldumpcmd=mysqldump

# MySQL Username and password
userpassword=" --user=your username --password=your password"

# MySQL dump options
dumpoptions=" --quick --add-drop-table --add-locks --extended-insert --lock-tables"

# Unix Commands
gzip=/bin/gzip
uuencode=/usr/bin/uuencode
mail=/usr/sbin/sendmail

# Send Backup? Would you like the backup emailed to you?
# Set to "y" if you do
sendbackup="y"
subject="Your MySQL Backup"
mailto="you@yourdomain.com"

# Create our backup directory if not already there
mkdir -p ${backupdir}
if [ ! -d ${backupdir} ]
then
echo "Not a directory: ${backupdir}"
exit 1
fi

# Dump all of our databases
echo "Dumping MySQL Database"
for database in $databases
do
$mysqldumpcmd $userpassword $dumpoptions $database > ${backupdir}/${database}.dump
done

# Compress all of our backup files
echo "Compressing Dump File"
for database in $databases
do
rm -f ${backupdir}/${database}.dump.gz
$gzip ${backupdir}/${database}.dump
done

# Send the backups via email
if [ $sendbackup = "y" ]
then
for database in $databases
do
$uuencode ${backupdir}/${database}.dump.gz > ${backupdir}/${database}.dump.gz.uu
$mail -s "$subject : $database" $mailto < ${backupdir}/${database}.dump.gz.uu
done
fi


# And we're done
ls -l ${backupdir}
echo "Dump Successful and Complete!"
exit