Author Topic: automatic mysql backups  (Read 4050 times)

Rooftop

  • Inner Core
  • Hero Member
  • *
  • Posts: 1915
    • View Profile
automatic mysql backups
« on: December 03, 2013, 12:39:51 PM »
We have nightly mysql backups anyway.  I am just not willing to guarantee these for clients, as they are out or our control.  I was looking for a 3rd party service that can do a nightly or weekly remote mysql backup. Hands off subscription type affair. 

I can see that there are a few around. Anyone use any / have feedback?

littleman

  • Administrator
  • Hero Member
  • *****
  • Posts: 6558
    • View Profile
Re: automatic mysql backups
« Reply #1 on: December 04, 2013, 02:06:43 AM »
Here's a shell script that could be used with cron.  It has worked for me.
Quote
#! /bin/bash

# Ameir Abdeldayem
# http://www.ameir.net
# You are free to modify and distribute this code,
# so long as you keep my name and URL in it.
# Last modified: August 1, 2006
#-------------------------------------------------------------

# your MySQL server's name
SERVER=

# directory to backup to
BACKDIR=~/backups

# date format that is appended to filename
DATE=`date +'%m-%d-%Y'`

#----------------------MySQL Settings--------------------#

# your MySQL server's location (IP address is best)
HOST=localhost

# MySQL username
USER=

# MySQL password
PASS=

# List all of the MySQL databases that you want to backup in here,
# each seperated by a space
DBS=""

# set to 'y' if you want to backup all your databases. this will override
# the database selection above.
DUMPALL=y

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



#----------------------Mail Settings--------------------#

# set to 'y' if you'd like to be emailed the backup (requires mutt)
MAIL=y

# email addresses to send backups to, separated by a space
EMAILS=""

SUBJECT="MySQL backup on $SERVER ($DATE)"

#----------------------FTP Settings--------------------#

# set "FTP=y" if you want to enable FTP backups
FTP=n

# FTP server settings; should be self-explanatory
FTPHOST="ftp.server.com"
FTPUSER="user"
FTPPASS="password"

# directory to backup to. if it doesn't exist, file will be uploaded to
# first logged-in directory
FTPDIR="backups"

#-------------------Deletion Settings-------------------#

# delete old files?
DELETE=y

# how many days of backups do you want to keep?
DAYS=5

#----------------------End of Settings------------------#

# check of the backup directory exists
# if not, create it
if  [ -e $BACKDIR ]
then
   echo Backups directory already exists
else
   mkdir $BACKDIR
fi

if  [ $DUMPALL = "y" ]
then
   echo Dumping all your databases...
   mysqldump -h $HOST --user=$USER --password=$PASS $OPTIONS --all-databases > \
$BACKDIR/$SERVER-mysqlbackup-ALL-$DATE.sql
   gzip -f -9 $BACKDIR/$SERVER-mysqlbackup-ALL-$DATE.sql
else
   echo Backing up MySQL databases...
for database in $DBS
do
   mysqldump -h $HOST --user=$USER --password=$PASS $database > \
$BACKDIR/$SERVER-mysqlbackup-$database-$DATE.sql
   gzip -f -9 $BACKDIR/$SERVER-mysqlbackup-$database-$DATE.sql
done
fi

# if you have the mail program 'mutt' installed on
# your server, this script will have mutt attach the backup
# and send it to the email addresses in $EMAILS

if  [ $MAIL = "y" ]
then
BODY="Your backup is ready! Find more useful scripts and info at http://www.ameir.net"
ATTACH=`for file in $BACKDIR/*$DATE.sql.gz; do echo -n "-a ${file} ";  done`

   echo "$BODY" | mutt -s "$SUBJECT" $ATTACH $EMAILS
       
   echo "Your backup has been emailed to you!"
fi

if  [ $FTP = "y" ]
then
cd $BACKDIR
ATTACH=`for file in *$DATE.sql.gz; do echo -n -e "put ${file}\n"; done`

   ftp -nv <<EOF
   open $FTPHOST
   user $FTPUSER $FTPPASS
   cd $FTPDIR
   $ATTACH
   quit
EOF
fi

if  [ $DELETE = "y" ]
then
   find $BACKDIR -name "*.sql.gz" -mtime $DAYS -exec rm {} \;

   if  [ $DAYS = "1" ]
   then
      echo "Yesterday's backup has been deleted"
   else
      echo "The backup from $DAYS days ago has been deleted"
   fi
fi

echo Your backup is complete!




ergophobe

  • Inner Core
  • Hero Member
  • *
  • Posts: 9324
    • View Profile
Re: automatic mysql backups
« Reply #2 on: December 04, 2013, 03:14:31 AM »
That's a lot more full-featured than the one I cobbled together myself, but I use this as a cron job.

Quote
#!/bin/bash
DATESTAMP=`date +%Y%m%d`
mysqldump --opt -h 127.0.0.1 -u <username> -p<password> <dbname>  | gzip -c > <filename>.gz
mutt -a <filename>.gz -s "Sitename DB Backup on $DATESTAMP" <user>@gmail.com < <body_text_filename>.txt

If you have a cheap server somewhere, rather than email, you can just do it with rsync, which I've also done (for a small DB I actually like the GMail option better because you have a complete archive just sitting there as attachments). I haven't used this place, but I keep thinking they look like a good deal
http://www.rsync.net/products/pricing.html

Rooftop

  • Inner Core
  • Hero Member
  • *
  • Posts: 1915
    • View Profile
Re: automatic mysql backups
« Reply #3 on: December 04, 2013, 11:21:47 AM »
Brilliant - thanks both.