Updated to allow directory copy, changed the --file option to --path to support this functionality
This commit is contained in:
@@ -25,47 +25,59 @@ use Getopt::Long;
|
|||||||
|
|
||||||
|
|
||||||
# sane defaults
|
# sane defaults
|
||||||
my $file = "";
|
my $path= "";
|
||||||
my $host = "";
|
my $host = "";
|
||||||
my $directory = ".";
|
|
||||||
my $user = getlogin() || (getpwuid($<))[0] || $ENV{LOGNAME} || $ENV{USER};
|
my $user = getlogin() || (getpwuid($<))[0] || $ENV{LOGNAME} || $ENV{USER};
|
||||||
my $dest= "/home/$user/Downloads/";
|
my $dest= "/home/$user/Downloads/";
|
||||||
my $flags = "rvz";
|
my $flags = "rvz";
|
||||||
my $verbose;
|
my $verbose;
|
||||||
my $help;
|
my $help;
|
||||||
|
my $dir;
|
||||||
|
|
||||||
# options
|
# options
|
||||||
my $result = GetOptions ( "file=s" => \$file,
|
my $result = GetOptions ( "path=s" => \$path,
|
||||||
"host=s" => \$host,
|
"host=s" => \$host,
|
||||||
"directory=s" => \$directory,
|
"dir" => \$dir,
|
||||||
"user=s" => \$user,
|
"user=s" => \$user,
|
||||||
"dest=s" => \$dest,
|
"dest=s" => \$dest,
|
||||||
"flags=s" => \$flags,
|
"flags=s" => \$flags,
|
||||||
"verbose" => \$verbose,
|
"verbose" => \$verbose,
|
||||||
"help" => \$help);
|
"help" => \$help);
|
||||||
|
|
||||||
print "file: $file, host: $host, directory: $directory, user: $user, dest: $dest flags: $flags verbose: $verbose help: $help\n";
|
|
||||||
|
sub transfer {
|
||||||
|
#print "file: $file, host: $host, srcdir: $dir, user: $user, dest: $dest flags: $flags verbose: $verbose help: $help\n";
|
||||||
### Begin main logic
|
### Begin main logic
|
||||||
# help catch
|
# help catch
|
||||||
if ($help) {
|
if ($help) {
|
||||||
help();
|
help();
|
||||||
|
|
||||||
}
|
}
|
||||||
# If we have all the right arguments, begin the copy
|
# if we're passed the directory flag and we have the required options set
|
||||||
elsif (length $file && length $host && length $user && length $dest && length $directory) {
|
elsif ($dir && length $host && length $dest && length $user) {
|
||||||
|
|
||||||
|
print "Transferring object 1 of 1 \n\n";
|
||||||
|
|
||||||
|
# transfer our directory
|
||||||
|
system("rsync", "-$flags", "$dir", "$user\@$host:$dest");
|
||||||
|
}
|
||||||
|
# If we have all the right options set for an infile, begin the copy
|
||||||
|
elsif (length $path && length $host && length $user && length $dest) {
|
||||||
#print "in elsif block\n";
|
#print "in elsif block\n";
|
||||||
# Open our filehandle
|
# Open our filehandle
|
||||||
open my $info, $file or die "Could not open $file: $!";
|
open my $info, $path or die "Could not open $path: $!";
|
||||||
|
|
||||||
|
my $linesnum = 0;
|
||||||
my $linenum = 0;
|
|
||||||
while( my $iterline = <$info>) {
|
while( my $iterline = <$info>) {
|
||||||
#print "in first while loop\n";
|
#print "in first while loop\n";
|
||||||
$linenum++;
|
$linesnum++;
|
||||||
}
|
}
|
||||||
close $file;
|
|
||||||
|
|
||||||
open $info, $file or die "Could not open $file: $!";
|
# wqe need to close the filehandle or we won't be able to read the file again
|
||||||
|
close $path;
|
||||||
|
|
||||||
|
# open our filehandle, this time for taking action
|
||||||
|
open $info, $path or die "Could not open $path: $!";
|
||||||
my $xfernum = 0;
|
my $xfernum = 0;
|
||||||
# read line by line
|
# read line by line
|
||||||
while( my $line = <$info> ) {
|
while( my $line = <$info> ) {
|
||||||
@@ -75,15 +87,15 @@ elsif (length $file && length $host && length $user && length $dest && length $d
|
|||||||
# actual copy logic
|
# actual copy logic
|
||||||
if (length $verbose) {
|
if (length $verbose) {
|
||||||
#print "in verbose section";
|
#print "in verbose section";
|
||||||
print "\n\nTransferring object $xfernum of $linenum\n\n";
|
print "\n\nTransferring object $xfernum of $linesnum\n\n";
|
||||||
system("rsync", "-vvv", "-$flags", "$directory/$line", "$user\@$host:$dest");
|
system("rsync", "-vvv", "-$flags", "$dir/$line", "$user\@$host:$dest");
|
||||||
} else {
|
} else {
|
||||||
#print "in non-verbose section";
|
#print "in non-verbose section";
|
||||||
print "\n\nTransferring object $xfernum of $linenum\n\n";
|
print "\n\nTransferring object $xfernum of $linenum\n\n";
|
||||||
system("rsync", "-$flags", "$directory/$line", "$user\@$host:$dest");
|
system("rsync", "-$flags", "$dir/$line", "$user\@$host:$dest");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
close $file;
|
close $path;
|
||||||
|
|
||||||
}
|
}
|
||||||
# if we fail the variables check, print usage help
|
# if we fail the variables check, print usage help
|
||||||
@@ -94,15 +106,16 @@ else {
|
|||||||
|
|
||||||
# help subroutine
|
# help subroutine
|
||||||
sub help {
|
sub help {
|
||||||
print "Usage: $ARGV[0] --file= --host= --directory= --user= --dest= --flags= [--verbose] [--help]\n
|
print "Usage: $ARGV[0] --path <infile or directory> [--dir] --host <IP or DNS name> --user <username> --dest <destination path> [--flags <rsync flags>] [--verbose] [--help]\n
|
||||||
--file - Input file to read with list of files/folders to transfer \n
|
--path - Input file to read or directory to copy
|
||||||
--host - IP address or hostname of the remote system\n
|
--host - IP address or hostname of the remote system\n
|
||||||
--directory - Relative or absolute path to the folder containing the files/folders to transfer, \n
|
--dir - enable directory copy mode\n
|
||||||
with no trailing slash\n
|
|
||||||
--user - User account on the remote system to use\n
|
--user - User account on the remote system to use\n
|
||||||
--dest - Destination on the remote system to copy files to\n
|
--dest - Destination on the remote system to copy files to\n
|
||||||
--flags - rsync flags to use when copying the files over\n
|
--flags - rsync flags to use when copying the files over. Defaults to rvz\n
|
||||||
--verbose - run rsync with -vvv\n
|
--verbose - run rsync with -vvv\n
|
||||||
--help - show this help\n";
|
--help - show this help\n";
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
transfer();
|
||||||
Reference in New Issue
Block a user