Skip to content

Instantly share code, notes, and snippets.

@Bharat-B
Last active March 21, 2017 10:15
Show Gist options
  • Save Bharat-B/4300b9315da61dbec8a7189218e4548b to your computer and use it in GitHub Desktop.
Save Bharat-B/4300b9315da61dbec8a7189218e4548b to your computer and use it in GitHub Desktop.
Bash script to backup any organization's github repositories to AWS S3 and post notifications to slack channel
#!/bin/bash
#Define variables
now=`date "+%Y-%m-%d-%H-%M-%S"`
#Github Organization
GB_ORG=""
#Github Username
GB_USR=""
#Github Password
GB_PWD=""
#Github API URI ( Usually doesnt change )
GB_API="https://api.github.com"
#Git clone command
GB_CLONE_CMD="git clone --quiet --mirror git@github.com:"
#Retrieves repository names
GB_REPOS=`curl --silent -u $GB_USR:$GB_PWD $GB_API/orgs/$GB_ORG/repos\?per_page=100 -q | grep "\"name\"" | awk -F': "' '{print $2}' | sed -e 's/",//g'`
#The location where backup tars will temporarily be stored
GB_BACKUP_DIR="/tmp/backups"
#S3 command to move the backups to S3
AWS_S3_PUT_CMD="/usr/bin/aws s3 mv"
#Replace "BUCKET_NAME" with the destination S3 bucket
AWS_S3_BUCKET="s3://BUCKET_NAME"
#Incoming webhook for slack channel
SLACK_URL=https://hooks.slack.com/services/YOUR_SERVICE_TOKEN_HERE
#Create the backup directory if it doesn't exist
mkdir -p $GB_BACKUP_DIR
#Function to notify slack
function notify_slack () {
curl -X POST --data "payload={\"text\": \":slack: $1\"}" ${SLACK_URL}
}
#Function to compress directory
function compress {
tar zcf $1.tar.gz $1 && rm -rf $1
}
notify_slack "Backup process initiated on ${now}"
for repo in $GB_REPOS
do
notify_slack "Cloning Repository $repo"
git clone git@github.com:${GB_ORG}/${repo}.git ${GB_BACKUP_DIR}/${repo}.${now}.git && compress ${GB_BACKUP_DIR}/${repo}.${now}.git
if [ $? -ne 0 ]; then
post_to_slack "Error Cloning Repository $repo"
exit 1
fi
notify_slack "Successfully Cloned & Compressed Repository $repo"
notify_slack "Moving ${repo}.${now}.git.tar.gz to {$AWS_S3_BUCKET}"
${AWS_S3_PUT_CMD} ${GB_BACKUP_DIR}/${repo}.${now}.git.tar.gz ${AWS_S3_BUCKET}/${repo}/${repo}.${now}.git.tar.gz
if [ $? -ne 0 ]; then
post_to_slack "Error Moving Repository ${repo}.${now}"
exit 1
fi
notify_slack "Moved ${repo}.${now}.git.tar.gz To {$AWS_S3_BUCKET} Successfully!"
done
notify_slack "Successfully ended backup process"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment