-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup_codecommit.sh
executable file
·40 lines (31 loc) · 1.57 KB
/
backup_codecommit.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
#!/bin/bash
# Copyright 2012-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Amazon Software License (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
# http://aws.amazon.com/asl/
#
# or in the "license" file accompanying this file. This file is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.
set -ex
# variable CodeCommitBackupsS3Bucket is exported into CodeBuild environment variables
backup_s3_bucket="${CodeCommitBackupsS3Bucket:-"my-s3-bucket"}"
git config --global credential.helper '!aws codecommit credential-helper $@'
git config --global credential.UseHttpPath true
declare -a repos=(`aws codecommit list-repositories | jq -r '.repositories[].repositoryName'`)
for codecommitrepo in "${repos[@]}"
do
echo "[===== Cloning repository: ${codecommitrepo} =====]"
git clone "https://git-codecommit.${AWS_DEFAULT_REGION}.amazonaws.com/v1/repos/${codecommitrepo}"
dt=$(date -u '+%Y_%m_%d_%H_%M')
zipfile="${codecommitrepo}_backup_${dt}_UTC.tar.gz"
echo "Compressing repository: ${codecommitrepo} into file: ${zipfile} and uploading to S3 bucket: ${backup_s3_bucket}/${codecommitrepo}"
tar -zcvf "${zipfile}" "${codecommitrepo}/"
aws s3 cp "${zipfile}" "s3://${backup_s3_bucket}/${codecommitrepo}/${zipfile}" --region $AWS_DEFAULT_REGION
rm $zipfile
rm -rf "$codecommitrepo"
done