
12-07-2005, 08:41 PM
|
|
<?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.
|

12-08-2005, 02:59 PM
|
|
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
?
|

12-08-2005, 03:17 PM
|
|
<?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.
|

12-19-2005, 09:30 AM
|
|
Aspiring Evangelist
|
|
Join Date: Jun 2005
Location: Internet
Posts: 448
|
|
thanks, nice tip  will try it out.
|

01-17-2006, 04:09 AM
|
|
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.
|

01-17-2006, 04:51 AM
|
|
<?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.
|

01-17-2006, 06:32 AM
|
|
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.
|

01-17-2006, 07:10 AM
|
|
<?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.
|

02-03-2006, 10:36 PM
|
|
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?
|

02-04-2006, 03:10 AM
|
|
<?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.
|

02-14-2006, 12:34 AM
|
|
Newbie
|
|
Join Date: Aug 2005
Posts: 17
|
|
thanks for this nice script
|

04-06-2006, 03:58 PM
|
|
Junior Guru Wannabe
|
|
Join Date: Nov 2005
Posts: 96
|
|
where do I input my password for mysql?
|

04-06-2006, 04:02 PM
|
|
<?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
|

04-06-2006, 04:06 PM
|
|
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!
|

04-06-2006, 04:14 PM
|
|
Junior Guru Wannabe
|
|
Join Date: Nov 2005
Posts: 96
|
|
one last question,
what command would I add to .qz the databases?
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
| 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
HTML code is Off
|
|
|
|
|
|
| Login: |
|
|
| Advertisement: |
|
|
| Web Hosting News: |
|
|
|