Help - Search - Members - Calendar
Full Version: Sharing: This script create a backup of mysql db, store on disk, and s
Hostony Board > General Support > PHP/MySQL
MartinB
Sharing: This script create a backup of mysql db, store on disk, and send it to an email account, Gmail for example:

Hi, I'm using this script for more than a year and I think it can be usefull for you too, so, here it is:


CODE
#!/bin/bash
# This script runs as a nightly cron job to back a MySQL database to a Gmail account

# Prints date of the format MM/DD/YY
TODAY1=`date +%D`

# Prints date of the format MMDDYYYY
TODAY2=`date +%m%d%Y`

# The email address to which we're sending backups
ADDRESS=x@gmail.com

# Local directory where we'll be doing work and keeping copies of all archived files

BACKUPDIR=/home/username/backups

# MySQL username and password

MYSQLUSER=username_dbname
MYSQLPASS=password

# MySQL database to backup (or --all-databases to backup everything)

#DATABASE="--all-databases"
DATABASE=username_dbname

# Name of backup file (used for plaintext and gzipped files)

FILENAME="mysql_backup_"

# Change to the backup directory (so absolute directories aren't included in the archive)

cd $BACKUPDIR

# Dump the MySQL databases to a plaintext file

mysqldump -u $MYSQLUSER -p$MYSQLPASS $DATABASE > $FILENAME$TODAY2

# Package (tar) and compress (gzip) the plaintext MySQL dump
# Append the date (MMDDYYYY) to the filename

tar czf $FILENAME$TODAY2.tar.gz $FILENAME$TODAY2
rm $FILENAME$TODAY2

# Mail the resulting archive to our Gmail account
# Body of email is the output from the date command at the exact time the cron job runs
# Subject is "$FILENAME MM/DD/YY"

date | mutt -s "$FILENAME ($TODAY1)" -a $FILENAME$TODAY2.tar.gz $ADDRESS


enjoy.

(obviously: not for so long databases, remember than the dump compressed must be under 10mb or so. but try it, the gzip do miracles with text files wink.gif )
MarkM
Nice!!! It works - It really works! blink.gif laugh.gif smile.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2024 Invision Power Services, Inc.
IPS Driver Error

IPS Driver Error

There appears to be an error with the database.
You can try to refresh the page by clicking here