-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush.sh
309 lines (285 loc) · 12.8 KB
/
push.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
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
#/*-------------------------------------------------------------------
#Author: Aaron Anthony Valoroso
#Date: January 2nd, 2010
#License: GNU GENERAL PUBLIC LICENSE
#Email: [email protected]
#--------------------------------------------------------------------*/
# 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...."
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"
top_directory=""
item_type=""
# 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
# 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 "Pushing: $argument"
echo "--------------------"
echo "Step-1: Packaging items and Transfering to server."
top_directory=$(pwd | sed 's|.*/||')
cd $current_directory
if [ -d "$current_directory/$argument" ]; then
item_type="dir"
else
item_type="file"
fi
# Concatenate the current working directory with the incoming argument to
# - get the full absolute path. Then check to make sure that the incoming
# - argument is either a directory or file, else exit. After that, I get
# - the number of files / folders of the argument, and will compress the
# - incoming argument and transfer it to the server. If there was an error,
# - print all of the output.
absolute_path="$current_directory/$argument"
if [ -d $absolute_path ] || [ -f $absolute_path ]; then
argument_size=$(find $current_directory -name $argument | wc -l | tr -d ' ')
if ! $compression transfer.tar.gz $argument 2> error_output.txt ; then cleanup; fi
if ! scp transfer.tar.gz $username@$ip_address:~/Transfer 2> error_output.txt 1> output.txt; then cleanup; fi
if ! rm transfer.tar.gz 2> error_output.txt ; then cleanup; fi
if [ -f error_output.txt ]; then rm error_output.txt; fi
if [ -f output.txt ]; then
if [ "$(uname -s)" == "Darwin" ]; then
sed -i '' 's/^/ /' output.txt
elif [ "$(uname -s)" == "Linux" ]; then
sed -i 's/^/\t/' output.txt
fi
if [ $error_switch -eq 1 ]; then
cat output.txt
fi
rm output.txt
fi
echo "Finished."
echo "Step-2: Replacing items in server."
else
echo "File / Directory could not be found on Client..." 1> error_output.txt
cleanup
fi
# This is the main guts of this script. In the first section of this heredoc I create
# - a cleanup function that has a different than the previous cleanup function. This function
# - will help clean up any time a command fails or a trap has been called. Then the next section
# - we will move to the Transfer directory and decompress the packaged item, and then remove the
# - archive file. Next, I will get the contents of the directory which should only be the one
# - file or folder, which I will then look for in the storage directory. The next section I make
# - sure that everything had made it over to the server and if not then cleanup. The next section
# - there are only three possible outcomes; the first is that the argument to be replace has only
# - been found zero times (which is an error), greater than two times (which is an error), and then
# - lastly only two occurences were found. In this if statment I make sure which element in the array
# - to remove and the absolute path to replace with. Lastly, then 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/$storage_location ]; then
echo "There was a problem with your storage location..." > error_output.txt
cleanup2
fi
if ! cd \$HOME/Transfer 2> \$HOME/Transfer/error_output.txt ; then cleanup2; fi
if ! $decompression transfer.tar.gz -m 2> error_output.txt ; then cleanup2; fi
if ! rm transfer.tar.gz 2> error_output.txt ; then cleanup2; fi
if [ -f error_output.txt ]; then rm error_output.txt; fi
argument=\$(ls)
argument_size_2=\$(find \$HOME/Transfer -name "\$argument" | wc -l)
if [ "\$argument_size_2" != "$argument_size" ]; then
pwd 1>> error_output.txt
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 to the darkside." 1>> error_output.txt
cleanup2
fi
if [ $item_type == "dir" ]; then
array=(\$(find \$HOME/$storage_location -type d -name "\$argument"))
elif [ $item_type == "file" ]; then
array=(\$(find \$HOME/$storage_location -type f -name "\$argument"))
else
array=(\$(find \$HOME/$storage_location -name "\$argument"))
fi
len=\${#array[*]}
if [ \$len == 0 ]; then
echo "Adding File / Directory to collection-1..."
if ! mv \$argument \$HOME/$storage_location 2> error_output.txt ; then cleanup2; fi
if [ -f error_output.txt ]; then rm error_output.txt; fi
elif [ \$len -ge 2 ]; then
array=(\$(find \$HOME/$storage_location -type d -name "$top_directory"))
len=\${#array[*]}
if [ \$len == 0 ] || [ \$len -ge 2 ]; then
echo "There is more than one File / Directory that have the same name..." 1> error_output.txt
cleanup2
elif [ \$len -eq 1 ]; then
if ! cd \${array[0]} 2> \${array[0]}/error_output.txt ; then cleanup2; fi
if [ -f error_output.txt ]; then rm error_output.txt; fi
temp=\$(pwd)
array=(\$(find \$temp -name "\$argument"))
len=\${#array[*]}
if [ \$len == 1 ]; then
echo "Replacing File / Directory to collection-1: \${array[0]}..."
if [ -f "\$argument" ]; then
if ! rm "\$argument" 2> error_output.txt ; then cleanup2; fi
elif [ -d "\$argument" ]; then
if ! rm -rf "\$argument" 2> error_output.txt ; then cleanup2; fi
fi
if ! mv \$HOME/Transfer/\$argument . 2> error_output.txt ; then cleanup2; fi
if [ -f error_output.txt ]; then rm error_output.txt; fi
else
echo "There is more than one File / Directory that have the same name..." 1> error_output.txt
cleanup2
fi
fi
elif [ \$len -eq 1 ]; then
echo "Replacing File / Directory to collection-2: \${array[0]}..."
absolute_path=$(echo \${array[0]} | sed 's|\(.*\)/.*|\1|')
if [ -d \${array[0]} ]; then
if ! rm -rf \${array[0]} 2> error_output.txt ; then cleanup2; fi
elif [ -f \${array[0]} ]; then
if ! rm \${array[0]} 2> error_output.txt ; then cleanup2; fi
fi
if ! mv \$argument \$absolute_path 2> error_output.txt ; then cleanup2; fi
fi
rm -rf \$HOME/Transfer/*
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, then print the "Finished." message and
# - remove the output.txt file if it exists.
error=$(tail -1 output.txt)
if [ "$error" == 'Exiting...' ]; then
echo "Found errors in the heredoc..."
cat output.txt 1> error_output.txt
if [ -f output.txt ]; then
rm output.txt
fi
cleanup
else
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
fi
echo "Finished."
if [ -f output.txt ]; then
rm output.txt
fi
done
echo "--------------------"
exit
# Useful URL's:
# - https://www.tutorialspoint.com/unix/unix-basic-operators.htm
# - https://zaiste.net/a_few_ways_to_execute_commands_remotely_using_ssh/