-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3_tool.sh
447 lines (382 loc) · 13.2 KB
/
s3_tool.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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#!/bin/bash
# command examples:
# ./s3_tool.sh set_acl_from_file --acl 'private' --key_file 's3__.log'
# set acl (private) for each line from file s3__.log
# ./s3_tool.sh set_acl_buckets --buckets 'bucket_name1 bucket_name2' --acl 'private'
# set acl for buckets
# ./s3_tool.sh set_acl_prefix --buckets 'bucket_name' --prefix 'demo' --acl 'public-read-write'
# set acl for bucket and prefix
# ./s3_tool.sh scan_buckets --buckets 'bucket_name1 bucket_name2'
# scan buckets
# ./s3_tool.sh scan_prefix --buckets 'bucket_name1' --prefix 'prefix'
# scan bucket with prefix
# ./s3_tool.sh scan_via_tag
# scan bucket via tag
# ./s3_tool.sh get_s3_tag --tag_for_scan 'scan'
# get s3 bucket with tag
# ./s3_tool.sh copy_to_bucket --key_file file_name --new_bucket bucket_name
# copy objects keys from file to new_bucket
# ./s3_tool.sh get_keys_bucket_prefix --key_file test_keys --buckets bucket_name --prefix prefix_name
# get keys list from bucket and prefix
# ./s3_tool.sh get_keys_bucket --key_file test_keys --buckets bucket_name
# get keys list from bucket and prefix
#./s3_tool.sh check_keys_file --source_file /tmp/product_photo2.txt --bucket_file test_keys --dif_file diff_file
# find each key from source_file and find it in bucket_file . if key not exist add it to dif_file
# flags :
# --acl
#private
#public-read
#public-read-write
#authenticated-read
#aws-exec-read
#bucket-owner-read
#bucket-owner-full-control
# --tag_for_scan - s3 tag for scan
# --log_prefix - prefix for log file
# --buckets - s3 buckets names
# --prefix s3 bucket prefix
# --aws_profile - aws profile from ~/.credentials = default
# --search_term - search word for acl = AllUsers
# --key_file - file name for command set_acl_from_file
command=$1
declare -i max_paralel_proces=21
declare -i delay_after=20
declare -i send_keys=0
max_items=400
wait_sleep=1
start_time=$(date +%F:%H:%M:%S)
aws_profile="default"
tag_for_scan="scan"
search_term="AllUsers"
#function_________________________
function get_s3_buckets {
local bucket_for_scan=$( aws s3 ls --profile $aws_profile |cut -f3 -d' ')
local bucket_for_scan_tag=''
for bucket in $bucket_for_scan
do
tags=$(aws s3api get-bucket-tagging --bucket $bucket --profile $aws_profile 2>$log_error)
result_found=$(echo $tags | jq -r '.TagSet[].Key' | grep "$tag_for_scan")
if [ ! -z "$result_found" ] ; then
bucket_for_scan_tag+="${bucket} "
fi
done
echo "$bucket_for_scan_tag"
}
function check_key {
json=$(aws s3api get-object-acl --bucket $1 --key $2 --profile $aws_profile 2>$log_error)
result_found=$(echo $json | jq -r '.Grants[].Grantee[]' | grep "$search_term")
if [ ! -z "$result_found" ]; then
last_modified=$(aws s3 ls "$1/$2" --profile $aws_profile 2>$log_error | cut -f1 -d' ')
echo "$1/$2 $last_modified">>$log;
echo "$1/$2">>$log_acl;
echo "$json" >> $log_acl
echo '****************' >> $log_acl
echo "$1/$2 --- public -- $last_modified"
fi
}
function set_acl_buckets {
#1 -acl
aws s3api put-object-acl --bucket "$2" --key "$3" --acl $1 --profile $aws_profile 2>> $log_error 1>>$log
}
function s3_cp {
#1 line
#2 new_bucket
from="s3://$1"
old_bucket=$(echo $1 | cut -f1 -d'/')
to="s3://$(echo $1 | sed -e 's/'$old_bucket'/'$2'/'g)"
aws s3 cp $from $to --acl public-read 2>> $log_error 1>>$log
}
function check_keys_file {
#1 source_file
#2 bucket_file
#3 dif_file
rm -f $3
declare -i line=1
all_line=$(wc -l $1)
echo " source_file =$1 bucket_file=$2 dif_file=$3 "
while IFS= read -r LINE || [ -n "$LINE" ];
do
bucket=$(echo $LINE | cut -f1 -d'/' )
key=$(echo $LINE |cut -f1 -d' ' | sed -e 's/'$bucket'\///'g )
result=$(grep "$key" $2)
if [ -z "$result" ];
then
echo $key>>$3
fi
echo " line $line of $all_line "
line+=1
done <$1
}
function get_keys_bucket {
#1 bucket
#2 prefix
#3 file
echo "***** $(date +%F:%H:%M:%S) === backet ===== start_time=$start_time " | tee -a $log_app
echo "$1" | tee -a $log_app
echo "***** $(date +%F:%H:%M:%S) prefix : $(echo $2 | cut -f2 -d ' ') " | tee -a $log_app
bucket=$1
rm -f $3
nexttoken='init'
declare -i all_keys=0
while [ -n "$nexttoken" ]
do
case $nexttoken in
init)
json=$(aws s3api list-objects-v2 --bucket $bucket --profile $aws_profile --max-items $max_items $2 )
;;
*)
json=$(aws s3api list-objects-v2 --bucket $bucket --profile $aws_profile --max-items $max_items $2 --starting-token $nexttoken )
;;
esac
echo $json |jq -r '.Contents[].Key' >>$3
nexttoken=$(echo $json |jq -r '.NextToken')
all_keys+=$max_items
echo "**** get $all_keys keys"
if [[ "$nexttoken" == "null" ]] ; then
nexttoken=''
echo "nexttoken null"
fi
# awk '{print "s3_bucket_name/"$0}' $3
# echo "keys = $(wc -l)"
done
}
function command_buckets {
#1 -buckets
echo "***** $(date +%F:%H:%M:%S) === backets ===== start_time=$start_time " | tee -a $log_app
echo "$1" | tee -a $log_app
echo "***** $(date +%F:%H:%M:%S) bucket amount : $(echo $1 | wc --words )" | tee -a $log_app
echo "***** $(date +%F:%H:%M:%S) prefix : $(echo $2 | cut -f2 -d ' ') " | tee -a $log_app
echo "***** $(date +%F:%H:%M:%S) command = $3 " | tee -a $log_app
for bucket in $1 ; do
declare -i send_backet_keys=0
echo "***** $(date +%F:%H:%M:%S) $3 backet $bucket start_time=$start_time " | tee -a $log_app
nexttoken='init'
declare -i all_keys=0
while [ -n "$nexttoken" ]
do
case $nexttoken in
init)
json=$(aws s3api list-objects-v2 --bucket $bucket --profile $aws_profile --max-items $max_items $2 )
;;
*)
json=$(aws s3api list-objects-v2 --bucket $bucket --profile $aws_profile --max-items $max_items $2 --starting-token $nexttoken )
;;
esac
keys_for_scan=$(echo $json |jq -r '.Contents[].Key')
nexttoken=$(echo $json |jq -r '.NextToken')
if [[ "$nexttoken" == "null" ]] ; then
nexttoken=''
echo "nexttoken null"
fi
for key in $keys_for_scan ; do
declare -i paralel_proces=0
paralel_proces+=$(ps aux | grep aws | wc -l)
while [[ $max_paralel_proces -lt $paralel_proces ]]
do
echo "***** $(date +%F:%H:%M:%S) sleep paralel_proces = $paralel_proces max_paralel_proces=$max_paralel_proces start_time=$start_time " | tee -a $log_app
sleep 1
declare -i paralel_proces=0
paralel_proces+=$(ps aux | grep aws | wc -l)
done
# echo "$bucket/$key"
$3 "$bucket" "$key" &
send_keys+=1
send_backet_keys+=1
all_keys+=1
if [[ ! "$send_keys" -lt "$delay_after" ]] ; then
echo "***** $(date +%F:%H:%M:%S) send $send_keys keys wait_sleep $wait_sleep bucket=$bucket send_backet_keys=$send_backet_keys all_keys=$all_keys start_time=$start_time" | tee -a $log_app
sleep $wait_sleep
declare -i send_keys=0
fi
done
done
echo "***** $(date +%F:%H:%M:%S) done backet $bucket send_backet_keys=$send_backet_keys start_time=$start_time " | tee -a $log_app
done
echo "***** $(date +%F:%H:%M:%S) done all_keys=$all_keys start_time=$start_time " | tee -a $log_app
}
#------ main -------
while [[ $# > 0 ]]; do
key="$1"
case "$key" in
--log_prefix)
log_prefix="$2"
shift
;;
--buckets)
buckets="$2"
shift
;;
--prefix)
prefix="$2"
shift
;;
--aws_profile)
aws_profile="$2"
shift
;;
--tag_for_scan)
tag_for_scan="$2"
shift
;;
--search_term)
search_term="$2"
shift
;;
--acl)
acl="$2"
shift
;;
--new_bucket)
new_bucket="$2"
shift
;;
--key_file)
key_file="$2"
shift
;;
--split_number)
split_number="$2"
shift
;;
--split_prefix)
split_prefix="$2"
shift
;;
--source_file)
source_file="$2"
shift
;;
--bucket_file)
bucket_file="$2"
shift
;;
--dif_file)
dif_file="$2"
shift
;;
*)
;;
esac
shift
done
# set variables
log="s3_$log_prefix""_.log"
log_acl="s3_$log_prefix""_acl.log"
log_error="s3_$log_prefix""_error.log"
log_app="s3_$log_prefix""_app.log"
case $command in
scan_via_tag)
rm -f $log $log_acl $log_acl $log_error $log_app
echo "***** scan_via_tag *******"
echo "***** $(date +%F:%H:%M:%S) get buckets for scan start_time=$start_time " | tee -a $log_app
bucket_for_scan_tag="$(get_s3_buckets)"
command_buckets "$bucket_for_scan_tag" '' 'check_key'
;;
scan_buckets)
rm -f $log $log_acl $log_acl $log_error $log_app
command_buckets "$buckets" '' 'check_key'
;;
scan_prefix)
rm -f $log $log_acl $log_acl $log_error $log_app
command_buckets "$buckets" "--prefix $prefix" "check_key"
;;
set_acl_buckets)
command_buckets "$buckets" "" "set_acl_buckets $acl "
;;
set_acl_prefix)
command_buckets "$buckets" "--prefix $prefix" "set_acl_buckets $acl "
;;
set_acl_from_file)
declare -i all_keys=0
echo "***** $(date +%F:%H:%M:%S) set_acl_from_file start_time=$start_time " | tee -a $log_app
while IFS= read -r LINE || [ -n "$LINE" ];
do
bucket=$(echo $LINE | cut -f1 -d'/' )
key=$(echo $LINE |cut -f1 -d' ' | sed -e 's/'$bucket'\///'g )
echo "bucket $bucket key =$key acl=$acl"
declare -i paralel_proces=0
paralel_proces+=$(ps aux | grep aws | wc -l)
while [[ $max_paralel_proces -lt $paralel_proces ]]
do
echo "***** $(date +%F:%H:%M:%S) sleep paralel_proces = $paralel_proces max_paralel_proces=$max_paralel_proces start_time=$start_time " | tee -a $log_app
sleep 1
declare -i paralel_proces=0
paralel_proces+=$(ps aux | grep aws | wc -l)
done
set_acl_buckets "$acl" "$bucket" "$key" &
send_keys+=1
all_keys+=1
if [[ ! "$send_keys" -lt "$delay_after" ]] ; then
echo "***** $(date +%F:%H:%M:%S) send $send_keys keys wait_sleep $wait_sleep bucket=$bucket all_keys=$all_keys start_time=$start_time" | tee -a $log_app
sleep $wait_sleep
declare -i send_keys=0
fi
done < $key_file
;;
get_s3_tag)
s3=$(get_s3_buckets)
echo $s3
;;
get_keys_bucket_prefix)
echo "get_keys_bucket $buckets . save to file $key_file"
get_keys_bucket $buckets "--prefix $prefix" $key_file
;;
get_keys_bucket)
echo "get_keys_bucket $buckets . save to file $key_file"
get_keys_bucket $buckets "" $key_file
;;
check_keys_file)
check_keys_file $source_file $bucket_file $dif_file
;;
copy_to_bucket)
keys_to_copy=$(wc -l $key_file)
echo "$key_file has $keys_to_copy keys to copy "
declare -i all_keys=0
echo "***** $(date +%F:%H:%M:%S) $key_file copy to new bucket start_time=$start_time " | tee -a $log_app
while IFS= read -r LINE || [ -n "$LINE" ];
do
# echo "bucket $new_bucket $LINE"
declare -i paralel_proces=0
paralel_proces+=$(ps aux | grep aws | wc -l)
while [[ $max_paralel_proces -lt $paralel_proces ]]
do
echo "***** $(date +%F:%H:%M:%S) $key_file sleep paralel_proces = $paralel_proces max_paralel_proces=$max_paralel_proces start_time=$start_time " | tee -a $log_app
sleep 1
declare -i paralel_proces=0
paralel_proces+=$(ps aux | grep aws | wc -l)
done
s3_cp "$LINE" "$new_bucket" &
send_keys+=1
all_keys+=1
if [[ ! "$send_keys" -lt "$delay_after" ]] ; then
echo "***** $(date +%F:%H:%M:%S) $key_file send $all_keys keys of $keys_to_copy wait_sleep $wait_sleep start_time=$start_time" | tee -a $log_app
sleep $wait_sleep
declare -i send_keys=0
fi
done < $key_file
echo "***** $key_file done "
echo "***** $key_file done " >/var/log/s3_log
;;
split_file)
declare -i file_line=0
declare -i file_line_count=$(wc -l $key_file | cut -d' ' -f1)
declare -i max_file_line=$((file_line_count/$split_number))
echo "file_line_count=$file_line_count"
echo "max_file_line=$max_file_line"
declare -i file_posfix=0
while IFS= read -r LINE || [ -n "$LINE" ];
do
if [[ "$file_line" -ge "$max_file_line" ]] ; then
file_posfix+=1
declare -i file_line=0
echo "file_posfix=$file_posfix"
fi
current_file_name=$split_prefix"_"$file_posfix
echo "$LINE">> $current_file_name
file_line+=1
done < $key_file
;;
*)
echo "***** none command"
;;
esac