-   Linux - General (
-   -   USB tar backups fail in cron, not from command line (

StikShift 03-06-2008 02:11 PM

USB tar backups fail in cron, not from command line
Title: USB tar backups fail in cron, not from command line

I use the --listed-incremental feature in tar to perform backups on my system. I have a script that performs this operation, and the script is run by cron in the wee hours of the morning. It has worked great for years, but I've run into a recent problem when using removable USB memory sticks. The script runs perfectly in cron as long as I don't try to backup anything off the USB stick with it.

If the contents of the stick, or a subdirectory on it are set to be backed up in the list. The incremental backup works perfectly when run from the command line. Unfortunately, when it runs in cron, I get an empty tar archive. I don't understand this, and I've had no success at debugging it. I suspect a problem with environmental variables, but again, I've been unable to debug it. Here is a copy of my crontab:

45 03 * * * /usr/local/sbin/bak3/ > /var/bak3/incr-bak.log

As you can see, I've used the full path to everything.

I've seen this problem on Mandriva as well as Kubuntu. It does not occur on Fedora 8 or Debian Etch. I suppose I could use those distros, but Mandriva is a fine distro, as is Kubuntu. There are many situations where these would be a better choice.

Thanks for any insight you might provide.

Galen Overstreet

lord-fu 03-06-2008 02:21 PM

What does your logs say during this time? Could you not put some debugging code within the backup script itself to help find out where something is going wrong? Is it a sudo problem? Maybe a mounting problem, permissions etc?

I know it is not an answer but at least maybe a direction.


StikShift 03-06-2008 05:36 PM

Nothing is amiss according to the logs. It all seems to work perfectly, except that the tar file is empty. This only happens when I try to back up files from the USB stick.

My "old" workstation ran Fedora 8, and Debian Etch before that. Both were set up the same as the Mandriva 2008, but neither had this problem.

Again, I'm at a loss to debug it, since it seems to work fine. I've tried all kinds of things with environmental variables, etc, but the problem persists. I'll keep tinkering.....

Thanks for your input!!!


gilead 03-06-2008 09:01 PM

Can you post the script? Does it use system calls that don't have the full path to the command and not check the result? I'm just guessing without seeing how the script sets up its environment and operates.

StikShift 03-07-2008 12:40 AM

It's in perl, you go. This is the "incremental" script, which is the one I run in cron. I usually run the full backups manually. For more info (including the "full" backup script & docs) at:

Thanks for your help!


!/usr/bin/perl -w
# Copyright 2004 G. P. Overstreet
# This file is part of bak3 - Unix/Linux Backup Scripts.
# bak3 - Unix/Linux Backup Scripts is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# bak3 - Unix/Linux Backup Scripts is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with bak3 - Unix/Linux Backup Scripts; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Version 3 Backup Script
# Incremental Backup
# GPO 5/11/04
# Last Mod GPO
# 6/28/04
# Set paths to conf files in /etc
$etc_path = "/etc/bak3";
$conf_file = "$etc_path/bak3.conf";
open (CONF, "<$conf_file")
|| die "Can't Open file $!";
# Read conf file, set local variable names/values
while (<CONF>) {
@var = split;
if ($var[0] eq "local_work") {$local_work = $var[2];}
if ($var[0] eq "fname") {$fname = $var[2];}
if ($var[0] eq "numincr") {$numincr = $var[2];}
if ($var[0] eq "encrypt_opt") {$encrypt_opt = $var[2];}
if ($var[0] eq "encrypt_key_id") {$encrypt_key_id = $var[2];}
if ($var[0] eq "incr_stor_opt") {$incr_stor_opt = $var[2];}
if ($var[0] eq "remote_user") {$remote_user = $var[2];}
if ($var[0] eq "remote_host") {$remote_host = $var[2];}
if ($var[0] eq "remote_full_path") {$remote_full_path = $var[2];}
if ($var[0] eq "numskipday") {$numskipday = $var[2];}
if ($var[0] eq "util_opt") {$util_opt = $var[2];}
if ($var[0] eq "scriptpath") {$scriptpath = $var[2];}
close CONF;
# Open last for input; Read in increment number of last incr backup
$verfile = "$local_work/last-incr";
open (INFILE, "<$verfile")
|| die "Can't Open file $!";
while (<INFILE>) {
# $iver = $_;
# $skiplast = $_;
@var = split;
close INFILE;
$iver = $var[0];
$skiplast = $var[1];
#$voo = chomp $iver;
#$voo2 = chomp $skiplast;
# Check if you want to write remote incremental this time
$skiplast = $skiplast + 1;
if ($skiplast eq $numskipday) {
$skiplast = 0;
# Create filename for new full backup; increment & write new number
$old_tar_name = "$fname-full-$iver.tar";
print $old_tar_name, "\n";
for ($count = 1; $count <= $numincr; $count++) {
if ($iver eq $count) {
$foo = $iver + 1;
if ($iver eq $numincr) {
$foo = 1;
$new_tar_name = "$fname-incr-$foo";
open (OUTFILE, ">$verfile")
|| die "Can't Open file $!";
print OUTFILE "$foo $skiplast";
close OUTFILE;
# Tar the new backup file
system $vook = "cp $local_work/incr-full.dat $local_work/incr.dat";
system $vook = "tar -cvpf $new_tar_name.tar --listed-incremental $loc#
# gzip or encrypt the new backup tar file
if ($encrypt_opt eq 0) {
system $vook = "gzip -f $new_tar_name.tar";
$copy_name = "$new_tar_name.tar.gz";
if ($encrypt_opt eq 1) {
system $vook = "gpg -r $encrypt_key_id -e $new_tar_name.tar";
$copy_name = "$new_tar_name.tar.gpg";
system $vook = "rm $new_tar_name.tar";
# Skip remote for skipdays
if ($skiplast ne 0) {
$skiplast = $skiplast + 1;
# copy to remote server
if ($incr_stor_opt gt 0) {
$vook2 = "$local_work/sftp-batch";
open (OUTFILE, ">$vook2")
|| die "Can't Open file $!";
print OUTFILE "put $copy_name $remote_full_path\nquit";
close OUTFILE;
system $vook = "sftp -b $vook2 $remote_user$remote_host";
# delete local copy for incr_stor_opt = 2
if ($incr_stor_opt eq 2) {
system $vook = "rm $copy_name";
# Delete previous incremental backup locally if incr_stor_opt = 3
if ($incr_stor_opt eq 3) {
system $vook = "rm $fname-incr-$iver*";
# Run optional utility script after incremental backup is complete
if ($util_opt eq 1) {
system $vook = "$scriptpath";
al_work/incr.dat -T $etc_path/backup.files.list";

gilead 03-10-2008 06:54 PM

The only suggestion I have is to change the commands in the system calls (gpg, etc.) to use the full path to the command. It may be failing because it's not finding the commands.

All times are GMT -5. The time now is 04:26 AM.