hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Hosting Security and Technology : Hosting Security and Technology Tutorials : How to Backup SQL DB's via Cron!
Reply

Hosting Security and Technology Tutorials Tutorials related to server security or the like.
Forum Jump

How to Backup SQL DB's via Cron!

Reply Post New Thread In Hosting Security and Technology Tutorials Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 12-07-2005, 08:41 PM
linux-tech linux-tech is offline
<?require_once("life")?>
 
Join Date: Sep 2002
Location: inside your network
Posts: 9,548

How to Backup SQL DB's via Cron!


Not sure if this one would be better off here or in the programming forums, moreso here I'd say as it's a Server related thing (all server dbs). Here's the idea and what this will do though.

This will take all of your sql databases, perform a "dump" on them, put them into the appropriate .sql format, and even create a "drop" command if the table exists. Perfect for moving to another host, repairing a db, or just routine backups

First, the script:
Code:
DATE=`date +%m%d%y`
THISMONTH=`date +%b-%y`
BACKMONTH=/backups/sql/$THISMONTH/
BACKDEST=$BACKMONTH/$DATE
if [ ! -d $BACKMONTH ];then
mkdir $BACKMONTH
fi
if [ ! -d $BACKDEST ];then
mkdir $BACKDEST
fi

for var in `find /var/lib/mysql/ -type d | \
sed -e "s/\/var\/lib\/mysql\///"`; do
mysqldump  --add-drop-table  $var >> $BACKDEST/$var.sql
done
Simple, and easy to use. Make SURE you change the BACKMONTH directory to something that exists, otherwise nothing will work properly!

Save that as any file, just remember the file name you called it.
Change the permissions on that script (chmod a+x script.sh)

Now, the cron entry:
Code:
0 0 * * * /path/to/script.sh
This will launch the backup @ midnight as necessary.

Reply With Quote


Sponsored Links
  #2  
Old 12-08-2005, 02:59 PM
sehe sehe is offline
Web Hosting Master
 
Join Date: Jun 2003
Posts: 962
what about removing sed and replacing
Code:
for var in `find /var/lib/mysql/ -type d | \                      
sed -e "s/\/var\/lib\/mysql\///"`; do
with
Code:
for var in `find /var/lib/mysql/ -type d -printf "%P\n"`; do
?

Reply With Quote
  #3  
Old 12-08-2005, 03:17 PM
linux-tech linux-tech is offline
<?require_once("life")?>
 
Join Date: Sep 2002
Location: inside your network
Posts: 9,548
I just posted what works for me here, and has been working for years.

Reply With Quote
Sponsored Links
  #4  
Old 12-19-2005, 09:30 AM
Cool Surfer Cool Surfer is offline
Aspiring Evangelist
 
Join Date: Jun 2005
Location: Internet
Posts: 448
thanks, nice tip will try it out.

Reply With Quote
  #5  
Old 01-17-2006, 04:09 AM
studiori.com studiori.com is offline
New Member
 
Join Date: Jan 2006
Posts: 4
Quote:
Originally Posted by linux-tech
...
Code:
0 0 * * * /path/to/script.sh
This will launch the backup @ midnight as necessary.
Mysql package come with ready to use script mysqlhotcopy which lock/copy/unlock selected databases very fast and may be scheduled via cron.

Reply With Quote
  #6  
Old 01-17-2006, 04:51 AM
linux-tech linux-tech is offline
<?require_once("life")?>
 
Join Date: Sep 2002
Location: inside your network
Posts: 9,548
Quote:
Originally Posted by studiori.com
Mysql package come with ready to use script mysqlhotcopy which lock/copy/unlock selected databases very fast and may be scheduled via cron.
While this is good (somewhat) for backing up sql to another host, or copying DBs to another host, it is no good for making actual backups, because this doesn't "dump" the db, it copies it, exactly as it is, in binary format, and can present a deal of problems with the mysql versions don't match on the hosts.

Reply With Quote
  #7  
Old 01-17-2006, 06:32 AM
studiori.com studiori.com is offline
New Member
 
Join Date: Jan 2006
Posts: 4
Quote:
Originally Posted by linux-tech
While this is good (somewhat) for backing up sql to another host, or copying DBs to another host, it is no good for making actual backups, because this doesn't "dump" the db, it copies it, exactly as it is, in binary format, and can present a deal of problems with the mysql versions don't match on the hosts.
It's true , but we want cron backups. Dump is more cpu sensitive than a copying especially if we have huge databases.

Reply With Quote
  #8  
Old 01-17-2006, 07:10 AM
linux-tech linux-tech is offline
<?require_once("life")?>
 
Join Date: Sep 2002
Location: inside your network
Posts: 9,548
Quote:
Dump is more cpu sensitive than a copying
Again, not so.
The same data has to be parsed either way, in fact with the extra stuff you're throwing into mysqlhotcopy (lock dbs, etc), you're creating MORE work for the system to do, thusly, creating more CPU intensity.

The downside of mysqlhotcopy, again is that it uses the BINARY data, meaning you're not going to save a lot on compression, whereas with the dump, which is a fully human readable text file (though don't edit it if you don't know what you're doing), it's possible to compress it down to next to nothing, saving even more on space.

While mysqlhotcopy may be a somewhat minor alternative, it's no real solution for backing things up. It was indended to copy db a to db b, and rename it. Your best way is always going to be going with the fully readable, compressable dump. Both will take close to the same time, neither will load your cpu incredibly (I do dumps of 150+m dbs nightly and notice no spike, whatsoever), the only difference is ease of use. Mysqldump adapts itself to the linux environment, mysqlhotcopy does not. Mysqldump reads and writes to a single file, hotcopy, again, the original binary data.

Reply With Quote
  #9  
Old 02-03-2006, 10:36 PM
redmalloc redmalloc is offline
Junior Guru Wannabe
 
Join Date: Oct 2005
Location: berkeley
Posts: 72
Quote:
Originally Posted by linux-tech
This will take all of your sql databases, perform a "dump" on them, put them into the appropriate .sql format, and even create a "drop" command if the table exists. Perfect for moving to another host, repairing a db, or just routine backups
Thanks for the script. It was exactly what I was looking for and didn't even know.

One comment, one question:

Comment: I notice that the script generates a double forward slash in the path in these lines:

Code:
BACKMONTH=/backups/sql/$THISMONTH/
BACKDEST=$BACKMONTH/$DATE
Makes:

Code:
BACKMONTH=/backups/sql/$THISMONTH//$DATE
Linux seems to handle this fine and I don't know if it is a typo i.e. does the double slash have a special meaning?

I changed mine to a single forward slash like so:
Code:
BACKMONTH=/backups/sql/$THISMONTH/
BACKDEST=$BACKMONTH$DATE
Question: Is there anything wrong with tar/gzip'ing up these files?

Reply With Quote
  #10  
Old 02-04-2006, 03:10 AM
linux-tech linux-tech is offline
<?require_once("life")?>
 
Join Date: Sep 2002
Location: inside your network
Posts: 9,548
Quote:
Question: Is there anything wrong with tar/gzip'ing up these files?
Not really, no. It's pretty simple to do if you want to, I just didn't add that in. I've got another that I'm working on and will post it here in the next few days when I can test.

Reply With Quote
  #11  
Old 02-14-2006, 12:34 AM
akucharski akucharski is offline
Newbie
 
Join Date: Aug 2005
Posts: 17
great quick script

thanks for this nice script

Reply With Quote
  #12  
Old 04-06-2006, 03:58 PM
stooley stooley is offline
Junior Guru Wannabe
 
Join Date: Nov 2005
Posts: 96
where do I input my password for mysql?

Reply With Quote
  #13  
Old 04-06-2006, 04:02 PM
linux-tech linux-tech is offline
<?require_once("life")?>
 
Join Date: Sep 2002
Location: inside your network
Posts: 9,548
Quote:
Originally Posted by stooley
where do I input my password for mysql?
As root, you should have something like .my.cnf setup . It should look something like this
Code:
[client]
user="root"
pass=""
This should sit in /root/.my.cnf and will authenticate you without the need for providing a password in the script

Reply With Quote
  #14  
Old 04-06-2006, 04:06 PM
stooley stooley is offline
Junior Guru Wannabe
 
Join Date: Nov 2005
Posts: 96
Quote:
Originally Posted by linux-tech
As root, you should have something like .my.cnf setup . It should look something like this
Code:
[client]
user="root"
pass=""
This should sit in /root/.my.cnf and will authenticate you without the need for providing a password in the script
that tis be the problem!

Thanks linux-tech!

Reply With Quote
  #15  
Old 04-06-2006, 04:14 PM
stooley stooley is offline
Junior Guru Wannabe
 
Join Date: Nov 2005
Posts: 96
one last question,

what command would I add to .qz the databases?

Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted
Jelastic Launches Version 1.9.1 of Java and PHP Hosting Platform Web Hosting News 2013-05-21 09:39:09
Malwarebytes Launches Data Scan-and-Backup Service Web Hosting News 2013-05-07 14:51:03
Sterling Data Storage Launches Three Cloud Backup Services Web Hosting News 2013-01-14 13:39:31
New BackupAgent Version Simplifies Channel Delivery of Cloud Backup Solution Web Hosting News 2012-06-26 16:57:11
Scaling a Profitable Backup Service with IASO's Johan Jongsma Web Hosting News 2012-02-15 18:10:40


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?