Computer Forums

Search Tech-Forums.net

Member Login

Remember Me? Sign Up! | Forgot Password
 
 
Reply
Old 01-20-2006, 06:12 AM   #1 (permalink)
 
Junior Techie
Join Date: Nov 2003
Posts: 58
Bullit
Default To use crontab to set mysql dbase backup

Hi there,
I have entered the world of cron jobs and am less than experienced!

I want to use crontab manager in my webhosting control panel to automate dbase backups...

I think this should work for a database backup:

In a db_backup.pl file: run from the crontab manager at a specific time.

/usr/bin/nice -20 /usr/bin/mysqldump -q --hdbhost.com --user=username --password=password dbname /usr/www/users/ourname/backup/database_name _`date "+%Y%m%d"`.sql ; cd /usr/www/users/ourname/backup ; /usr/bin/find *.sql -mtime +7 -delete
>/dev/null 2>&1

##comments###
# hostprovider want us to nice the task with val 10+
# -q to avoid exceeding memory lims for large tables
# host user and pass and dbname provided
# output to database_name with date.sql
# change dir to the backup folder
# delete any .sql older than 7 days
#If any output is produced by a command executed from a crontab, the cron daemon will normally email the user that output.
#thus >/dev/null 2>&1
#
#could change dir to a success_email.txt and send that email to me@myhost.com
#not sure of the syntax to do this
#how can we find out about errors?

I would value input from anyone who is familiar with this task.
I'd love a report emailed to me of success / fail etc

Cheers

database is mysql
Bullit is offline   Reply With Quote
Old 01-20-2006, 04:52 PM   #2 (permalink)
 
Junior Techie
Join Date: Nov 2005
Posts: 74
philg
Default

The > /dev/null 2>&1 at the end of the command send all output to stdout and stderr to /dev/null, deleting that bit means cron will catch all output including any error messages and email to the user running the cron job.

The rest of the command looks allright to me, just change the username, password, database name, file paths to suit your setup
__________________
www.pdgaskell.co.uk
philg is offline   Reply With Quote
Old 01-20-2006, 04:58 PM   #3 (permalink)
 
Junior Techie
Join Date: Nov 2003
Posts: 58
Bullit
Default

Thanks for the reply.

I put > /dev/null 2>&1 to switch off the email report, as I think it will go to my boss who's addess is registered for the account.
Plus wouldnt it send the finished .sql file too? I dont want that.

I would however like to send an email report to myself of success or fail. Would you know how to do that?
That'd be the icing on the cake for this project.
Bullit is offline   Reply With Quote
Old 01-21-2006, 08:36 AM   #4 (permalink)
 
Junior Techie
Join Date: Nov 2005
Posts: 74
philg
Default

No it wouldn't send the sql file by email.

You can set the MAILTO directive in the crontab to have output from cronjobs sent to you, or you can pipe the output of the command through mail and send it to you that way.

If you just want a Success or Failure in your email then you'll have to catch exit codes, normally programs should return 0 on a successful execution, you should check all your commands to make sure. example:

Code:
#!/bin/bash

EXIT_STATUS_TOTAL=0

command one
EXIT_STATUS_TOTAL=$[ $EXIT_STATUS_TOTAL + $? ]
command two
EXIT_STATUS_TOTAL=$[ $EXIT_STATUS_TOTAL + $? ]
command three
EXIT_STATUS_TOTAL=$[ $EXIT_STATUS_TOTAL + $? ]

if [ $EXIT_STATUS_TOTAL -eq 0 ]; then
    RESULT="Success"
else
    RESULT="Failure"
fi
echo $RESULT | mail -s "SQL Backup" you@somewhere.com
exit 0

__________________
www.pdgaskell.co.uk
philg is offline   Reply With Quote
Old 01-21-2006, 01:29 PM   #5 (permalink)
 
Junior Techie
Join Date: Nov 2003
Posts: 58
Bullit
Default

You've been a great help philg. Thanks for the input.
If i get this up and running its bonus points with the boss!
(Especially after nearly losing the whole client database on Monday... can you beleive we didn't have a backup. Nightmare!)

Thanks again.
Bullit is offline   Reply With Quote
Old 01-21-2006, 02:15 PM   #6 (permalink)
 
Junior Techie
Join Date: Nov 2005
Posts: 74
philg
Default

Your welcome
__________________
www.pdgaskell.co.uk
philg is offline   Reply With Quote
Old 01-28-2006, 05:44 PM   #7 (permalink)
 
Junior Techie
Join Date: Nov 2003
Posts: 58
Bullit
Default

My cron job is running well.
I'll post the code here soon - maybe it will help others.

Just one snag left to iron out on it...

When this line runs:
/usr/bin/mysqldump -q -hdbhost.com -uusername -ppassword dbname /usr/www/users/ourname/backup/database_name_`date "+%Y%m%d"`.sql ;

The result should be:
database_name_20060128.sql

but I get:

database_name_.sql

Any idea why?

I run the cron via crontab manager using the command:
sh cronjobfilename.sh

If anyone knows why my date function is not executing please give me a shout.
Thanks
Bullit is offline   Reply With Quote
Old 01-29-2006, 02:36 AM   #8 (permalink)
 
Junior Techie
Join Date: Nov 2005
Posts: 74
philg
Default

You probably need to specify the absolute path to date, eg on my system it is /bin/date
__________________
www.pdgaskell.co.uk
philg is offline   Reply With Quote
Old 01-29-2006, 04:50 AM   #9 (permalink)
 
Junior Techie
Join Date: Nov 2003
Posts: 58
Bullit
Default

so:
dbname /usr/www/users/ourname/backup/database_name_`/bin/date "+%Y%m%d"`.sql ;

should do it i guess?

will let you know.
ta once agin!
Bullit is offline   Reply With Quote
Old 02-08-2006, 03:31 AM   #10 (permalink)
 
Junior Techie
Join Date: Nov 2003
Posts: 58
Bullit
Default

Hi Philg,
The script is now creating the files, but I cant transfer / delete them in ftp software. When I telnet in and ls I see ?'s after each filename.
Do I have to close the files after mysqldumping to them or something?

Help appreciated.
Thanks


#!/bin/sh
/usr/bin/nice -20 /usr/local/bin/mysqldump -q -hhost.com -uusername -ppassword databasename > /usr/home/backup/databasename_`/bin/date +%Y%m%d`.sql
cd /usr/home/backup
/usr/bin/find *.sql -mtime +7 -delete
echo "Database backup complete. Please FTP in and collect resulting .sql files" | /usr/bin/mail -s "Database backup complete " me@me.com ;
Bullit is offline   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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On