Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to create a backup cron script??? Plz help

Status
Not open for further replies.

brokenhalo

IS-IT--Management
Feb 24, 2008
169
US
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. Thanks in advance for the help!

Brad L. - MCP

"If the doctors told me I had 5 minutes to live, I would type faster.
 
It sounds like you have two problems. First, you need to create a script to copy the backups. That can be accomplished pretty easily, just look at the script that is running in the other cron job and adjust it as needed. Second, you need to create another entry in the cron.tab that will actually kick off the script you just wrote.

FWIW, you might get better response in the Linux (server) forum.
 
This is all stuff I already know, its just how to do it. The cron that I already have I didn't create in Linux, I created it in a web-based admin panel of my hosting software installed on the server, so I'm pretty clueless as to how it needs to be done...

Brad L. - MCP

"If the doctors told me I had 5 minutes to live, I would type faster.
 
For a simple backup, create an empty text document.
$touch mybackup.sh

Make it executable by the owner.
$chmod 755 mybackup.sh

Open in a text editor and add the following.

Code:
#!/bin/bash
rm -Rf /mnt/usbfantom/*
cp -R /home/backups/ /mnt/usbfantom/

Save it and run it to test.
$./mybackup.sh

If it works, add to a line to the crontab.
$crontab -e
0 6 * * 0 /location/of/mybackup.sh

If using vi to edit the crontab. Press the Insert key before typing. Press Escape when you're done, then type :wq and hit Enter to save and exit.

For Nano, just type as normal, then hit CTRL+O to save, press Enter when it asks, the hit CTRL+X to exit.

This will now run every Sunday at 0600 hrs. It deletes everything on /mnt/usbfantom/ then copies everything from /home/backups/ to it. If you want to create a series of backups (create folders named today's date YYYYMMDD), I'd recommend using PHP. The script is much smaller to write. I posted a shell script in the MySQL forum to backup databases and store them in the day folder format. You could use that also.

Here's a quick PHP script to do the backup with date folders.
Code:
<?php
$today = date('Ymd');
shell_exec("mkdir /mnt/usbfantom/$today");
shell_exec("cp -R /home/backups/ /mnt/usbfantom/$today/");
?>

Set up in the crontab just the same. Or to be safe, you could use...

0 6 * * 0 /usr/bin/php /location/of/mybackup.php

So it knows to use the php interpreter. You need to have php-cli installed to use it.

Mark
 
Thanks for the help, but to make things simpler, instead of creating a script and entering that into the crontab, I just entered the copy job right into the crontab... Below is what I entered into the crontab and it works great...

43 14 * * 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 2:43PM. Thank you for the help though guys!

Brad L. - MCP

"If the doctors told me I had 5 minutes to live, I would type faster.
 
Double-check your cron. The days of the week go from 0-6. Sunday is 0.

Mark
 
Actually the days go from 0-7, Sunday is both 0 and 7, whichever (I tried it over and over again, trust me)

Brad L. - MCP

"If the doctors told me I had 5 minutes to live, I would type faster.
 
I've never tried 7. Their doc doesn't show it as valid.

I take that back, I just found some doc that shows a 7. I've never seen that before.

Learn something new every day. I guess I'm done. :)

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top