#!/usr/bin/perl -w # Backup an entire folder to a date-stamped tarball. use strict; use POSIX qw(strftime); # My source and destination for backups. my $backupname = shift(); my $backupsource = shift(); my @options = @ARGV; die "Usage: makebackup [tar options]\n" if not $backupname; # Setup my backup file prefix and extension. my $backupprefix = sprintf('%s-%s', $backupname, strftime("%Y-%m-%d", localtime)); my $backupextension = ".tar.gz"; # Setup my default backup file name. my $backupfile = $backupprefix . $backupextension; # If my desired backup file already exists, keep appending a "-n" # to the file name until we find a file that doesn't exist and use # that file name. my $fileindex = 0; while(-e "$backupfile") { # Increment my index. $fileindex++; # Build my new file name. $backupfile = $backupprefix . '-' . $fileindex . $backupextension; } # Make a tarball. system "/usr/bin/tar @options -zcf $backupfile $backupsource";