Jump to content

Cron Job


Recommended Posts

Is there a tutorial or something that describes to a newbie how to setup a cron job? The documentation doesnt really say anything other than "You need to have a good knowledge of Linux commands before you can use cron jobs effectively" which is not much help to me :(

 

Or, could someone just describe to me the command i need to input in order to backup my forum database every night? Also, would the backups overwrite eachother, or would they be named differently each time, and I would have to delete them every now and then to conserve web space? thanks.

Link to comment
Share on other sites

This is what I use to backup my MT database nightly, as well as my Textpattern database:

 

>mysqldump -u cpanelname_dbusername -ppassworrd cpanelname_dbname | gzip -c > /home/cpanelname/backups/backup-txp-demlatest.sql.gz

 

rundown:

 

cpanelname = your login name for cpanel

password = your password for the dabase (note, there is NO space between -p and password)

dbname = the name of your database

 

 

I place mine BEFORE the public_html directory as I do not want this to be publically accessible. You can call it whatever you want (replace backup-txp-demlatest, leave the .sql.gz). This will not increment numbers, it will replace the old file every time the backup runs.

 

Does that help?

Edited by TCH-Lisa
Link to comment
Share on other sites

Question: how do I know what the name of my cpanel is? On the masthead, is a chick with a laptop and the logo says cPanel X; right below it on the left its says in small print cpanel 8.

 

Um.. so.. what's the name? X? 8? :)

 

help,

!!blue

 

**SRATCH THAT***

Let's assume the following:

cpanelname = your login name for cpanel > BLUE

password = your password for the dabase (note, there is NO space between -p and password) > CRAYON

dbname = the name of your database > BIGDB

dbusername = login name for the database (?) > BLUEinDB

 

so assuming that, is this correct?

 

mysqldump -u BLUE_ BLUEinDB -pCRAYON BLUE_BIGDB | gzip -c > /home/BLUE/backups/backup-txp-demlatest.sql.gz

 

mysqldump -u cpanelname_dbusername -ppassworrd cpanelname_dbname | gzip -c > /home/cpanelname/backups/backup-txp-demlatest.sql.gz

Edited by !!blue
Link to comment
Share on other sites

  • 3 weeks later...
This is what I use to backup my MT database nightly, as well as my Textpattern database:

 

>mysqldump -u cpanelname_dbusername -ppassworrd cpanelname_dbname | gzip -c > /home/cpanelname/backups/backup-txp-demlatest.sql.gz

 

rundown:

 

cpanelname = your login name for cpanel

password = your password for the dabase (note, there is NO space between -p and password)

dbname = the name of your database

 

 

I place mine BEFORE the public_html directory as I do not want this to be publically accessible.  You can call it whatever you want (replace backup-txp-demlatest, leave the .sql.gz).  This will not increment numbers, it will replace the old file every time the backup runs.

 

Does that help?

This is the first time I'm going to be trying to do a cron job backup of my blog's DB. In fact, it'll be the first time I've used a cron job to do anything! I need a bit of clarification as to what the instruction is actually saying...

 

Also, the portion of the command line that says

>-ppassworrd

Is that correct? Are there supposed to be two letter Rs in there?

 

Finally, there's a line in the Cron Job manager that says

Please enter an email address where the cron output will be sent:

I hate to sound stupid about this, but this is not where the backup will be saved, is it? From the way I'm reading it, the cron daemon will perform the backup and send the resultant gzipped backup file to whatever email address I specify. Am I understanding it correctly? If so, how do I make sure the backup stays in my web space, as well as being emailed to me?

 

Sorry for all the newbie questions. I know they can get frustrating at times, especially if you're really experienced at this stuff!

Link to comment
Share on other sites

Normally I can spell password.

 

Actually, however, that word password needs to be replaced with your password. No space between -p and the password. So if your password was telephone, it'd be:

 

-ptelephone.

 

Clearer?

 

And the email addresses is where cron will send failure notices. The backup is saved to the location you specify in the cron job according to the line I gave.

 

Does that help? =)

Link to comment
Share on other sites

All I can say is "duuuuuuh" to myself. If I had even thought about what your post said instead of immediately thinking of questions, I would have not even needed to ask the question about -ppassword because I would have understood to type it as -phereismypassword. Silly me.

 

Yes, Lisa, thank you for clarifying for me what the email address is for. It makes much more sense now. Boy, I'm just learning so much about managing a web site. Very, very cool.

 

Thanks for 'splainin'! :)

Link to comment
Share on other sites

As a new question that follows very closely to this topic, I want to do nightly backups of my site using the cron daemon. The subject of nightly backups has been discussed here: http://www.totalchoicehosting.com/forums/i...?showtopic=7787.

 

I want to do the same thing, with one change...I don't have a remote FTP server, so I'd just like the backups stored in my home directory rather than being FTPed to a remote server.

 

How can I do this? Is there a cron command similar to the one used by Lisa to backup SQL databases that will perform a backup of the entire web site?

 

Thanks.

Link to comment
Share on other sites

I haven't looked at the script yet, but remember one thing:

 

Unless you exempt ONE directory in your home area from that backup, the size of that nightly backup is going to increase exponentially for each time it runs ;)

Link to comment
Share on other sites

Unless you exempt ONE directory in your home area from that backup, the size of that nightly backup is going to increase exponentially for each time it runs ;)

Excellent point. Someone will have to tell me how to exempt one directory from the backup process! ;)

Link to comment
Share on other sites

  • 4 weeks later...

OK. It's been about a month, and I'm just now getting around to doing site backups. Here's what I want to do.

 

(1) Perform weekly backup of my entire site, including my main directory, a subdomain that holds my blog and any MySQL databases I use.

(2) I'm already doing a nightly cronjob backup of my MT database as previously outlined in this forum (using the exact command that TCH-Lisa gave), so backing up my MT database is not an issue.

(3) Clearly, as Annie has pointed out, I'd need to exclude whatever directory is used to store the backups from the actual backup process. Otherwise, my backup file will grow exponentially each week.

 

Can anyone help walk me through this? I've looked at the cPanel page for performing backups, and I'm a bit confused. I've asked questions about cPanel's backup process before, but for some reason, the logic is not sticking in my brain.

Link to comment
Share on other sites

If you are only doing it once a week, why not just do it manually and copy it to your machine when done. That's what I do. Only takes a second to start it. I'm in cPanel everyday anyway checking stats and what not. It sends an email when complete and I fire up the FTP client and download it.

 

Just seems like a waste of time to run a cron job for a weekly backup. If you aren't downloading them and the server craps out the backup would not be accessible.

Link to comment
Share on other sites

Thanks Lisa, we are currently running a helpdesk database on the servers and a backup is very important for us.

 

This may be a tip for others;

 

We are running 8 backup scripts, one for each day of the week, and also one script that runs every two hours.

 

So, if we realize on a monday morning that something has gone wrong on Saturday, we can always go back to the backup made on saturday morning, and not the 'empty backup' made on the Sunday or Monday morning.

 

Also, if we realize straight away that something has gone wrong, we can always use the bi-hourly backup, with a maximum loss of two hours of data.

Link to comment
Share on other sites

SlitheryImp,

 

I take it you're not doing a standard cron mysql dump? You said you were running scripts which makes me think it's not the mysql dump.

 

I like the way you do a different backup each day. Does that mean that Monday's backup won't overwrite Sunday's backup? Also, does that mean that if you have a Monday backup on the 1st of the month, then the next Monday backup on the 8th of the month will overwrite that from the first of the month?

Link to comment
Share on other sites

kaseytraeger,

 

Sorry, I was a little unclear on that, I'm just running the code that TCH-Lisa posted. (the mysqldump)

 

Just change the filename of the backup, ie.

 

/home/cpanelname/backups/backup-monday.sql.gz

/home/cpanelname/backups/backup-tuesday.sql.gz

/home/cpanelname/backups/backup-wednesday.sql.gz etc.

 

(also a backup-bi-hourly.sql.gz)

 

And let them run on the corresponding day in Cron

 

So the backup made on Monday will not overwrite the Sunday backup, but it will overwrite the backup made on the previous Monday. This way, I will (hopefully) always have a weeks worth of backups.

Link to comment
Share on other sites

You can also use bits from my code:

 

$today = mktime(0,0,0,date("m"),date("d")-1,date("Y"));

$today = strftime("%d%b%Y",$today);

 

Then call it by including this $thatday in the file name that gets saved.

 

I'm using the code for log backup, but I'm sure you could hack something for different tasks. Click on the smiley to see the whole thing I'm using:

 

:lol:

Link to comment
Share on other sites

  • 3 weeks later...

Thank you for the very informative page, Annie. I need to read through it and see if I can make sense out of it. That's always a big issue for me when learning something new and foreign!

 

I hope you don't mind if I ask a few questions if something's unclear to me.

Link to comment
Share on other sites

  • 1 month later...

Here's a question about this - (mysql cron script works beautifully, Lisa, thanks for posting!) -

 

I have a hosting account on server 74, one on server 75, and one on server 78. I'd like to save my backup from server 74 to server 75, save 75 to 78, and save 78 to 74. Since I have all the passwords and usernames to all of them, can I do that via a cron job, or would I have to do it manually - download the logs and then uploading them to the other server?

Link to comment
Share on other sites

Greetings all; very helpful thread. Thanks to everyone.

 

[Edited for clarity]

 

I'm working on doing backups done of my site. I don't want to back up the whole site; nor do I want to back up my entire database. It will be a five-step process for my daily backups:

 

1) Dump several tables from my database into a .SQL file in my backups directory (Done, using a PHP file)

2) Copy several custom PHP files from my site to that same backup directory (Done, using a PHP file)

3) Combine all of the files created in #1 and #2 into a TAR file (PROBLEM)

4) Gzip that TAR file (Unstarted)

5) FTP it off-site (Unstarted)

 

I'm having a problem with step 3, TAR'ing all of the files in my backup directory. The PHP file that I created to TAR the 8 or 9 files in this directory is named backup.php, and it consists of:

 

><?php

// date & timestamp file
// commented out until I figure out what's wrong with my tar syntax
// $filename="/".date("Y-m-d_Hi", mktime())."_backup";

// create .TAR file
exec("tar -cvf mybackup.tar /public_html/backups"); // tarring everything in the directory

?>

 

When I point my browser to www/backups/backup.php, it executes, but the "testbackup.tar" file that is created is a 10240-byte file filled with NULL, and which (according to 7-Zip file manager) is not a valid archive.

 

Plus, I'm backing up about 120k worth of files (PHP and SQL dump files), so I know that a 10k file isn't right.

 

Can anyone shed some insight into what I may be doing wrong?

 

Thanks in advance for any help. I hope this makes sense. :)

 

Syenna

Link to comment
Share on other sites

  • 2 weeks later...

Just been reading about the cron job for backing up. Im wondering if I was to excute a cron job for a daily back up if there was another cron job to trash previous backups so as to maintain diskspace?

 

Wayne Rock Sign

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...