-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpull.sh
More file actions
341 lines (315 loc) · 14.5 KB
/
pull.sh
File metadata and controls
341 lines (315 loc) · 14.5 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/bin/bash
#--------------------------------------------------------------------
#Author: Aaron Anthony Valoroso
#Date: january 2nd, 2019
#License: GNU GENERAL PUBLIC LICENSE
#Email: valoroso99@gmail.com
#--------------------------------------------------------------------
# This function is used to clean up wherever the script is at. This function can be
# - ran when a command fails or when the user does a control c and etc.
cleanup () {
if [ -f error_output.txt ]; then
echo -e "\tHere is what caused the error: "
if [ "$(uname -s)" == "Darwin" ]; then
sed -i '' 's/^/ /' error_output.txt
elif [ "$(uname -s)" == "Linux" ]; then
sed -i 's/^/\t/' error_output.txt
sed -i 's/^/\t/' error_output.txt
fi
cat error_output.txt
rm error_output.txt
fi
echo -e "\tCleaning up...."
if [ -d $HOME/Transfer ]; then
rm -rf $HOME/Transfer
fi
echo "Exiting..."
cd $current_directory || cd $HOME
exit
}
trap cleanup 1 2 3 6
#--------------------------------------------------------------------
# This function is used to pull the information from the .locations file in order
# - setup the ssh calls. How this function works is by locating the file line number
# - of the head name of the ip_address and username. Then grab the next two lines and
# - pull the credentials that we will need. Once you grab the line, cut everything before
# - the equal sign and save.
credentials () {
if [ -f $HOME/.monday/.locations ]; then
line=$(grep -n "$1" $HOME/.monday/.locations | cut -d : -f 1)
if [ ! -z "$line" ] || [ "$line" != "" ]; then
line=$((line+1))
username2=$(sed -n $line'p' $HOME/.monday/.locations | awk -F'=' '{print $2}')
if [ -z "$username2" ] || [ "$username2" == "" ]; then
echo "There was an issue with getting the username that you requested..."
exit
else
username=$username2
fi
line=$((line+1))
ip_address2=$(sed -n $line'p' $HOME/.monday/.locations | awk -F'=' '{print $2}')
if [ -z "$ip_address2" ] || [ "$ip_address2" == "" ]; then
echo "There was an issue with getting the password that you requested..."
exit
else
ip_address=$ip_address2
fi
fi
fi
}
#--------------------------------------------------------------------
incoming_items=()
error_switch=0
current_directory=$(pwd)
ip_address=""
username=""
storage_location="Documents/storage"
compression="tar -czf"
decompression="tar -xzf"
# Get the credentails from the file.
credentials "DEFAULT"
#--------------------------------------------------------------------
# The following section of code will check the following arguments that are
# - passed to the following script. There are a total of four different arguments
# - that can be passed to this script. Please just type the alias name "push" or
# - "pull" then "-h" or "--h" to get more information about the possible arguments
# - that can be passed. For everything else that is passed in we will want to
# - check the validity, then I check for the ending and beginning forward slash.
# - The beginning slash needs to be there (so added) and the last forward slash
# - (removed) does not need to be there. It will be added to the array.
while [ $# -gt 0 ];
do
if [ "$1" == "-error" ]; then
error_switch=1
compression="tar -czvf"
decompression="tar -xzvf"
elif [ "$1" == "-storage" ]; then
shift
store=$1
if [ "${store:0:1}" == '/' ]; then store="${store:1}"; fi
if [ "${store: -1}" == '/' ]; then store="${store::-1}"; fi
storage_location=$store
elif [ "$1" == "-remote" ]; then
shift
incoming_argument=$(echo $1 | awk '{print toupper($0)}')
credentials $incoming_argument
elif [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
cat $HOME/.monday/.usage | more
exit
else
argument=$1
if [ ! -z "$argument" ] || [ "$argument" != "" ]; then
if [ "${argument:0:1}" = '-' ]; then
echo "There was a problem with the following argument: $argument"
echo "Moving forward."
else
if [ "${argument:0:1}" == '/' ]; then argument="${argument:1}"; fi
if [ "${argument: -1}" == '/' ]; then argument="${argument::-1}"; fi
incoming_items+=("$argument")
fi
else
echo "There was a problem with the following argument: $argument"
echo "Moving forward."
fi
fi
shift
done
# This will be a place to put all the files to transfer from the server.
# - So we have to check to see if the directory already exits in the specific
# - location and if not then create the directory. Then change into that
# - directory.
if ! [ -d $HOME/Transfer ]; then
if ! mkdir $HOME/Transfer 2> error_output.txt ; then cleanup; fi
fi
if ! cd $HOME/Transfer 2> error_output.txt ; then cleanup; fi
# Make sure that there are arguments to process and pass along.
if [ ${#incoming_items[*]} == 0 ]; then
echo "There were zero arguments passed to the script..."
echo "Exiting..."
exit
fi
for argument in "${incoming_items[@]}"
do
echo "--------------------"
echo "Pulling: $argument"
echo "--------------------"
echo "Step-1: Locating File on server."
# This is the main part in the script and we are finding the file on ther server in order
# - to pull to the client. In the first part of this section of code we are creating a
# - cleanup function that has a different name as the previoius clenaup function. I then
# - setup the trap for any possiblity of a control sequenece from the user. In the next
# - section of the code we move to the Transfer directory, and then try to find the number
# - of occurences of the directory or file that the user is looking for. There is either
# - zero occurences, to many occurences, or two occurences. The two occurences show up
# - once in the Transfer and in the storage directory. In the second if statment we move
# - to the location above the item that the user is looking for, and package it up. Lastly,
# - we print the number of files and directory for after the heredoc, and do some clean up.
ssh $username@$ip_address -T 1> output.txt << EOSSH
cleanup2 () {
if [ -f error_output.txt ]; then
cat error_output.txt
rm error_output.txt
fi
if [ -d \$HOME/Transfer ]; then
rm -rf \$HOME/Transfer/*
fi
echo "Exiting..."
exit
}
trap cleanup 1 2 3 6
if [ ! -d \$HOME/Transfer ]; then
if ! mkdir \$HOME/Transfer 2> \$HOME/Transfer/error_output.txt ; then cleanup2; fi
fi
if ! cd \$HOME/Transfer 2> \$HOME/Transfer/error_output.txt ; then cleanup2; fi
if [ ! -d \$HOME/$storage_location ]; then
echo "There was a problem with your storage location..." > error_output.txt
cleanup2
fi
array=(\$(find \$HOME/$storage_location -name "$argument"))
len=\${#array[*]}
if [ \$len == 0 ]; then
echo "There were no files found with that given name..." 1> error_output.txt
cleanup2
elif [ \$len -eq 1 ]; then
item=\${array[0]}
item=$(echo \${item%/*})
if ! cd \$item 2> error_output.txt ; then cleanup2; fi
if ! $compression \$HOME/Transfer/transfer.tar.gz $argument 2> error_output.txt ; then cleanup2; fi
lines=\$(find "\${array[0]}" | wc -l)
echo \$lines
elif [ \$len -ge 2 ]; then
echo "There were more than one file found with that name..." 1> error_output.txt
echo -e "\nHere are all the other items that we found with the same name." 1>> error_output.txt
for i in "\${!array[@]}"
do
echo -e "\t\$i" "\${array[\$i]}" 1>> error_output.txt
done
echo "Try pulling the upper directory of the item." 1>> error_output.txt
cleanup2
fi
if [ -f error_output.txt ]; then
rm error_output.txt
fi
exit
EOSSH
# Here we will check to see if the previous code ran into an error. If it did
# - Then print an error message, remove the "Exiting..." from the end of the file
# - and if the output.txt file is there then remove it. Lastly, we will call the
# - cleanup to exit the script.
error=$(tail -1 output.txt)
if [ "$error" == 'Exiting...' ]; then
echo "Found errors in the heredoc..."
head -n -1 output.txt 1> error_output.txt
if [ -f output.txt ]; then
rm output.txt
fi
cleanup
fi
# If there were no errors then the last line in the code should be the number of
# - lines that the system is transferring over to the client. Next, we remove the
# output file if it exists, and then print the following messages.
argument_size=$(tail -1 output.txt)
if [ -f output.txt ]; then
rm output.txt
fi
echo "Finished."
echo "Step-2: Pulling File"
# Use scp to transfer the compressed files over to the client. Then save any output
# - to a file. I have to use scp because there is no other way to pull / push files
# - to the client from the remote system. This format of the following line is to
# - check the validity of the command and if it fails then we run the cleanup.
if ! scp -r $username@$ip_address:"~/Transfer/transfer.tar.gz" . 2> error_output.txt 1> output.txt ; then cleanup; fi
# Decompress the file and then remove the archive file. These next two lines also
# - have the same format as the previous line, and if they fail then call the clean
# - up function. We also store the output of the decompression command and if the
# - error switch is on, then print out the output.
if ! $decompression transfer.tar.gz -m 2> error_output.txt 1> output.txt ; then cleanup; fi
if ! rm transfer.tar.gz 2> error_output.txt ; then cleanup; fi
if [ $error_switch -eq 1 ]; then
echo "Decompression output..."
if [ "$(uname -s)" == "Darwin" ]; then
sed -i '' 's/^/ /' output.txt
elif [ "$(uname -s)" == "Linux" ]; then
sed -i 's/^/\t/' output.txt
fi
cat output.txt
if [ ! -f output.txt ]; then
rm output.txt
fi
fi
echo "Finished."
# Get the number of lines of all the files and directories that was transfered over
# - just to make sure that we got everything from the server. If the number from the
# - server that we got does not match then there was an issue with the transfer. If
# - there is an issue and we are not running the test then pring an error message,
# - else print a 1. The one again signifies that there was an issue to the tests. Then
# - we make sure to change back to the home directory and remove the Transfer directory.
argument_size_2=$(find $argument | wc -l)
if [ $argument_size != $argument_size_2 ]; then
echo "Argument-1: $argument_size" 1> error_output.txt
echo "Argument-2: $argument_size_2" 1>> error_output.txt
echo "Not everything made it over from the dark side." 1>> error_output.txt
cleanup
fi
# Try and find the file that is getting added to the client computer to see if
# - it is already in the file system. Now I have the starting point in /home,
# - because you will get permission warnings if you dig through the root directories.
# - You can change this but just keep in mind that this search will take a little bit
# - longer to search through.
array=($(find $HOME -name "$argument"))
len=${#array[*]}
# If the find command has found the file or directory (once) on the file system then it
# - has found the one that got scp to the Transfer directory. If the find command
# - has found the file or directory (twice) on the file system then it has found it in
# - the Transfer directory and the location that the user is looking to swap out for the
# - one on the server. If the find command has found anymore than three, then there is to
# - many locations. In the second if statment I have to figure out what each item is in the
# - array in order to know which one to replace and which one to remove.
if [ $len == 1 ]; then
echo "Step-3: Adding File / Directory to local collection..."
echo "Finished."
if ! mv $argument $current_directory 2> error_output.txt ; then cleanup; fi
elif [ $len == 2 ]; then
echo "Step-3: Replacing File / Directory to collection..."
echo "Finished."
search="$current_directory/$argument"
if [ $search == ${array[0]} ]; then
destroy=$search
elif [ $search == ${array[1]} ]; then
destroy=$search
fi
absolute_path=$(echo $destroy | sed 's|\(.*\)/.*|\1|')
if [ -d $destroy ]; then
if ! rm -rf $destroy 2> error_output.txt ; then cleanup; fi
if ! mv $argument $absolute_path 2> error_output.txt ; then cleanup; fi
elif [ -f $destroy ]; then
if ! rm $destroy 2> error_output.txt ; then cleanup; fi
if ! mv $argument $absolute_path 2> error_output.txt ; then cleanup; fi
fi
else
echo "ERROR: There were more than one file found with that name..." 1> error_output.txt
cleanup
fi
if ! rm -rf $HOME/Transfer/* 2> error_output.txt ; then cleanup; fi
# Clean up the folder on the server. This could not be done in the previous
# - ssh because we need to scp over the tar'd file.
ssh $username@$ip_address -T << EOSSH
rm -rf \$HOME/Transfer/*
EOSSH
done
# Put the user back into their current directory that they were working from.
cd $current_directory || cd $HOME
echo "--------------------"
# Clean up our transfer directory on the client.
if [ -d $HOME/Transfer ]; then
rm -rf $HOME/Transfer
fi
# Clean up any possible error files on the client.
if [ -f error_output.txt ]; then
rm error_output.txt
fi
exit
# Useful URL's:
# - https://www.digitalocean.com/community/tutorials/how-to-use-sftp-to-securely-transfer-files-with-a-remote-server
# - https://www.tutorialspoint.com/unix/unix-basic-operators.htm
# - https://zaiste.net/a_few_ways_to_execute_commands_remotely_using_ssh/