Help - Search - Members - Calendar
Full Version: Automatic Mysql backups sended to mail address.
Hostony Board > General Support > PHP/MySQL
MartinB
Hello dears:

I have setted an script to backup a mysql database and send it to my gmail account.
(Surely not for big databases.)

Maybe it's usefull for somebody here:

CODE
#!/bin/bash
# This script runs as a nightly cron job to back a MySQL database to an email 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=your@email.com

# Local directory where we'll be doing work and keeping copies of all archived files
BACKUPDIR=/home/account/backups

# MySQL username and password
MYSQLUSER=account_user

MYSQLPASS=the_password_for_the_database_user

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


# 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



then you can go to your control panel and setup a cron job to run it daily.
(I use it with a little <1mb database)

If you have big databases you can modify it to send the tar.gz file using scp or ftp to another server.

enjoy.
smokie
Hi thanks for the script, however I'm using a different script, it works well calling via browser, but not if I set up a cron job, get permission denied and various other errors, whats the correct command, is it /usr/local/bin/php still ?? or has it changed ref php module now.

Smokie
MartinB
I use a bash script, not php script.

but for my php scripts on cron jobs I just add:

CODE
#!/usr/local/bin/php -q
<?php


to the beginning of the file, and run it like a command
eg. "/home/account/script.php"


And for pipes on email forwards i use just "| php -q /path/to/script.php"

hope some of them help.
smokie
Thanks for the help, have tried but still get "permission denied" email., it runs fine if called via browser
Regards
Smokie
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