Skip to content

Backward-compatible upgrade to remote-folder-copy, a bash script to securely copy file(s)/folder(s) from a remote server

License

Notifications You must be signed in to change notification settings

richbl/remote-copy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Remote-Copy

Remote-Copy (remote_copy.sh) is a bash script to remotely copy file(s)/folder(s) using the secure remote copy (scp) command, or the less-secure sshpass command when the localhost user is not certificate-authenticated on the remote server.

run_remote_copy.sh is a related wrapper script intended to be used for making unattended script calls into remote_copy.sh (e.g., running cron jobs).

Note that Remote-Copy is a backward-compatible upgrade of the Remote-Folder-Copy project. In general, you will be better served using this project, as Remote-Folder-Copy will likely no longer be maintained in favor of this project.

Remote-Copy uses a bash template (BaT) called A-Bash-Template designed to make script development and command line argument management more robust, easier to implement, and easier to maintain. Here are a few of those features:

  • Dependencies checker: a routine that checks all external program dependencies (e.g., sshpass and jq)
  • Arguments and script details--such as script description and syntax--are stored in the JSON file format (i.e., config.json)
  • JSON queries (using jq) handled through wrapper functions
  • A script banner function automates banner generation, reading directly from config.json
  • Command line arguments are parsed and tested for completeness using both short and long-format argument syntax (e.g., -u|--username)
  • Optional command line arguments are permissible and managed through the JSON configuration file
  • Template functions organized into libraries to minimize code footprint in the main script

For more details about using a bash template, check out the BaT sources here.

Requirements

  • An operational bash environment (bash 4.3.46 used during development)
  • Two additional external programs:
    • sshpass, for piping password into the scp command when scp cannot be used directly (e.g., localhost user is not certificate-authenticated on the remote server)
    • jq, for parsing the config.json file

While this package was written and tested under Linux (Ubuntu 16.10), there should be no reason why this won't work under other Unix-like operating systems.

Basic Usage

Remote-Copy is run through a command line interface, so all of the command options are made available there.

Here's the default response when running remote_copy.sh with no arguments:

  $ ./remote_copy.sh

   |
   | A bash script to remotely copy file(s)/folder(s) using scp
   |   0.7.1
   |
   | Usage:
   |   remote_copy -u username [-p password] -w website [-P port] -s source(s) -d destination
   |
   |   -u, --username 		username (must have local/remote permissions)
   |   -p, --password 		password used to access remote server (optional)
   |   -w, --website 		website domain name (e.g., example.com)
   |   -P, --port 			website/server SSH port
   |   -s, --source 		source file(s)/folder(s) path (quote mult. sources, "/path/file1 /path/folder2")
   |   -d, --destination 	destination folder path
   |

  Error: username argument (-u|--username) missing.
  Error: website argument (-w|--website) missing.
  Error: source argument (-s|--source) missing.
  Error: destination argument (-d|--destination) missing.

In this example, the program responds by indicating that the required script arguments must be set before proper operation.

When arguments are correctly passed, the script provides feedback on the success or failure of the remote folder copy:

$ ./remote_copy.sh -u user -p 'pass123' -w example.com -s /home/user/src -d /home/user/desktop

   |
   | A bash script to remotely copy file(s)/folder(s) using scp
   |   0.7.1
   |
   | Usage:
   |   remote_copy -u username [-p password] -w website [-P port] -s source(s) -d destination
   |
   |   -u, --username 		username (must have local/remote permissions)
   |   -p, --password 		password used to access remote server (optional)
   |   -w, --website 		website domain name (e.g., example.com)
   |   -P, --port 			website/server SSH port
   |   -s, --source 		source file(s)/folder(s) path (quote mult. sources, "/path/file1 /path/folder2")
   |   -d, --destination 	destination folder path
   |

Copying remote file(s)/folder(s) using scp...

Success.
Remote file(s)/folder(s) example.com:/home/user/src copied to /home/user/desktop/src.

Copying Multiple Files/Folders

Remote-Copy (remote_copy.sh) permits for the remote copy of any number of files and/or folders to be copied from a remote server. It does this by taking advantage of the scp command syntax defining a source object. In that syntax, multiple source objects (either files or folders) are identified individually using the space character ( ) to delimit each element. Remote-Copy (remote_copy.sh) uses that same syntax, but requires the --s, --source(s) script argument to be wrapped in quotation marks. ("). Quotation marks are optional if only one source element is passed in.

For example, in the command sequence below, both a file and a folder are identified as source objects to be remotely copied:

$ ./remote_copy.sh -u user -p 'pass123' -w example.com -s "/remote/user/srcfile /remote/user/another_location/srcfolder" -d /home/user/desktop

This syntax is slightly different if only one source element is passed on the command line:

$ ./remote_copy.sh -u user -p 'pass123' -w example.com -s "/remote/user/srcfile" -d /home/user/desktop

As noted, when passing in only one source element, the quotation marks are optional:

$ ./remote_copy.sh -u user -p 'pass123' -w example.com -s /remote/user/srcfile -d /home/user/desktop

This optional syntax provides a level of backward compatibility with this project's predecessor, the Remote-Folder-Copy project.

Choosing Between the scp and sshpass Commands

Remote-Copy (remote_copy.sh) uses two methods for copying folders from a remote server: scp and sshpass. The decision on which command to use is based on whether the -p, --password argument has been passed into the script. If no password is used, then the scp command is used directly. If a password is passed into the script, then the sshpass command is used instead.

In general, it's preferred to use scp with a current certificate of authority in place on the remote server. For the security-minded, users of this script should review the section entitled Security Considerations on the sshpass website.

IMPORTANT: This Project Uses Git Submodules

This project uses Git submodule project(s) (located in the bash-lib folder) to keep this project up-to-date without manual intervention.

Be sure to clone this project with the --recursive switch (git clone --recursive https://github.com/richbl/this_project) so any submodule project(s) will be automatically cloned as well. If you clone into this project without this switch, you'll likely see empty submodule project folders (depending on your version of Git).

About

Backward-compatible upgrade to remote-folder-copy, a bash script to securely copy file(s)/folder(s) from a remote server

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages