How to easily append a timestamp to tar archive name ?

Discussion in 'Perl Misc' started by Matthew Lincoln, Dec 9, 2007.

  1. I would like to create a tar archive with the well known tar command

    tar czvf "/usr/local/user234/bkp.tar.gz" /home/users/pmiller/

    It works. However I would like to automatically append a timestamp whenever I call this command
    in a (perl) script.

    The created archive file name should look like

    bkp-20071109-1523.tar.gz

    Where the format is preferred as YYYYMMDD-HHMM

    Does Gnu-tar offer such an automatic timestamp appending?

    If not: Is there a (short) work around (in perl) which let me create such a timestamp?

    Thank you
    Matthew
     
    Matthew Lincoln, Dec 9, 2007
    #1
    1. Advertising

  2. Matthew Lincoln

    Martin Klar Guest

    Matthew Lincoln schrieb:
    > I would like to create a tar archive with the well known tar command
    >
    > tar czvf "/usr/local/user234/bkp.tar.gz" /home/users/pmiller/
    >
    > It works. However I would like to automatically append a timestamp whenever I call this command
    > in a (perl) script.
    >
    > The created archive file name should look like
    >
    > bkp-20071109-1523.tar.gz
    >
    > Where the format is preferred as YYYYMMDD-HHMM
    >
    > Does Gnu-tar offer such an automatic timestamp appending?


    AFAIK no.

    Assuming you are using a bash you could solve it by:

    tar czvf "/usr/local/user234/bkp-$(date +%Y%m%d-%H%M).tar.gz" /home/users/pmiller/

    It *should* also work when called from a perl script, but I didn't test it.


    HTH Martin
     
    Martin Klar, Dec 9, 2007
    #2
    1. Advertising

  3. Matthew Lincoln

    Ben Morrow Guest

    Quoth Martin Klar <>:
    >
    > Assuming you are using a bash you could solve it by:
    >
    > tar czvf "/usr/local/user234/bkp-$(date +%Y%m%d-%H%M).tar.gz"
    > /home/users/pmiller/
    >
    > It *should* also work when called from a perl script, but I didn't test it.


    Not with that syntax :).

    Something like

    system 'tar', 'czvf', (
    '/usr/local/user234/bkp-' . qx(date +%Y%m%d-%H%M) . '.tar.gz'
    ), '/home/users/pmiller/';

    or
    system sprintf 'tar cvf "/usr/.../bkp-%s.tar.gz" /home/...',
    qx(date +%Y%,%d-%H%M);

    would work, but it would be better to do it from within Perl using
    POSIX::strftime.

    Ben
     
    Ben Morrow, Dec 9, 2007
    #3
  4. Matthew Lincoln wrote:

    > I would like to create a tar archive with the well known tar command
    >
    > tar czvf "/usr/local/user234/bkp.tar.gz" /home/users/pmiller/
    >
    > It works. However I would like to automatically append a timestamp
    > whenever I call this command in a (perl) script.
    >
    > The created archive file name should look like
    >
    > bkp-20071109-1523.tar.gz
    >
    > Where the format is preferred as YYYYMMDD-HHMM
    >
    > Does Gnu-tar offer such an automatic timestamp appending?
    >
    > If not: Is there a (short) work around (in perl) which let me create such
    > a timestamp?
    >
    > Thank you
    > Matthew


    ==================================

    #!/your/perl/path

    use strict;
    use warnings;
    use POSIX qw(strftime);

    my $now_string = strftime "%Y%m%d-%H%M", localtime;

    my $your_filename = "bkp-".$now_string".tar.gz

    ==================================

    man perlfunc

    man strftime

    --
    Regards,

    Gregory.
    Gentoo Linux - Penguin Power
     
    Gregory Shearman, Dec 9, 2007
    #4
  5. Matthew Lincoln

    John Bokma Guest

    Gregory Shearman <> wrote:


    > my $your_filename = "bkp-".$now_string".tar.gz


    o_O too much PHP?

    my $your_filename = "bkp-$now_string.tar.gz";

    or to make the scalar stand out a bit more:

    my $your_filename = "bkp-${now_string}.tar.gz";

    or, if you insist on . :

    my $your_filename = 'bkp-' . $now_string . '.tar.gz';


    --
    John

    Arachnids near Coyolillo - part 1
    http://johnbokma.com/mexit/2006/05/04/arachnids-coyolillo-1.html
     
    John Bokma, Dec 9, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Claudio Grondi
    Replies:
    4
    Views:
    583
    Claudio Grondi
    Aug 20, 2005
  2. Replies:
    2
    Views:
    446
    Michael Hoffman
    Apr 24, 2007
  3. Ray Van Dolson
    Replies:
    0
    Views:
    340
    Ray Van Dolson
    Sep 23, 2009
  4. Ray Van Dolson
    Replies:
    0
    Views:
    848
    Ray Van Dolson
    Sep 25, 2009
  5. benoit Guyon
    Replies:
    2
    Views:
    233
    benoit Guyon
    Jul 26, 2005
Loading...

Share This Page