#!/usr/bin/perl ###################################################################### # bak -- Copy files to a backup directory, appending the current date. # To be used with mop.pl # (http://www.linux-magazin.de/Artikel/ausgabe/2001/09/Perl/perl.html) # Mike Schilli, 2004 (m@perlmeister.com) ###################################################################### use strict; use warnings; my $VERSION = "1.0"; our $CVSVERSION = '$Revision: 1.1 $'; use Pod::Usage; use Getopt::Std; use File::Basename; use File::Copy; use constant DEBUG => 0; my $BAK_DIR = "$ENV{HOME}/BACKUPS"; getopts( 'hd:q', \ my %opts ); $BAK_DIR = $opts{d} if exists $opts{d}; pod2usage(-verbose => 2) if $opts{h}; pod2usage("No directory defined") unless defined $BAK_DIR; pod2usage("$BAK_DIR doesn't exist or is not writeable") unless -d $BAK_DIR and -w $BAK_DIR; foreach my $file (@ARGV) { my $newfile = File::Spec->catfile($BAK_DIR, basename($file) . "_" . nicedate()); print "Copying $file to $newfile\n" unless $opts{q}; copy $file, $newfile or die "Cannot copy $file to $newfile ($!)"; } ################################################## sub nicedate { ################################################## my ($time) = @_; $time = time() unless defined $time; my ($sec,$min,$hour,$mday,$mon, $year,$wday,$yday,$isdst) = localtime(time); return sprintf "%d-%02d-%02d-%02d:%02d:%02d", $year+1900, $mon+1, $mday, $hour, $min, $sec; } __END__ =head1 NAME bak - Copy files to a backup directory, appending today's date =head1 DOWNLOAD _SRC_HERE_ =head1 SYNOPSIS bak [-d backup_dir] file ... Options: -d specify a backup directory (defaults to ~/BACKUPS) -h get help -q be quiet =head1 OPTIONS =over 8 =item B<-d> Specify a backup directory (defaults to ~/BACKUPS) =item B<-h> Prints this manual page in text format. =item B<-q> Don't print progress messages on screen while copying. =back =head1 DESCRIPTION B is a simple command line utility to save snapshots of a file to a backup directory. Typically, it is used in connection with C which cleans up afterwards and deletes all but daily, weekly and monthly snapshots. See the article on http://www.linux-magazin.de/Artikel/ausgabe/2001/09/Perl/perl.html for details; =head1 EXAMPLES $ bak foo # Copies foo to ~/BACKUPS/foo_2004 =head1 LEGALESE Copyright 2004 by Mike Schilli, all rights reserved. This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself. =head1 AUTHOR 2004, Mike Schilli