I use the following script to create a gzipped tarball of my main website (/var/www/) and another of our extra sites with a datestamp on the file name:
When I run the script manually with
/bin/tar -cvf /backups/websites.tar /usr/share/websites/
mkdir -p /backups/websites/`/bin/date +%Y/%b`
/bin/mv /backups/websites.tar.gz /backups/websites/`/bin/date +%Y/%b/websites_%d-%b-%Y.tar.gz`
/bin/tar -cvf /backups/joomla.tar /var/www/
mkdir -p /backups/joomla/`/bin/date +%Y/%b`
/bin/mv /backups/joomla.tar.gz /backups/joomla/`/bin/date +%Y/%b/joomla_%d-%b-%Y.tar.gz`
It works perfectly (about 24M for Joomla, and about 1.5GB for the websites)
However when I run the same script via crontab, it only creates a tarball about 300k in size?
Am stumped as to why it doesnt work as a cronjob....
@daily /path/to/scripts/scriptname.sh #Backup all websites
cron job running as a user who cn't read those files?
Nope - running as root - other cronjob runs fine - does the same thing with mysql (mysqldump, gzip with date appeneded..)
Wondering if cron timse out at all?
Try re-ordering it - make the directory first, and then do the tar as a one liner (add "z" to the commandline opts to make it zip automatically).
Oooh... and check that the output of "date" isn't changing while your cron is taking place. You might stick that in a variable first before use.
Default installations of cron on Ubuntu mail the job output to root, unless you redirect it (which you haven't). Take a look there.
You also don't have a bang (!) in your shebang, line 1 (should read #!/bin/sh)
Eek! good spot powdarrmonkey!!
@tom_newton - the cron job runs at 00:00 so date should have already changed before it runs!
Also @ powdarrmonkey - i cant read teh system mail?? installed mutt and ran it as root and main user (the one created at install) but all empty?
Check cat /var/mail/root or /var/spool/mail/root? See if it's being directed somewhere else (/dev/null is a favourite) in /etc/aliases.
nothing in /var/mail or /var/spool/mail (which is ointing to /var/mail/ anyway!)
Always worth a check :) How about /etc/aliases?
root@web01:~# cat /etc/aliases
# See man 5 aliases for format
Hmm. Ubuntu folks do even more silly things than I thought.
In that case, redirect it to a log file yourself - in cron,
It will be easier to read if you do the gzip inline, as Tom suggests:
@daily /path/to/scripts/scriptname.sh > /var/mail/mybackup.log 2>&1 #Backup all websites
and get rid of the calls to gzip. Add the -v (verbose) flag to your calls to mv too.
/bin/tar -czvf /backups/joomla.tar.gz /var/www/
can I still datestamp it that way
Would be a heck of a ot easier i admit..
gonna redirect it to /var/log/backups/websites.log - does it need to be >> though to append?
Yes, your mv command will still rename it, you're just combining the tar and gzip steps (why have two processes when tar can do it all? :))