randomfox (randomfox) wrote,
randomfox
randomfox

Simple versioned backup

This Perl script adds a time stamp to the file name and copies the file to a backup directory.


#!perl -w
use strict;
require 5.004;

use File::Basename;
use File::Copy;
use File::Spec;

my $BACKUPDIR = 'c:\\backup';

sub doBackup {
    my $fname = shift;
    my $basename = basename $fname;

    my $destbase = sprintf("%08X-%s", time, $basename);
    my $destname = File::Spec->catfile($BACKUPDIR, $destbase);

    print "Backing up $fname to $destname...\n";
    copy($fname, $destname) or die "copy failed: $!\n";
}

@ARGV > 0 or die "Usage: $0 files-to-backup\n";
for my $fname (@ARGV) {
    doBackup $fname;
}

__END__

Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments