Running this from SSH works fine, and running this through http works fine too. No errors, no problems, no fuss, no muss.
When it runs automatically from cron, the mysqldump fails, though I can't seem to catch the error message.
Here's some details:
Cron line:
56 5 * * * cd /usr/home/mydirectory/htdocs/cgi-bin/backup; perl backup.pl Mysqldump line (backticks)
`mysqldump -u username --password=password databasename > /usr/home/mydirectory/htdocs/cgi-bin/backup/files/filename.txt`; Tarball line (backticks)
`tar czpvf /usr/home/mydirectory/htdocs/cgi-bin/backup/files/backup.tar.gz /usr/home/mydirectory/htdocs`; Our webhost is offering no support on this, and I have a hunch it's a difference of permissions vs. cron, but both the files and the backup directories are chmod 755'd.
(directory names, user names and passwords changed to protect the innocent)
Any help is appreciated.
Thanks,
-- Lax
Running this from SSH works fine, and running this through http works fine too. No errors, no problems, no fuss, no muss.
tar czpf /home/mysite/backup.tar.gz /home/mysite
mysqldump -u user -pmypasswd mydb > /path/to/mydump.txt
You don't need a perl script, backticks or semi-colons.
Also the v option in tar isn't needed.
If the file is executable it should work with the following
56 5 * * * /path/to/my/backup
Each line will be executed when the script is run.
- Ian: I'll add that e-mail address right now
- Gorufu: I thought about that, but I'm timestamping the filenames. I know there's a way to do that using the *nix date() function, but it wouldn't have both timestamps the same for mysqldump and tarball. I'll remove the "v" flag from the command, thanks.
I know I'm being particular about this, but I want a rolling backup history of about a week at a time. This way, I can remove the "old" backups from the directory by timestamp. That's why I'm running this from a perl script.
Thanks guys, and I'll let you know about the little changes.
-- Lax
A very simple shell script should work. Just type the commands exactly the same as you use through SSH and save them to a file, for example
Don't be so sure.
Depending on the configuration of the server, cron will not use the same environment variables that are set when you run the shell of your choice. Of those variables, PATH causes the most problems.
Laxters, try using full path to mysqldump, or set appropriate PATH in your cron tab file.
Hoefully this example script will work. It works for me on RH 9
==============================
#!/usr/bin/perl
$dir = "/home/myname";
@months = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
($sec,$min,$hour,$mday,$mon,$year,$wday) = (localtime(time))[0,1,2,3,4,5,6];
$year += 1900;
$date = "$months[$mon]-$mday-$year";
# Example saved files Feb-26-2004.sql Feb-26-2004.tar.gz
`mysqldump -u myname -pmypass mydb > $dir/backup/$date.sql`;
`tar czf $dir/backup/$date.tar.gz --exclude="backup" $dir`;
exit;
==============================================
--exclude="backup" (excludes your existing backup files, assuming they were saved in a directory named backup).
chmod the backup script to 700 and it should work in any directory within your user area.
Edit your crontab to the following
56 5 * * * /path/to/my/backup.pl