仍然收到 Removingleading `/' from member name with tar -C option from cron email

仍然收到 Removingleading `/' from member name with tar -C option from cron email

我试图摆脱Removing leading来自运行备份作业的 cron 的“”消息tar。电子邮件仍然有这些消息,我正在使用慢性病仅在出现错误时发送电子邮件:

ERROR OUTPUT:
/bin/tar: Removing leading `/' from member names
/bin/tar: Removing leading `/' from hard link targets
/bin/tar: Removing leading `/' from member names

我编辑了tar -czf包含-C我从中获得的选项这个线程像这样。

my($tarcmd) = "$tar -czf $backupname -C / $args $backup";

我在哪里可以删除前面提到的斜杠奥维尔·班纳特的博客文章

也许在脚本的这一部分的某个地方?

            # drobo=/path/to/drobo
if( /^\s*drobo\s*=\s*(.*)$/ ) {
$drobopath=$1;
}
            # tarargs=global tar arguments
elsif( /^\s*tarargs\s*=\s*(.*)$/ ) {
$tarargs=$1;
}
            # backup [condition] =/path/for/backup [tar args]
elsif( /^\s*backup\s*(\[[^\]]*\])?\s*=\s*(.*)$/ ) {
push(@backup,$2);
if( $1 ) {
    $condition{$2} = $1;

这是整个脚本:

#!/usr/bin/perl -W
use POSIX;
# Global variables
# Host name will be used as name of directory for backups on drobo
my($hostname)=`/bin/hostname`;
chomp($hostname);
my($configfile)="/etc/drobo-backup.conf";
my($tar)="/bin/tar";        # Path to tar utility
my($mkdir)="/bin/mkdir";    # Path to mkdir utility
my($verbose)=0;
my($testmode)=0;

sub Usage {
    print "Usage: drobo-backup [-v] [-c configfile]\n";
    print "   -v : verbose mode";
    print "   -c : specify configuration file (default $configfile)";
    print "   -n : printd but don't execute commands (for testing config)";
    exit 1;
}

# Subroutine to back up one directory to drobo
sub do_backup {
    my($drobo,$args,$backup,$cond) = @_;

# The backup arg may include per-backup tar args.  Strip off these
# and quotes to get at the filename
    my($backuppath)=$backup;
# If quoted, remove quotes for naming.  Otherwise it is required not
# not to have embedded blanks so that per-backup tar arguments may follow.
    if( $backuppath =~ /^"([^"]*)"/ ) { # double quotes
    $backuppath = $1;
    }
    elsif( $backuppath =~ /^'([^']*)'/ ) { # single quotes
    $backuppath = $1;
    }
    elsif( $backuppath =~ /^(\S*)/ ) { # otherwise no blanks in path
    $backuppath = $1;
    }

    if( -d $backuppath || -f $backuppath ) { # check it is a valid dir or file
    my($drobodir) = "$drobo/$hostname";
    # make sure the drobo subdirectory for this host exists
    my($mkdircmd) = "$mkdir $drobodir";
    if( $verbose ) {
        print "$mkdircmd\n";
    }
    if( ! ($testmode || -d $drobodir) ) {
        system($mkdircmd);
    }
    # if it did not work, bail.
    if( ! ($testmode || -d $drobodir) ) {
        print "Failed to create destination dir $drobodir\n";
        exit 1;
    }
# construct tarfile name, e.g. usr-local.tgz
    my($backupfilestem) = $backuppath;
                # Sanitize the tarfile name
    while($backupfilestem =~ s,^/,,) { next; } # remove any leading slash(es)
    while($backupfilestem =~ s,/$,,) { next; } # remove any trailing slash(es)
    $backupfilestem =~ s,/,-,g; # all internal slashes become hyphens
    $backupfilestem =~ s/[^-\.\w]/X/g;# all remaining non-word chars exc . become X

    my($backupname)="$drobodir/$backupfilestem-new.tgz";
    my($backuprename) = "$drobodir/$backupfilestem.tgz";
    my($tarcmd) = "$tar -czf $backupname -C / $args $backup";

    if( $cond ) {
        $cond =~ s/^\[//;   # remove the [ ] around condition
        $cond =~ s/\]$//;
        $cond =~ s/BKPATH/$backuppath/g; # convenience substitutions
        $cond =~ s/TARFILE/$backuprename/g;
        $cond =~ s/TARDIR/$drobodir/g;
    }
    if( $cond && WEXITSTATUS(system("test $cond")) != 0 ) {
        if( $verbose ) {
        print "Condition [$cond] tests false\n";
        print "No backup of $backuppath\n";
        }
    }
    else {
        if( $verbose ) {
        if( $cond ) {
            print "Condition [$cond] tests true\n";
        }
        print "$tarcmd\n";
        }
    # tar returns 0 for success, 1 for warnings such as file changed while
    # being copied.  So we take either as meaning success.  Rename foo-new.tgz
    # to the (usually existing) foo.tgz.  N.B. system() returns status<<8.
        if( !$testmode ) {
        if( WEXITSTATUS(system($tarcmd)) >= 2 ) {
            print "\nBackup of $backuppath FAILED\n\n";
            # to avoid bad backup being renamed to good in second try, call it bad
            $backuprename = "$drobodir/$backupfilestem-FAILED.tgz";
        }
        if( rename("$backupname","$backuprename") ) {
            print "Backed up $backuppath to $backuprename\n";
        }
        else {
            print "Failed to rename $backupname to $backuprename: $!\n";
        }
        }
    }
    }
    else {
    print "$backuppath is not a directory or file\n";
    }
}



# default arguments to use on every backup
my($tarargs)="--atime-preserve --one-file-system";


# set default drobopath according to dsm (lc) or cis (rh) network

my($drobopath)="/path"; #
if($hostname =~ /\.our\.domain\.edu/) {
    $drobopath="/path"; #
}


# Process command line arguments
while(@ARGV) {
    if( $ARGV[0] eq "-c" ) {    # -c configfile
    shift (@ARGV);
    if(@ARGV) {
        $configfile = $ARGV[0];
    }
    else {
        Usage();
    }
    }
    elsif( $ARGV[0] eq "-v" ) { # -v (verbose mode)
    ++$verbose;
    }
    elsif( $ARGV[0] eq "-n" ) { # -n (no-exec mode)
    $testmode = 1;
    }
    else {          # unrecognized argument
    Usage();
    }

    shift (@ARGV);
}


open(CONFIGFILE,$configfile) || die("Cannot open configfile $configfile: $!");

if($verbose) {
    print "Reading configfile $configfile\n";
}

my(@backup);
my(%condition);
my($configline) = 0;
foreach (<CONFIGFILE>) {
    $configline++;
    if( /^\s*#/ || /^\s*$/ ) {  # skip blank & comment lines (first nonblank is #)
    next;
    }
                # drobo=/path/to/drobo
    if( /^\s*drobo\s*=\s*(.*)$/ ) {
    $drobopath=$1;
    }
                # tarargs=global tar arguments
    elsif( /^\s*tarargs\s*=\s*(.*)$/ ) {
    $tarargs=$1;
    }
                # backup [condition] =/path/for/backup [tar args]
    elsif( /^\s*backup\s*(\[[^\]]*\])?\s*=\s*(.*)$/ ) {
    push(@backup,$2);
    if( $1 ) {
        $condition{$2} = $1;
    }
    }
    else {
    print "Unknown config directive at line $configline in $configfile:\n";
    print;
    exit 1;
    }
}

close(CONFIGFILE);


my($path);
foreach $path (@backup) {
    do_backup($drobopath,$tarargs,$path,$condition{$path});
}


# For unknown reason, rename of some files often fails.  Try again here.

foreach $tarfile ( glob("$drobopath/$hostname/*-new.tgz") ) {
    $rename_name = $tarfile;
    $rename_name =~ s/-new\.tgz$/.tgz/;
    if( rename($tarfile,$rename_name) ) {
    print "Second try renamed $tarfile to $rename_name\n";
    }
}

答案1

在 Perl 脚本中,文件名从带有 . 的文件中读取,foreach (<CONFIGFILE>)并添加到带有 . 的数组中push(@backup,$2);。对于 array( foreach $path (@backup)) 中的每一项,都会调用子例程do_backup(),文件名是参数$backup。正如您所指出的,这在设置 时保持不变$tarcmd。因此,解决方案是在该行之前编辑此变量:

$backup =~ s|/|| if $backup =~ m|^['"]?/|; # remove any leading slash
my($tarcmd) = "$tar -czf $backupname -C / $args $backup";

s|/||如果变量与模式匹配^['"]?/,即在开头,可能是引号或双引号,然后是斜杠,则这会将 ( ) 第一个斜杠替换为空。这是因为早期的代码假设此变量中给定的路径可能用引号引起来,例如为了保护空格。

但是,您只能执行此操作并添加-C /路径是否以斜杠开头,因此除非您确定是这种情况,否则您可能应该有 2 个版本,例如

my($tarcmd);
if($backup =~ m|^['"]?/|){
   $backup =~ s|/||;  # remove any leading slash
   $tarcmd = "$tar -czf $backupname -C / $args $backup";
}else{
   $tarcmd = "$tar -czf $backupname $args $backup";
}

答案2

你将不得不做类似的事情:

tar -czf backup.tar.gz -C / $(ls /)

即列出根目录中的所有文件和目录,不带前导斜杠。但这可能不是一件非常明智的事情,因为根目录包含您不想备份的各种条目,例如目录/proc/dev目录等。

相关内容