LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-28-2010, 08:19 PM   #1
struct
Member
 
Registered: Feb 2009
Posts: 68

Rep: Reputation: 10
crontab - need help making it run at 2:30am everyday.


I'm trying to back up my database daily at 2:30am. is this the right format?

30 2 * * * mysqldump -u root -pPassword database > backup_$(date +%y%m%d).sql

please let me know.
Thanks guys!

Last edited by struct; 07-28-2010 at 08:20 PM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 07-28-2010, 08:58 PM   #2
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Ubuntu MATE, Mageia, and whatever VMs I happen to be playing with
Posts: 19,311
Blog Entries: 28

Rep: Reputation: 6137Reputation: 6137Reputation: 6137Reputation: 6137Reputation: 6137Reputation: 6137Reputation: 6137Reputation: 6137Reputation: 6137Reputation: 6137Reputation: 6137
I would suggest writing a bash script to back up the database, then using cron to run the script.

With a script, you can create a new directory to receive the backup, give the backup a specific name not duplicating any other file name, and lots of other neat stuff.

Once I got that working, it worked perfectly until I moved my website out to GoDaddy. I haven't tried scripting on GoDaddy.

You can read the details and see the script on my blog.

I've never tried to enter variables in cron, so I'm not competent to comment on that.
 
Old 07-28-2010, 10:18 PM   #3
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,973

Rep: Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623
There are also some weird things about cron. Seems like you also have to watch it's environment like it's shell and some other stuff.
 
0 members found this post helpful.
Old 07-29-2010, 12:43 AM   #4
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 707

Rep: Reputation: 55
Quote:
Originally Posted by struct View Post
I'm trying to back up my database daily at 2:30am. is this the right format?

30 2 * * * mysqldump -u root -pPassword database > backup_$(date +%y%m%d).sql

please let me know.
Thanks guys!
Hello struct,

Have you every tried zmanda mysql backup...Its very easy Just have a look
here
 
Old 07-29-2010, 01:14 AM   #5
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
No answer to the actual question, but I strongly suggest that you don't use the mysql root user for this. Reason is that you have the password in the clear in your cron (and/or script).

I suggest that you create a mysql user who basically has read-only access to the databases (tables etc); so no delete, update, insert etc permissions.
 
2 members found this post helpful.
Old 07-29-2010, 03:17 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
I second the suggestion by Wim Sturkenboom. In addition, if you want to run the mysqldump command directly from the crontab, better to use absolute paths both for the command and for the output file (as previously mentioned, the crontab environment is very limited, e.g PATH is usually /bin:/usr/bin. Also cron jobs are executed from the user's home directory: to assure that every file is created in the place you want it to be, better specify the full path or at least use the $HOME environment variable which is available to cron jobs).

Finally, take in mind that the % sign has a special meaning in crontab (please see man 5 crontab for details). To get it literally you have to escape using backslash, as in +\%y\%m\%d.
 
1 members found this post helpful.
Old 07-29-2010, 11:59 AM   #7
struct
Member
 
Registered: Feb 2009
Posts: 68

Original Poster
Rep: Reputation: 10
You guys are great.
frankbell, I followed the link to the shell script. Thank you for the link. I'll find out tomorrow morning if it worked.
Probably the variables I put in place in the crontab messed up the command so it wasn't running at 2:30am like I expected. I didn't know the limitations of the cron.

As for the user that I put in the crontab only had select access to the database. If anyone has root crontab access would have access to the shell script with user/pass... so I didnt' think too much of it. But I'll try to hide it better

vinaytp, I'll put the utility for the future systems. It looks very helpful and handy to have.

Thanks to everyone who gave me the insight and valuable knowledge on this subject.
 
Old 07-30-2010, 10:52 PM   #8
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Ubuntu MATE, Mageia, and whatever VMs I happen to be playing with
Posts: 19,311
Blog Entries: 28

Rep: Reputation: 6137Reputation: 6137Reputation: 6137Reputation: 6137Reputation: 6137Reputation: 6137Reputation: 6137Reputation: 6137Reputation: 6137Reputation: 6137Reputation: 6137
Quote:
Originally Posted by struct View Post
You guys are great.
frankbell, I followed the link to the shell script. Thank you for the link. I'll find out tomorrow morning if it worked.
Probably the variables I put in place in the crontab messed up the command so it wasn't running at 2:30am like I expected. I didn't know the limitations of the cron.
I agree with you, the folks at LQ are great. It was years between when I registered here and when I started trying to answer, rather than ask, questions. But I am really glad to have found this place.

About the script:

It took me a week to make the script work for me, because, to tell the truth, it was the first time I messed with variables in scripting. I am not a programmer by any stretch . . . .

The box in question was dedicated as a webserver and had nothing on it other than files related to the website, plus a firewall and an anti-virus, so security was a concern, but not an obsession; all a bad guy would discover is the backstory to stuff he could already read and I'm such a small fry that I'm hardly a target for someone who is looking to take over the world.

I'll give you a for instance: I'm an Opera browser fanatic, and I never even bothered to install Opera on it.

The suggestion not to use the root mysql user to run the backup is an excellent one. Out of exhaustion after all the testing, plus the reasons above, I didn't use a more restricted MySQL user to run the backup, but, if I were doing it again, I would.
 
Old 07-31-2010, 02:16 AM   #9
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
If its' solved, please mark the thread as 'solved' using the thread tools just above the first post.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
My BASH script work perfect when i run it manualy, but not when it run in the crontab roqarg Linux - Newbie 30 05-06-2010 01:37 PM
[SOLVED] Difference between manual run and crontab run? EnderX Linux - Newbie 3 01-15-2010 03:46 PM
How can I make a C program or a UNIX script run once everyday? beckettisdogg Linux - Newbie 1 09-01-2009 08:18 PM
making a crontab xushi Slackware 5 05-08-2005 11:34 PM
How to run java class everyday automatic?? karleong Linux - Newbie 5 07-15-2003 07:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration