-
Notifications
You must be signed in to change notification settings - Fork 574
/
s3-buckets-file-size-s3cmd.sh
executable file
·98 lines (78 loc) · 2.52 KB
/
s3-buckets-file-size-s3cmd.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
# #!/usr/bin/env bash
##################################################################
# Depreciating this script since using the s3api is must faster! #
##################################################################
# # Script to count total size of all data stored in all s3 buckets (IAM account must have permission to access all buckets)
# # Requires s3cmd
# # Functions
# # Fail
# function fail(){
# tput setaf 1; echo "Failure: $*" && tput sgr0
# exit 1
# }
# # Check for command
# function check_command {
# type -P $1 &>/dev/null || fail "Unable to find $1, please install it and run this script again."
# }
# # Completed
# function completed(){
# echo
# HorizontalRule
# tput setaf 2; echo "Completed!" && tput sgr0
# HorizontalRule
# echo
# }
# # Horizontal Rule
# function HorizontalRule(){
# echo "============================================================"
# }
# # Convert bytes to human readable
# function bytestohr(){
# SLIST="bytes,KB,MB,GB,TB,PB,EB,ZB,YB"
# POWER=1
# VAL=$( echo "scale=2; $1 / 1" | bc)
# VINT=$( echo $VAL / 1024 | bc )
# while [ $VINT -gt 0 ]
# do
# let POWER=POWER+1
# VAL=$( echo "scale=2; $VAL / 1024" | bc)
# VINT=$( echo $VAL / 1024 | bc )
# done
# echo $VAL $( echo $SLIST | cut -f$POWER -d, )
# }
# # Check required commands
# check_command "s3cmd"
# # Verify s3cmd Credentials are setup
# # http://s3tools.org/s3cmd-howto
# if ! [ -f ~/.s3cfg ]; then
# fail "Error: s3cmd config not found or not installed."
# fi
# # List buckets
# S3CMDLS=$(s3cmd ls 2>&1)
# # Count number of buckets
# TOTALNUMBERS3BUCKETS=$(echo "$S3CMDLS" | wc -l | rev | cut -d " " -f1 | rev)
# # Get list of all bucket names
# BUCKETNAMES=$(echo "$S3CMDLS" | cut -d ' ' -f 4 | nl)
# echo
# HorizontalRule
# echo "Counting Total Size of Data in $TOTALNUMBERS3BUCKETS S3 Buckets"
# echo "(This may take a very long time depending on number of files)"
# HorizontalRule
# echo
# START=1
# TOTALBUCKETSIZE=0
# for (( COUNT=$START; COUNT<=$TOTALNUMBERS3BUCKETS; COUNT++ ))
# do
# CURRENTBUCKET=$(echo "$BUCKETNAMES" | grep -w [^0-9][[:space:]]$COUNT | cut -f 2)
# HorizontalRule
# echo \#$COUNT $CURRENTBUCKET
# CURRENTBUCKETSIZE=$(s3cmd du $CURRENTBUCKET | cut -d ' ' -f 1)
# TOTALBUCKETSIZE=$(($TOTALBUCKETSIZE + $CURRENTBUCKETSIZE))
# echo "Size: "
# bytestohr $CURRENTBUCKETSIZE
# echo "Subtotal: "
# bytestohr $TOTALBUCKETSIZE
# done
# completed
# echo "Total Size of Data in All $TOTALNUMBERS3BUCKETS S3 Buckets:"
# bytestohr $TOTALBUCKETSIZE