作者: reistlin
来源: http://www.reistlin.com/blog/174
更新时间: 2011.02
版权声明: 原创文章.转载请保留作者信息和原文完整.谢绝任何方式的摘要

perl.gif

googlecode1.gifgooglecode2.gif
[https://reistlin.googlecode.com/svn/trunk/perl/cpbak2.pl]

#!/usr/bin/perl
 
# name: cpbak2 v1.0
# author: reistlin
# website: www.reistlin.com
# date: 2011.02.24
 
use strict;
use Data::Dumper;
use Time::Local;
use Getopt::Std;
 
# debug switch
my $debug = 0;
 
 
####################
# Configuration
####################
 
# defined current user home
my $home = $ENV{''HOME''};
 
# defined backup directory
my $path = "$home/backup";
 
# defined time tag
my $date = `date +%Y%m%d`;
 
 
####################
# Initial Directory
####################
 
if ( ! -e $path ) {
	system "mkdir -p $path";
}
 
 
####################
# Initial Getopt
####################
 
use vars qw($opt_c $opt_d);
getopts(''cd'');
 
# parameter "-c" backup to current directory
if ( $opt_c ) {
	$path = ".";
}
 
# parameter "-d" detail date mode "YYYY_MMDD_HHSS"
if ( $opt_d ) {
	$date = `date +%Y_%m%d_%H%m`;
}
 
 
####################
# Main Program
####################
 
# input target
my $target = shift;
 
# remove "\n"
chomp $date;
 
if ( $target ) {
	my $check = index($target, ".");
 
	if ( $check == -1 ) {
		print `cp -a $target $path/$target.$date`;
		exit;
	} else {
		$target =~ m/^(.+)\.(.+)$/;
		print `cp -a $target $path/$1.$date.$2`;
		exit;
	}
} else {
	exit;
}

标签: perl