-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbkup.pl
executable file
·84 lines (75 loc) · 3.04 KB
/
bkup.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/perl
use strict;
use warnings;
use File::Copy qw(cp);
use File::Copy qw(move);
use POSIX qw(strftime);
use File::Spec::Functions;
use File::Path qw(make_path);
use Getopt::Long qw(GetOptions);
Getopt::Long::Configure qw(gnu_getopt);
###################################
# bkup - Version 0.7.5 #
# #
# Released under the MIT License #
# By Austin Lowery #
###################################
## Setup
my $date = strftime "%F %H:%M:%S", localtime;
my $epoch = time();
my $user = $ENV{LOGNAME} || $ENV{USER} || getpwuid($<);
my $homeDir = $ENV{HOME} || (getpwuid $<)[7];
my $defaultFallbackInstallDir = "$homeDir/bkups/bin/";
my $backupDir = $homeDir."/bkups/";
my $logPath = $backupDir.'bkup.log';
make_path($backupDir);
open my $fhout, ">>", $logPath or die $!;
## Options
my $install;
my $installDir = "/usr/local/bin/";
## Handle Input
GetOptions(
'install|i' => \$install,
'install-dir=s' => \$installDir,
) or die "No help built in. You're on your own for now.\n";
if ($install){
installBkup();
}
my $input = shift or die "Usage: $0 FILENAME\n";
my $origPath = validateFilePath($input);
my ($volume, $dirs, $fileName) = File::Spec->splitpath($origPath);
my $destPath = $backupDir.$fileName."-".$epoch;
## Do the thing
mkdir ($backupDir, 0755);
cp $origPath, $destPath;
select $fhout;
print $fhout $date." ".$user." ".$origPath." -> ".$destPath."\n";
select STDOUT;
print "Copied ".$origPath." -> ".$destPath;
## Subs
sub validateFilePath {
my ($input) = @_;
if (! -e $input) {
die ("$input not found.");
} elsif (-d $input){
die ("$input is a directory. Directory support is planed in feature #48. Check pm.austinlowery.com for more info.");
} elsif (! file_name_is_absolute($input)) {
return File::Spec->rel2abs($input);
} else {
return $input
}
}
sub installBkup {
if ( $installDir !~ m/\/$/) { $installDir = "$installDir/"; };
my $installDest = $installDir."bkup";
if ($installDir eq $defaultFallbackInstallDir) { make_path($installDir); }; # Since this is the default fallback directroy, we'll create it to faciliate a quick and easy two step installation process for non root users.
if (! -e $installDir) { die("$installDir does not exist. Please create it or choose a different directory")};
if (! -d $installDir) { die("$installDir is not a directory. You must use a directory with the --install-dir flag.")};
if (! -w $installDir) { die("You do not have permission to install bkup to $installDir . Try using sudo. If you don't have sudo access, you can use --install-dir [Path to install Dir] in additon to the install flag to specify a directory that you have permission to write to.\n");};
if (-e $installDest) { die("bkup is already installed at $installDest\n");};
cp ($0, $installDest);
chmod 0755, $installDest;
print "bkup installed to $installDest\n";
if ( grep(! /^$installDir$/, File::Spec->path()) ) { print "To complete your installation, issue the following command and add it to your shell configuration file (.bashrc etc.):\nsource \"\$PATH:$installDir\"";};
exit;
}