LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Create a backup cron script (http://www.linuxquestions.org/questions/linux-newbie-8/create-a-backup-cron-script-672554/)

brokenhalo 09-26-2008 11:51 AM

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!

trickykid 09-26-2008 11:59 AM

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.

brokenhalo 09-26-2008 12:37 PM

...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.

TB0ne 09-26-2008 01:31 PM

Quote:

Originally Posted by brokenhalo (Post 3292645)
...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.

brokenhalo 09-26-2008 02:53 PM

Quote:

Originally Posted by trickykid (Post 3292619)
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?

trickykid 09-26-2008 02:59 PM

Quote:

Originally Posted by brokenhalo (Post 3292757)
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.

trickykid 09-26-2008 03:02 PM

Quote:

Originally Posted by TB0ne (Post 3292692)
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. ;)

brokenhalo 09-26-2008 03:04 PM

Okay... I really appreciate your help, but what about the rest of the questions about the minute and day (for testing purposes right now).

brokenhalo 09-26-2008 03:09 PM

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???

TB0ne 09-26-2008 03:37 PM

Quote:

Originally Posted by brokenhalo (Post 3292775)
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.

brokenhalo 09-26-2008 03:41 PM

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!

trickykid 09-26-2008 03:50 PM

Quote:

Originally Posted by brokenhalo (Post 3292805)
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.

brokenhalo 09-26-2008 04:06 PM

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

brokenhalo 09-26-2008 04:42 PM

Okay, everything seemed to work great!! I really appreciate all of your time.

Illuvator 09-27-2008 05:50 AM

Quote:

Originally Posted by brokenhalo (Post 3292814)
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


All times are GMT -5. The time now is 02:58 AM.