-
Notifications
You must be signed in to change notification settings - Fork 582
/
route53-export-zones.sh
executable file
·130 lines (108 loc) · 3.84 KB
/
route53-export-zones.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
#!/usr/bin/env bash
# This script will use cli53 to export the zone file for each Hosted Zone domain in Route 53 for git version control
# Requires Python, pip, awscli, cli53
# For more info on cli53 see https://github.com/barnybug/cli53
# Download here: https://github.com/barnybug/cli53/releases/latest
# Functions
# Check required commands
function check_command {
type -P $1 &>/dev/null || fail "Unable to find $1, please install it and run this script again."
}
# Fail
function fail(){
tput setaf 1; echo "Failure: $*" && tput sgr0
exit 1
}
# Completed
function completed(){
echo
HorizontalRule
tput setaf 2; echo "Completed!" && tput sgr0
HorizontalRule
echo
}
function HorizontalRule(){
echo "============================================================"
}
# Verify AWS CLI Credentials are setup
# http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html
if ! grep -q aws_access_key_id ~/.aws/credentials; then
if ! grep -q aws_access_key_id ~/.aws/config; then
fail "AWS config not found or CLI not installed. Please run \"aws configure\"."
fi
fi
# Check for AWS CLI profile argument passed into the script
# http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html#cli-multiple-profiles
if [ $# -eq 0 ]; then
scriptname=`basename "$0"`
echo "Usage: ./$scriptname profile"
echo "Where profile is the AWS CLI profile name"
echo "Using default profile"
echo
profile=default
else
profile=$1
fi
check_command "cli53"
# # Test if cli53 already installed, else install it
# command -v cli53 >/dev/null 2>&1 || {
# echo "Installing cli53."
# sudo pip install cli53
# echo "cli53 installed."
# }
# Test for ~/.boto file
# if ! [ -f ~/.boto ]; then
# # read -rp "Attempt to configure cli53 using AWS CLI credentials? (y/n) " CONFIGURE
# # if [[ $CONFIGURE =~ ^([yY][eE][sS]|[yY])$ ]]; then
# # Look for AWS CLI credentials
# echo "Attempting to configure cli53 using AWS CLI credentials..."
# if grep -q aws_access_key_id ~/.aws/config; then
# export AWS_ACCESS_KEY_ID=$(grep aws_access_key_id ~/.aws/config | cut -d ' ' -f3)
# export AWS_SECRET_ACCESS_KEY=$(grep aws_secret_access_key ~/.aws/config | cut -d ' ' -f3)
# elif grep -q aws_access_key_id ~/.aws/credentials; then
# export AWS_ACCESS_KEY_ID=$(grep aws_access_key_id ~/.aws/credentials | cut -d ' ' -f3)
# export AWS_SECRET_ACCESS_KEY=$(grep aws_secret_access_key ~/.aws/credentials | cut -d ' ' -f3)
# else
# echo "AWS config not found or CLI not installed. Please run \"aws configure\"."
# exit 1
# fi
# echo "Found AWS_ACCESS_KEY_ID:" $AWS_ACCESS_KEY_ID
# echo "Found AWS_SECRET_ACCESS_KEY:" $AWS_SECRET_ACCESS_KEY
# echo "Building ~/.boto config file with these credentials..."
# # Build ~/.boto config file
# echo "[Credentials]" >> ~/.boto
# echo "aws_access_key_id = "$AWS_ACCESS_KEY_ID >> ~/.boto
# echo "aws_secret_access_key = "$AWS_SECRET_ACCESS_KEY >> ~/.boto
# fi
# Get list of Hosted Zones in Route 53
DOMAINLIST=$(aws route53 list-hosted-zones --output text --profile $profile | cut -f 4 | rev | cut -c 2- | rev | grep -v '^$')
if [ -z "$DOMAINLIST" ]; then
fail "No hosted zones found in Route 53!"
fi
# Count domains found
TOTALDOMAINS=$(echo "$DOMAINLIST" | wc -l)
echo
HorizontalRule
echo "Exporting Zone Files for Route 53 Hosted Zones"
echo "Total number of Hosted Zones: "$TOTALDOMAINS
HorizontalRule
echo "$DOMAINLIST"
echo
if ! [ -d route53zones/$profile/ ]; then
mkdir -p route53zones/$profile
fi
# Export Hosted Zones
START=1
for (( COUNT=$START; COUNT<=$TOTALDOMAINS; COUNT++ ))
do
HorizontalRule
echo \#$COUNT
DOMAIN_ID=$(echo "$DOMAINLIST" | nl | grep -w $COUNT | cut -f 2)
cli53 export --full --profile $profile $DOMAIN_ID > route53zones/$profile/$DOMAIN_ID.zone
echo "Exported: "$DOMAIN_ID
done
# Remove any empty zone file created
if [ -f route53zones/$profile/.zone ]; then
rm route53zones/$profile/.zone
fi
completed