Gossamer Forum
Home : General : Perl Programming :

system date and tar archive names

Quote Reply
system date and tar archive names
I'm trying to make small backup script and I'm having a heck of a time getting a system datestamp in the archive name. Here is the code I'm using:

Code:
#!/usr/bin/perl -w
use strict;

my $date = `date +%Y%m%d`;
my $user = 'piper';

my $cmd = "tar -cpszf $user-$date.tar.gz /path/to/public_html/$user";

print `$cmd`;

When I run that code I get the following error:
Quote:
tar: Cowardly refusing to create an empty archive
Try `tar --help' for more information.
sh: .tar.gz: command not found

If I use $user by itself the code runs fine. If I type in the date by hand instead of using a scalar it runs fine. If I declare date like: my $date = '2002-10-13'; and use it in the archive name it works. Why cant I user the system date function to get the date for the archive name?

Regards,
Charlie
Quote Reply
Re: [Chaz] system date and tar archive names In reply to
I've solved the problem by getting the date with localtime but I'd still like to know why the system date wouldn't work.

~Charlie
Quote Reply
Re: [Chaz] system date and tar archive names In reply to
Your code should have worked. Here is what I get from my ssh account.

[root@paul /root]# date +%Y-%m-%d
2002-10-13

I would use the following instead of a system command

Code:
use POSIX qw/strftime/;

my $date = POSIX::strftime("%Y-%m-%d", localtime);

Last edited by:

Paul: Oct 14, 2002, 2:27 AM
Quote Reply
Re: [Paul] system date and tar archive names In reply to
Thanks, Paul. I still can't figure out why it wont let me use that date and it's really bugging me. I'll give your method a go, it looks simpler than what I did.

Thanks,
Charlie
Quote Reply
Re: [Chaz] system date and tar archive names In reply to
The problem was a \n in $date.

This works:
Code:
#!/usr/bin/perl -w
use strict;

my $date = `date +%Y%m%d`;
my $user = 'piper';
chomp($date);

my $cmd = "tar -cpszf $user-$date.tar.gz /path/to/public_html/$user";
print `$cmd`;

It's so easy to overlook the simple things :/

Thanks,
Charlie