Repository navigation
Expand file tree
/
Copy pathqadrsc.sh
More file actions
executable file
·138 lines (122 loc) · 4.06 KB
/
Copy pathqadrsc.sh
File metadata and controls
executable file
·138 lines (122 loc) · 4.06 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env bash
# ============================================================================
# qadrsc - Quick-and-dirty remote secure copy script
# Copyright (C) 2020 by Ralf Kilian
# Distributed under the MIT License (https://opensource.org/licenses/MIT)
#
# GitHub: https://github.com/urbanware-org/qadrsc
# GitLab: https://gitlab.com/urbanware-org/qadrsc
# ============================================================================
version="1.2.2"
# This allows to set a default user which is used by default in case no user
# is explicitly on the command-line using '@'
remote_user_default=""
parse_path() {
input_path="$1"
path_is_remote=0
egrep "@|:" <<< $input_path &>/dev/null
if [ $? -eq 0 ]; then
path_is_remote=1
remote_user="$(echo "$input_path" | cut -d '@' -f 1)"
remote_target="$(echo "$input_path" | cut -d '@' -f 2)"
remote_ip="$(echo "$remote_target" | cut -d ':' -f 1)"
if [ "$remote_user" = "$remote_target" ]; then
if [ ! -z "$remote_user_default" ]; then
# No specific user was given, so use the default one set above
remote_user="$remote_user_default"
else
# The remote user and target are identical if no user was
# given and in case there no default user is set above, use
# the username of the current user
remote_user="$(whoami)"
fi
fi
if [ -z "$remote_user" ]; then
# In case an '@' was given without a preceding username
usage "Remote username missing"
fi
# The leading slash prevents using relative paths
remote_path="/$(echo "$remote_target" | cut -d ':' -f 2)"
fi
}
usage() {
script_file=$(basename "$0")
echo "usage: $script_file SOURCE_PATH DESTINATION_PATH"
echo
echo "required arguments:"
echo " SOURCE_PATH source path of the on the local or remote"\
"system"
echo " DESTINATION_PATH destination path on the local or remote"\
"system where"
echo " to copy the files to"
echo
echo "Notice that when using asterisks (*) in the source path, the path"\
"must either"
echo "be enclosed with single (') or double (\") quotes."
echo
echo "The remote path syntax is identical with the one from 'rsync' and"\
"'scp',"
echo "e. g. \"johndoe@192.168.2.1:/etc\"."
echo
echo "Further information can be found inside the 'README' file, for"\
"usage examples"
echo "see the 'USAGE' file."
if [ -z "$1" ]; then
exit 0
else
echo
echo "error: $1"
exit 1
fi
}
if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
usage
elif [ "$1" = "--version" ]; then
echo "$version"
exit 0
elif [ $# -ne 2 ]; then
usage "Invalid number of arguments (2 required)"
fi
# Parse source path
parse_path "$1"
if [ $path_is_remote -eq 1 ]; then
src_is_remote=1
src_user="$remote_user"
src_ip="$remote_ip"
src_path="$remote_path"
else
src_is_remote=0
src_path=$1
fi
# Parse destination path
parse_path "$2"
if [ $path_is_remote -eq 1 ]; then
if [ $src_is_remote -eq 1 ]; then
usage "Source and destination must not both be a remote path"
fi
dst_is_remote=$path_is_remote
dst_user="$remote_user"
dst_ip="$remote_ip"
dst_path="$remote_path"
else
if [ $src_is_remote -eq 0 ]; then
usage "Neither source nor destination is a remote path"
fi
dst_path=$2
fi
if [ $src_is_remote -eq 0 ]; then
dir_current=$(pwd)
file_source=$(sed -e "s/.*\///g" <<< $1)
dir_source=$(sed -e "s/$file_source$//g" <<< $1)
if [ -z "$dir_source" ]; then
dir_source=$(pwd)
fi
cd $dir_source
tar -c ./$file_source | ssh ${dst_user}@${dst_ip} \
"sudo tar -x --no-same-owner -C $dst_path"
cd $dir_current
else
# Simply use 'scp' to handle that way
scp -q -r ${src_user}@${src_ip}:$src_path $dst_path
fi
# EOF