Go Back   Cloud Computing > Support > Linux Server Hosting
 

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 02-20-09, 08:38
BOD Member
 
Join Date: Jan 2009
Posts: 73
Default cron job to send a database backup to gmail account

I have this setup on one of my servers. It is a lifesaver.
First thing to do is to make a bash script.
In the bash script was the following code:

Code:
#!/bin/sh
######
# first make a mysqldump of all databases, and tar and zip it
######
mysqldump --add-drop-table -u user_name -p password --all-databases | tar -vxzf > backup.sql.tar.gz
######
# next email it
# The echo at the start of the last line adds a blank into the body of the 
# mail being set out.
######
echo | mutt -a backup.sql.tar.gz -s “daily backup of database” email@gmail.com
####
# finally, delete the backup from your server
####
rm backup.sql.gz
Secondly chmod +x the script to make it executable,
next, just add a daily cron job to call the script and that's it.
Reply With Quote
  #2 (permalink)  
Old 02-20-09, 08:45
BOD Member
 
Join Date: Jan 2009
Posts: 73
Default

Just to add that you can also put the following line into the script to have it backup your directory structure and mail it to your gmail too

Code:
####
# backup the directory structure and send it in a separate email
####
tar -vxzf backup_dir.tar.gz /home/mysite/public_html
Code:
#####
# send a second email with the data structure as attachment
#####
echo | mutt -a backup_dir.tar.gz -s “daily backup of directory data” address@gmail.com

If you want one single email then you need to use the following line for the sending email part

Code:
####
# send a single email with both attachments
####
echo | mutt -a backup_dir.tar.gz -a backup.sql.gz -s “daily backup of directory data and database” address@gmail.com
also, remember to add an
Code:
####
# delete the data backup
####
rm backup_dir.tar.gz
at the bottom to remove the temp data.

Last edited by php_rules : 02-20-09 at 08:46. Reason: additions
Reply With Quote
  #3 (permalink)  
Old 02-20-09, 15:34
carl owen's Avatar
Super Moderator
 
Join Date: Nov 2008
Posts: 1,061
Smile

Thank you for this information. It is certainly a very good procedure as you will be able to schedule the database backup according to you. The updates of the database will be sent on your email address as per the cron job settings. If in case, the server fails and your database is lost you can recover the database as you will have it on your email address but the cron job settings plays a very important role in this and you should do it carefully.
Reply With Quote
  #4 (permalink)  
Old 02-25-09, 15:19
BOD Member
 
Join Date: Jan 2009
Posts: 73
Default

doh!! I just re-read this post - I put
Code:
tar -vxzf
that is to untar =|
it should read

Code:
tar -cvzf
to create the tar files!!
Reply With Quote
  #5 (permalink)  
Old 03-19-10, 08:32
BOD Member
 
Join Date: Jan 2010
Posts: 37
Default

So this backs up to your gmail email account? How do you then access it, to get a copy back, to use as a back up?

I'm just curious as to what format this works as-does it send as a file attachment?
Reply With Quote
  #6 (permalink)  
Old 07-20-10, 12:27
BOD Member
 
Join Date: Jun 2010
Posts: 50
Default

This looks like a neat little script. Like Frankie, I'm curious as to whether the backup file will be sent as an attachment or as the body of the email. Either way, good work!
Reply With Quote
  #7 (permalink)  
Old 07-22-10, 20:41
carl owen's Avatar
Super Moderator
 
Join Date: Nov 2008
Posts: 1,061
Default

Quote:
Originally Posted by Frankie View Post
So this backs up to your gmail email account? How do you then access it, to get a copy back, to use as a back up?

I'm just curious as to what format this works as-does it send as a file attachment?
Yes, the backup is sent to your Gmail account. The backup is sent as an attachment in tar.gz format which can be used if required.

Quote:
Originally Posted by Skipper View Post
I'm curious as to whether the backup file will be sent as an attachment or as the body of the email. Either way, good work!
The backup will be sent as an attachment in tar.gz format.

Hope I have answered all the questions
Reply With Quote
  #8 (permalink)  
Old 07-23-10, 10:20
BOD Member
 
Join Date: Jun 2010
Posts: 50
Default

Yep, that's the only question I had. I am wondering if anyone here can verify that this works (someone who has tried it themselves and had good results) before I set this up with my databases.
Reply With Quote
  #9 (permalink)  
Old 07-23-10, 16:23
carl owen's Avatar
Super Moderator
 
Join Date: Nov 2008
Posts: 1,061
Default

Quote:
Originally Posted by Skipper View Post
Yep, that's the only question I had. I am wondering if anyone here can verify that this works (someone who has tried it themselves and had good results) before I set this up with my databases.
Well, we do have customers who have tried this and it has worked for them but I doubt if they will share their experience here ;)

You can try this and see if it works for you.

All the best!!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump


All times are GMT -6. The time now is 20:07.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
Copyright © 1999-2012, BODHost Ltd. All rights reserved.