Skip to content

Instantly share code, notes, and snippets.

@erincerys
Created September 26, 2013 16:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erincerys/6716865 to your computer and use it in GitHub Desktop.
Save erincerys/6716865 to your computer and use it in GitHub Desktop.
Schedules a backup of an Atlassian OnDemand instance and uploads the resulting file to S3
#!/bin/bash
#
# Obtained from:
# https://confluence.atlassian.com/display/ONDEMANDKB/Automatic+backups+for+JIRA+OnDemand
# and modified to include:
# - longer waiting period for backup to be created
# - S3 upload
#
MAIL_BODY_PATH="/tmp/message.body"
MAIL_RECIPIENT="dev@foobar.com"
MAIL_SUBJECT="Atlassian OnDemand backup failed"
S3BUCKET=backups
ATLASSIAN_UID=youruser
ATLASSIAN_PWD=yourpassword
ATLASSIAN_INST=example.atlassian.net
BACKUP_PATH=/storage/atlassian-backup/
# Grabs cookies and generates the backup by calling REST API with your credentials
TODAY=`date +%Y%m%d`
COOKIE_PATH='/tmp/jiracookie'
curl --silent -u $ATLASSIAN_UID:$ATLASSIAN_PWD --cookie-jar $COOKIE_PATH https://${ATLASSIAN_INST}/Dashboard.jspa --output /dev/null
BKPMSG=`curl -s --cookie $COOKIE_PATH --header "X-Atlassian-Token: no-check" -H "X-Requested-With: XMLHttpRequest" -H "Content-Type: application/json" -X POST https://${ATLASSIAN_INST}/rest/obm/1.0/runbackup -d '{"cbAttachments":"true" }' `
rm $COOKIE_PATH
#Checks if the backup procedure has failed
if [ `echo $BKPMSG | grep -i backup | wc -l` -ne 0 ]; then
echo "I failed to issue the backup initialization command to Atlassian's API server. This could occur due to incorrect authentication credentials." > ${MAIL_BODY_PATH}
exit 1
fi
START=$(date)
WAITUNTIL=$((`date '+%s'` + (60 * 90)))
# checks that backup exists every 30 seconds for 1.5 hours
while [ `date '+%s'` -lt $WAITUNTIL ] ; do
wget --user=$ATLASSIAN_UID --password=$ATLASSIAN_PWD --spider https://${ATLASSIAN_INST}/webdav/backupmanager/JIRA-backup-${TODAY}.zip >/dev/null 2>/dev/null
OK=$?
if [ $OK -eq 0 ]; then
break
fi
sleep 30
done
# script fails if backup is not there eventually
if [ $OK -ne 0 ] ; then
echo "I failed to retrieve the backup file from your Atlassian OnDemand instance. I have been trying since ${START}, and am now giving up. Check https://${ATLASSIAN_INST}/webdav/backupmanager/JIRA-backup-${TODAY}.zip later today, and adjust the \$WAITUNTIL variable as appropriate, when the backup is present!" > ${MAIL_BODY_PATH}
mutt -s "${MAIL_SUBJECT}" -- "${MAIL_RECIPIENT}" < "${MAIL_BODY_PATH}"
exit 1
else
# If it's confirmed that the backup exists on WebDAV the file gets copied to the $BACKUP_PATH directory.
wget --user=$ATLASSIAN_UID --password=$ATLASSIAN_PWD -t 0 --retry-connrefused https://${ATLASSIAN_INST}/webdav/backupmanager/JIRA-backup-${TODAY}.zip -P $BACKUP_PATH >/dev/null 2>/dev/null
if [[ $(ls -1 ${BACKUP_PATH} -gt ${BACKUP_FILES} ] ; then
find ${BACKUP_PATH} -name '*.zip' -type f -daystart -ctime +7 -exec rm -f {} \;
fi
s3cmd sync --delete-removed "${BACKUP_PATH}" s3://${S3BUCKET}/
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment