LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 09-26-2008, 11:51 AM   #1
brokenhalo
LQ Newbie
 
Registered: Sep 2008
Distribution: CentOS 4.4 & 4.6
Posts: 12

Rep: Reputation: 0
Create a backup cron script


Hey,

I have a few web servers, and there is a backup cron setup in the web panel of my server to create a tarball for every website hosted on my server and save it locally to /home/backups every Saturday evening. I just installed and mounted an external hard drive to the /mnt/usbfantom directory. My goal here is to make a script that automatically copies everything from the /home/backups directory and save it to the /mnt/fantom directory on Sunday (the day AFTER the backups from the server are created) on a cron schedule. I am completely lost here. I found some similar threads, but nothing that I could figure out to make work with what I need. Thanks in advance for the help!
 
Old 09-26-2008, 11:59 AM   #2
trickykid
Guru
 
Registered: Jan 2001
Posts: 24,133

Rep: Reputation: 199Reputation: 199
You really wouldn't need to create a separate script, something this easy could be done within the crontab.

Example:

Code:
0 10 * * 7 /usr/bin/rsync -av /home/backups /mnt/usbfantom/ 1> /dev/null 2>&1
The above would copy only the new backups in /home/backups to /mnt/usbfantom/ every Sunday at 10AM.
 
Old 09-26-2008, 12:37 PM   #3
brokenhalo
LQ Newbie
 
Registered: Sep 2008
Distribution: CentOS 4.4 & 4.6
Posts: 12

Original Poster
Rep: Reputation: 0
...What do I do with this code? Where do I put it? Excuse my ignorance, I'm pretty new to linux. And what do you mean by "would copy only the new backups"? Do you mean only the backups (or files within the /home/backups directory) that have been changed will be copied? A new baackup is created for each domain regardless, so I'm pretty sure everything would be changed weekly, whether the website itself changed or not as it makes a new tarball with every cron backup.
 
Old 09-26-2008, 01:31 PM   #4
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,769

Rep: Reputation: 2614Reputation: 2614Reputation: 2614Reputation: 2614Reputation: 2614Reputation: 2614Reputation: 2614Reputation: 2614Reputation: 2614Reputation: 2614Reputation: 2614
Quote:
Originally Posted by brokenhalo View Post
...What do I do with this code? Where do I put it? Excuse my ignorance, I'm pretty new to linux. And what do you mean by "would copy only the new backups"? Do you mean only the backups (or files within the /home/backups directory) that have been changed will be copied? A new baackup is created for each domain regardless, so I'm pretty sure everything would be changed weekly, whether the website itself changed or not as it makes a new tarball with every cron backup.
Yep, rsync would just keep what's changed, 'in sync' with what's elsewhere. You specify the 'master', and where to copy it. That's a pretty slick cron entry too, BTW, trickykid.

To put this in your cron file, brokenhalo, first SU to root. Then, make sure your default editor is VI, by typing in "export EDITOR=vi", then type in "crontab -e". This will bring up your cron (job scheduler) file. If you don't have one, it will create a new one for you. Put the entry in as shown, but modify it as needed. This page

http://www.pcwebhosting.net/support/cron_scheduler.htm

will show you what's up with the different fields, and how to specify an entry. The case as shown here would run at 10 AM, every Sunday.
 
Old 09-26-2008, 02:53 PM   #5
brokenhalo
LQ Newbie
 
Registered: Sep 2008
Distribution: CentOS 4.4 & 4.6
Posts: 12

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by trickykid View Post
You really wouldn't need to create a separate script, something this easy could be done within the crontab.

Example:

Code:
0 10 * * 7 /usr/bin/rsync -av /home/backups /mnt/usbfantom/ 1> /dev/null 2>&1
The above would copy only the new backups in /home/backups to /mnt/usbfantom/ every Sunday at 10AM.
In the code you posted above, is there supposed to be a / directly after /mnt/usbfantom??? It just looks fishy... Also, If i want to test this right now to make sure it works, what would I change the 7 to??? Technically, isn't 1 Sunday (Sunday is the first day of the week)???? Also (sorry for all the questions) if I wanted to make the time, say, 10:42... How do I make that happen?

Last edited by brokenhalo; 09-26-2008 at 02:55 PM.
 
Old 09-26-2008, 02:59 PM   #6
trickykid
Guru
 
Registered: Jan 2001
Posts: 24,133

Rep: Reputation: 199Reputation: 199
Quote:
Originally Posted by brokenhalo View Post
In the code you posted above, is there supposed to be a / directly after /mnt/usbfantom??? It just looks fishy... Also, If i want to test this right now to make sure it works, what would I change the 7 to??? Technically, isn't 1 Sunday (Sunday is the first day of the week)???? Also (sorry for all the questions) if I wanted to make the time, say, 10:42... How do I make that happen?
Not necessarily. You can put a trailing / or not but Linux knows what is and what isn't a directory, etc. Either way would work.

0 or 7 is Sunday.

0 = Sunday
1 = Monday
2 = Tuesday
3 = Wednesday
4 = Thursday
5 = Friday
6 = Saturday
7 = Sunday

I always seem to use 7 instead of 0 cause while at work, the start of the week is always Monday for me and Sunday is the last day of the week when it comes to backups. Just my own rule of thumb I've kept over the years.

As for changing to test, here are what the fields represent:

MIN HOUR DOM(Day of month) MONTH DOW(Day of week)

Also for DOW, you can actually use names instead of Numbers if that makes it easier to remember.

Here's what you could do to change it to what you specified:

Code:
42 10 * * 5 /usr/bin/rsync -av /home/backups /mnt/usbfantom/ 1> /dev/null 2>&1
That would run at 10:42AM on Friday each week.

Last edited by trickykid; 09-26-2008 at 03:07 PM.
 
Old 09-26-2008, 03:02 PM   #7
trickykid
Guru
 
Registered: Jan 2001
Posts: 24,133

Rep: Reputation: 199Reputation: 199
Quote:
Originally Posted by TB0ne View Post
Yep, rsync would just keep what's changed, 'in sync' with what's elsewhere. You specify the 'master', and where to copy it. That's a pretty slick cron entry too, BTW, trickykid.
Thanks. Yeah, if you can do it in a one liner, no reason to ever create a separate script for it if it's gonna be a cron job.
 
Old 09-26-2008, 03:04 PM   #8
brokenhalo
LQ Newbie
 
Registered: Sep 2008
Distribution: CentOS 4.4 & 4.6
Posts: 12

Original Poster
Rep: Reputation: 0
Okay... I really appreciate your help, but what about the rest of the questions about the minute and day (for testing purposes right now).
 
Old 09-26-2008, 03:09 PM   #9
brokenhalo
LQ Newbie
 
Registered: Sep 2008
Distribution: CentOS 4.4 & 4.6
Posts: 12

Original Poster
Rep: Reputation: 0
One last thing... Tried to make the crontab and got this...


[root@host ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
"/tmp/crontab.XXXXUmMqZH":2: bad minute
errors in crontab file, can't install.
Do you want to retry the same edit? y
crontab: installing new crontab


What the hell does all this mean???
 
Old 09-26-2008, 03:37 PM   #10
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,769

Rep: Reputation: 2614Reputation: 2614Reputation: 2614Reputation: 2614Reputation: 2614Reputation: 2614Reputation: 2614Reputation: 2614Reputation: 2614Reputation: 2614Reputation: 2614
Quote:
Originally Posted by brokenhalo View Post
One last thing... Tried to make the crontab and got this...


[root@host ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
"/tmp/crontab.XXXXUmMqZH":2: bad minute
errors in crontab file, can't install.
Do you want to retry the same edit? y
crontab: installing new crontab


What the hell does all this mean???
Well, you don't have a root crontab, so it created one (note above where I mentioned that earlier).

Chances are, your editor is set to ed, which IS a user-friendly editor...it's just very picky about who its friends are. The "export EDITOR=vi" line I mentioned, puts you into the VI editor, ready to create/edit your crontab file.
 
Old 09-26-2008, 03:41 PM   #11
brokenhalo
LQ Newbie
 
Registered: Sep 2008
Distribution: CentOS 4.4 & 4.6
Posts: 12

Original Poster
Rep: Reputation: 0
Well, I DID follow your direction, all except for "export EDITOR=vi" I changed to nano instead (I hate vi with a passion and love nano ) What I find odd is that it created the cron in the /tmp directory... I really need to test this out, so I want to change the date to today (what is the number?) and I need to change the time to 5 minutes from now (how do I do minutes in the time field?). Please, any help would be great so I cna leave today and know that the backups are being made... Thanks!
 
Old 09-26-2008, 03:50 PM   #12
trickykid
Guru
 
Registered: Jan 2001
Posts: 24,133

Rep: Reputation: 199Reputation: 199
Quote:
Originally Posted by brokenhalo View Post
Well, I DID follow your direction, all except for "export EDITOR=vi" I changed to nano instead (I hate vi with a passion and love nano ) What I find odd is that it created the cron in the /tmp directory... I really need to test this out, so I want to change the date to today (what is the number?) and I need to change the time to 5 minutes from now (how do I do minutes in the time field?). Please, any help would be great so I cna leave today and know that the backups are being made... Thanks!
I went back and edited my other post that would define what you would use to test today. Just change the time and follow my chart of the day of week. I don't know what timezone you are in or country for that matter.
 
Old 09-26-2008, 04:06 PM   #13
brokenhalo
LQ Newbie
 
Registered: Sep 2008
Distribution: CentOS 4.4 & 4.6
Posts: 12

Original Poster
Rep: Reputation: 0
when I type the command crontab -e, does it create a new crontab every time? If it does, how do I delete the old ones? If it doesn't, how do I create new ones? (I'm almost done with the questions guys, bear with me ) Thanks a million! You guys are so much help, I can't thank you enough
 
Old 09-26-2008, 04:42 PM   #14
brokenhalo
LQ Newbie
 
Registered: Sep 2008
Distribution: CentOS 4.4 & 4.6
Posts: 12

Original Poster
Rep: Reputation: 0
Okay, everything seemed to work great!! I really appreciate all of your time.
 
Old 09-27-2008, 05:50 AM   #15
Illuvator
LQ Newbie
 
Registered: Sep 2008
Posts: 8

Rep: Reputation: 0
Quote:
Originally Posted by brokenhalo View Post
when I type the command crontab -e, does it create a new crontab every time? If it does, how do I delete the old ones? If it doesn't, how do I create new ones? (I'm almost done with the questions guys, bear with me ) Thanks a million! You guys are so much help, I can't thank you enough
No it doesnt, you just add new lines to it. One crontab could arguably have many lines, each doing something different at a different time.

To delete a job, just either remove the line, or prefix it with a # (i.e. comment out that line)

Il
 
  


Reply

Tags
backup, bash, cron, script


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
Cron backup script stops running after a few days brokenpromises Linux - Server 5 04-05-2008 05:30 AM
i want to cron a backup script sneakyimp Linux - Software 4 03-25-2008 09:02 PM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 08:16 AM
Backup script won't run in cron job. Why? Micro420 Linux - General 19 10-31-2007 08:26 PM
How to create/delete temp/backup files through a shell script ? Sid2007 Programming 4 10-17-2007 01:55 PM


All times are GMT -5. The time now is 04:38 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration