-
Notifications
You must be signed in to change notification settings - Fork 4
/
remote_copy.sh
executable file
·115 lines (101 loc) · 4.16 KB
/
remote_copy.sh
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# -----------------------------------------------------------------------------
# Copyright (C) Business Learning Incorporated (businesslearninginc.com)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License at <http://www.gnu.org/licenses/> for
# more details.
# -----------------------------------------------------------------------------
#
# A bash script to remotely copy file(s)/folder(s) using scp
#
# requirements:
# --sshpass command installed
# --jq (json query) command installed
#
# inputs:
# --username
# --user password (optional)
# --scp port (default:22)
# --website/server (domain name) to access
# --file(s)/folder(s) source location
# --file(s)/folder(s) destination location
#
# outputs:
# --notification of success/failure (error code 0/1 passed on exit)
# --side-effect: moved file(s)/folder(s)
#
# -----------------------------------------------------------------------------
# script configuration
#
shopt -s extglob
EXEC_DIR="$(dirname "$0")"
# shellcheck source=bash-lib/args
source "${EXEC_DIR}/bash-lib/args"
# [user-config] set any external program dependencies here
declare -a REQ_PROGRAMS=('jq' 'sshpass')
# -----------------------------------------------------------------------------
# perform script configuration, arguments parsing, and validation
#
check_program_dependencies "${REQ_PROGRAMS[@]}"
display_banner
scan_for_args "$@"
check_for_args_completeness
# -----------------------------------------------------------------------------
# perform remote file(s)/folder(s) copy
#
ARG_PORT=$(get_config_arg_value port)
PASSWORD=$(get_config_arg_value password)
TMP_DIR=$(mktemp -d)
SRC_VALUE=$(get_config_arg_value source)
if [ -z ${PASSWORD} ]; then
# no password passed in arguments, so use scp
#
echo "Copying remote file(s)/folder(s) using scp..."
echo
scp -r -P "${ARG_PORT:-22}" "$(get_config_arg_value username)"@"$(get_config_arg_value website)":"$(get_config_arg_value source)" "${TMP_DIR}" &>/dev/null;
RETURN_CODE=$?
else
# password passed in arguments, so use sshpass
#
echo "Copying remote file(s)/folder(s) using sshpass..."
echo
sshpass -p "$(get_config_arg_value password)" scp -r -P "${ARG_PORT:-22}" "$(get_config_arg_value username)"@"$(get_config_arg_value website)":"$(get_config_arg_value source)" "${TMP_DIR}" &>/dev/null;
RETURN_CODE=$?
fi
# check for error codes, move file(s)/folder(s) to final destination, and clean up
#
if [ ${RETURN_CODE} -ne 0 ]; then
rm -rf "${TMP_DIR}"
if [ -z ${PASSWORD} ]; then
echo "Error: scp return code ${RETURN_CODE} encountered (see https://linux.die.net/man/1/scp for details)."
else
echo "Error: sshpass return code ${RETURN_CODE} encountered (see http://linux.die.net/man/1/sshpass for details)."
fi
echo "Remote file(s)/folder(s) copy failed."
quit 1
else
if [ "$(get_config_details compress_results)" == "true" ]; then
ARCHIVE="$(get_config_arg_value website)"-"$(basename ${SRC_VALUE%% *})"-"$(date +"%Y%m%d%H%M%S")".tar.gz
# TODO should be a better way to archive relative folders (-C option fails)
(CWD=$PWD && cd ${TMP_DIR} && (tar -zcf ${ARCHIVE} *) && cd ${CWD})
mv "${TMP_DIR}"/${ARCHIVE} "$(get_config_arg_value destination)" &>/dev/null;
echo "Success."
echo "Remote file(s)/folder(s) $(get_config_arg_value website):$(get_config_arg_value source) archived and copied to $(get_config_arg_value destination)/${ARCHIVE}"
rm -rf "${TMP_DIR}"
else
mv "${TMP_DIR}"/* "$(get_config_arg_value destination)" &>/dev/null;
echo "Success."
echo "Remote file(s)/folder(s) $(get_config_arg_value website):$(get_config_arg_value source) copied to $(get_config_arg_value destination)/"$(basename ${SRC_VALUE%% *})"."
rm -rf "${TMP_DIR}"
fi
quit 0
fi