View Full Version : Crontab Backup Script
ffurtado
11-13-2003, 11:57 AM
I'm looking for a solution to backup a few log files using a Crontab. Does anyone have any experience with this script?
World Wide Backup - Our latest free script that will allow you to backup your website and even gives you the option to FTP that backup to another server. Requires that your server has Tar (most UNIX/Linux based systems do). For the FTP option, you also will need the Net::FTP module installed on your server (again, most do). The script also can email you when the backup is finished (or if there was an error). This script is not a .cgi script that you can execute from a browser. You will need either telnet (shell) access to your server, OR crontab access to your server.
http://www.worldwidecreations.com/freescripts.htm
Thanks,
Fred
Kevin
11-13-2003, 12:48 PM
Fred,
I do not have any experience with that particular backup script however we do have tar and Net::FTP installed and we do provide both shell and cron access so it should work.
If you need help with cron there is a tutorial here: http://www.aota.net/Script_Installation_Tips/cronhelp.php4
--
Kevin
ffurtado
11-13-2003, 02:16 PM
Thank you for the information.
The problem is that I need a perl script to *call* in the Crontab file to backup the few files in question. I don't have a .pl script and I don't know how to write such a script. I have not been able to find a backup script anywhere that I could modify slightly for my needs.
I have been using the Windows XP Scheduler to run my CuteFTP program at 9:50 AM. In CuteFTP, I scheduled a transfer to download three files to my computer at 10:00AM.
This works Ok, but I have a dial-up connection to the Internet, so I have to be sure my computer is on and connected to the Internet at the appropriate time.
I really need a Crontab job to automatically backup the 3 files and send me an email telling me it was done (without the use of my computer).
Fred
ffurtado
11-14-2003, 07:48 PM
I have spent hours looking for a perl script I can modify to accomplish a very simple backup using Crontab. I would appreciate help finding such a script.
Crontab
At 11:50PM every day
backup.pl
backup.pl script should do this:
1. Copy the 4 ".log" files from my /cgi-bin/data folder to my cgi-bin/data/backup folder
2. Email me a message saying it was done. I can use a sendmail routine I have for the email function.
Or maybe a cron like this might work:
50 11 * * * cd /big/dom/xdomain/cgi-bin/data;tar -cvf - * | tar -C /big/dom/xdomain/cgi-bin/data/backup/ -xv
I don't have much experience with Perl, Tar, or Cron jobs.
Thanks,
Fred
Kevin
11-15-2003, 10:55 AM
Originally posted by ffurtado:
Crontab
At 11:50PM every day
backup.pl
backup.pl script should do this:
/big/dom/xdomain/cgi-bin/data/backup/
1. Copy the 4 ".log" files from my /cgi-bin/data folder to my cgi-bin/data/backup folder
2. Email me a message saying it was done. I can use a sendmail routine I have for the email function.
Or maybe a cron like this might work:
50 11 * * * cd /big/dom/xdomain/cgi-bin/data;tar -cvf - * | tar -C /big/dom/xdomain/cgi-bin/data/backup/ -xv
Fred,
if all you want is to copy some files from one directory to another the cron job would be pretty simple:
50 11 * * * cp -fv /big/dom/xdomain/cgi-bin/data/*.log /big/dom/xdomain/cgi-bin/data/backup/
That would copy all of the logfiles overwriting the previous backup and cron would send the output to the email address listed on the MAILTO= line of the crontab.
--
Kevin
ffurtado
11-15-2003, 12:19 PM
Kevin, thank you for your reply.
Before I risk any possible harm to my files, I'd like to double-check this procedure with you.
*** My Backup Cron Job ***
Copy all log files in specified directory to a new backup directory
Overwrite previous backup files if present in backup directory
Send the output to the email address listed.
Daily at 11:50PM
Note: replace xdomain with user account
1. Create new directory /big/dom/xdomain/cgi-bin/cronjobs
2. CHMOD 755
3. Create text file "logcron.txt"
MAILTO=webmaster@domain.com
50 11 * * * cp -fv /big/dom/xdomain/cgi-bin/data/*.log /big/dom/xdomain/cgi-bin/data/backup/
[ENTER for last line]
4. Copy "logcron.txt" to /big/dom/xdomain/cgi-bin/cronjobs directory (upload in ASCII)
5. Telnet into account and issue the following command:
crontab /big/dom/xdomain/cgibin/cronjobs/logcron.txt
I do not know what to expect from this:
"Send the output to the email address listed"
What is the output I receive in the email?
Thank you,
Fred
Kevin
11-15-2003, 12:28 PM
Fred,
Your script is correct as is your method for getting it into the crontab. However, you should be aware that when you run 'crontab [file]' it overwrites what is in the crontab instead of adding to it therefore if you ever have multiple cron jobs they should all be in one text file not multiple text files in a cronjobs directory. IOW, it looks like you were planning to setup for multiple cron jobs in separate text files and that will not work. Every time you want to change or add to the crontab you must use a master text file with all of the jobs in it. If you lose that file you can regenerate it with the following command:
$ crontab -l > textfile
I do not know what to expect from this:
"Send the output to the email address listed"
What is the output I receive in the email?
The -v parameter on the cp command will cause it to be verbose and it will print something like:
'sourcefile' -> 'destfile'
for each file that it copies. Cron automatically sends that output (along with any errors) in an email unless told not to.
--
Kevin
ffurtado
11-15-2003, 01:08 PM
Hello Kevin,
Whew!
Thank you for that last bit of information.
Yes, I have log files for 3 different versions of the same database program.
The .log files are located in separate directories and I want to use the crontab job to back them all up to the one backup directory. Do I repeat the commands like this in the crontab file?
MAILTO=webmaster@domain.com
50 11 * * * cp -fv /big/dom/xdomain/cgi-bin/data/*.log /big/dom/xdomain/cgi-bin/data/backup/
50 11 * * * cp -fv /big/dom/xdomain/cgi-bin/data2/*.log /big/dom/xdomain/cgi-bin/data2/backup/
50 11 * * * cp -fv /big/dom/xdomain/cgi-bin/data3/*.log /big/dom/xdomain/cgi-bin/data3/backup/
[ENTER for last line]
3 separate cp's
/data directory
/data2 directory
/data3 directory
Fred
Kevin
11-15-2003, 01:14 PM
Fred,
That will backup the log files in all 3 directories but it will back them up to 3 different directories since you specified 3 different targets. If you want them all backed up to the same directory then make all the targets the same. Also, if you want to backup all the logs in all the data directories to the same backup directory you could do it in one job like this:
50 11 * * * cp -fv /big/dom/xdomain/cgi-bin/data*/*.log /big/dom/xdomain/[backup dir]/
--
Kevin
Kevin
11-15-2003, 01:32 PM
Fred,
One more thing I just noticed. You said to run the job at 11:50 pm however cron operates on a 24 hour clock so you need to use 23 instead of 11 in your cron jobs.
--
Kevin
ffurtado
11-15-2003, 01:47 PM
Hello Kevin,
Maybe I'm confused with "source vs destination" in the crontab command?
What I *really* have is these separate data directories:
/big/dom/xdomain/cgi-bin/webdata/
/big/dom/xdomain/cgi-bin/ham/
/big/dom/xdomain/cgi-bin/silentkey/
The .log files in each of these directories (with different .log filenames) should all be copied to:
/big/dom/xdomain/cgi-bin/webdata/backup/
MAILTO=webmaster@domain.com
50 11 * * * cp -fv /big/dom/xdomain/cgi-bin/webdata/*.log /big/dom/xdomain/cgi-bin/webdata/backup/
50 11 * * * cp -fv /big/dom/xdomain/cgi-bin/ham/*.log /big/dom/xdomain/cgi-bin/webdata/backup/
50 11 * * * cp -fv /big/dom/xdomain/cgi-bin/silentkey/*.log /big/dom/xdomain/cgi-bin/webdata/backup/
[ENTER for last line]
By the way, do I need the trailing / after backup above????
Now if I could do all that somehow with just one 50 11 * * *
That would be great.
Thanks
Fred
Kevin
11-15-2003, 01:54 PM
Originally posted by ffurtado:
MAILTO=webmaster@domain.com
50 11 * * * cp -fv /big/dom/xdomain/cgi-bin/webdata/*.log /big/dom/xdomain/cgi-bin/webdata/backup/
50 11 * * * cp -fv /big/dom/xdomain/cgi-bin/ham/*.log /big/dom/xdomain/cgi-bin/webdata/backup/
50 11 * * * cp -fv /big/dom/xdomain/cgi-bin/silentkey/*.log /big/dom/xdomain/cgi-bin/webdata/backup/
[ENTER for last line]
Fred,
Source and target are just ways of saying the path you are copying from and the path you are copying to. Since your 3 source directories are not similarly named you can't use the data* syntax that I was suggesting so you will have to use 3 seperate jobs instead.
The trailing '/' on the backup directory isn't required but it looks nice and does an extra check to ensure that it is copying to a directory instead of a file.
Your current syntax will be correct once you change the '11' to '23' to specify pm.
--
Kevin
ffurtado
11-15-2003, 02:03 PM
Kevin,
My last question!
What permissions to I need to give my my new /cgibin/webdata/backup folder for crontab to write the log files?
755 666 ???
I will try the crontab later today.
I'm sorry if I have cluttered up your Forum with all these replies. Please delete them all if you wish.
Thank you very much for your patience and support.
You people are great!
Kevin
11-15-2003, 02:07 PM
Originally posted by ffurtado:
Kevin,
My last question!
What permissions to I need to give my my new /cgibin/webdata/backup folder for crontab to write the log files?
755 666 ???
Directories always need execute permission so 666 is out. Since I doubt you want to publish your backups on your web site 755 probably isn't a good idea either. 700 Should provide all the access that is needed without allowing anything that shouldn't be.
I'm sorry if I have cluttered up your Forum with all these replies. Please delete them all if you wish.
That is exactly what these forums are for ;)
--
Kevin
ffurtado
11-19-2003, 11:52 AM
I followed the instructions and the crontab backup job worked perfectly. Thank you for your assistance.
The MAILTO function sends email to me with a subject that I would like to change.
SUBJECT: big/dom/xdomain/cgi-bin/webdata/*.log /big/dom/xdomain/cgi-bin/webdata/backup/
to
SUBJECT: WDB Backup Job
Is there a command you can use directly in the crontab file to include a subject for the email message?
50 11 * * * cp -fv /big/dom/xdomain/cgi-bin/webdata/*.log /big/dom/xdomain/cgi-bin/webdata/backup/ SUBJECT HERE
Kevin
11-19-2003, 01:04 PM
Originally posted by ffurtado:
I followed the instructions and the crontab backup job worked perfectly. Thank you for your assistance.
The MAILTO function sends email to me with a subject that I would like to change.
SUBJECT: big/dom/xdomain/cgi-bin/webdata/*.log /big/dom/xdomain/cgi-bin/webdata/backup/
to
SUBJECT: WDB Backup Job
Is there a command you can use directly in the crontab file to include a subject for the email message?
50 11 * * * cp -fv /big/dom/xdomain/cgi-bin/webdata/*.log /big/dom/xdomain/cgi-bin/webdata/backup/ SUBJECT HERE
The only way to do that is to bypass cron's email handling and send the mail yourself. You could do something like this:
50 11 * * * cp -fv /big/dom/xdomain/cgi-bin/webdata/*.log /bin/dom/xdomain/cgi-bin/webdata/backup/ | mail -s "[subject]" [email address] 2>&1
Note that this would cause you to always recieve an email instead of just getting one when there is output to send but with the -v option on cp there will allways be output so that doesn't matter in this case. Also, if you do this the MAILTO line will no longer matter.
--
Kevin
ffurtado
11-19-2003, 01:57 PM
Would this be correct?
50 11 * * * cp -fv /big/dom/xdomain/cgi-bin/webdata/*.log /bin/dom/xdomain/cgi-bin/webdata/backup/ | mail -s "[subject]" [email address] 2>&1
| mail -s "WDB Backup Job" webmaster@domain .com 2>&1
or do I need to enclose above inside the [ ] 's shown above.
Thanks
Fred
Kevin
11-19-2003, 02:00 PM
Originally posted by ffurtado:
Would this be correct?
| mail -s "WDB Backup Job" webmaster@domain .com 2>&1
That is correct. I used the [] notation to show you what to change. The quotes are needed to group multiple words together in the subject line.
--
Kevin
vBulletin® v3.6.8, Copyright ©2000-2013, Jelsoft Enterprises Ltd.