Skip to content

Instantly share code, notes, and snippets.

@zsim0n
Last active August 29, 2015 14:04
Show Gist options
  • Save zsim0n/8da158b065f29f27890e to your computer and use it in GitHub Desktop.
Save zsim0n/8da158b065f29f27890e to your computer and use it in GitHub Desktop.
Atlassian OnDemand Backup script
*.cookie
*.zip
#!/bin/bash
ATLASSIAN_USER=youruser
ATLASSIAN_PASS=yourpassword
ATLASSIAN_HOST=yourhost
DOWNLOAD_LOCATION="."
WAIT_TIMEOUT=20 # 20 times 10s
# set an initial value for the flag
ARG_BACKUP=0
ARG_DOWNLOAD=0
HELP_USAGE="Usage: ./atlassian-backup.sh [-b|--backup] [-d|--download] [-T|--target <Jira|Confluence>] [-h|--help]"
# extract options and their arguments into variables.
[ $# -eq 0 ] && { echo "$HELP_USAGE" ; exit 1; }
while :; do
case "$1" in
-h|--help) echo "$HELP_USAGE" ; exit 1 ;;
-b|--backup) ARG_BACKUP=1 ; ;;
-d|--download) ARG_DOWNLOAD=1 ; ;;
-T|--target) ATLASSIAN_TARGET=$2 ; shift ;;
--) shift ; break ;;
-?*)
printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
;;
*)
break;
esac
shift
done
# Login and get cookie via UI
ATLASSIAN_COOKIE="atlassian.cookie"
case $ATLASSIAN_TARGET in
Jira )
BACKUP_PREFIX="rest"
DOWNLOAD_PREFIX="JIRA-backup"
;;
Confluence )
BACKUP_PREFIX="wiki/rest"
DOWNLOAD_PREFIX="Confluence-backup"
;;
*)
echo "Unknown target :${ATLASSIAN_TARGET}"
exit 1
;;
esac
TODAY=`date +%Y%m%d`
BACKUP_URL="https://${ATLASSIAN_HOST}/${BACKUP_PREFIX}/obm/1.0/runbackup"
DOWNLOAD_URL="https://${ATLASSIAN_HOST}/webdav/backupmanager/${DOWNLOAD_PREFIX}-${TODAY}.zip"
if [ $ARG_BACKUP -ne 0 ] || [ $ARG_DOWNLOAD -ne 0 ]; then
echo "Login..."
LOGIN_MESSAGE=`curl --silent -u $ATLASSIAN_USER:$ATLASSIAN_PASS --cookie-jar ${ATLASSIAN_COOKIE} https://${ATLASSIAN_HOST}/Dashboard.jspa --output /dev/null `
if [ $? -ne 0 ]; then
echo $LOGIN_MESSAGE
exit 1
fi
fi
if [ $ARG_BACKUP -ne 0 ]; then
echo "Backup..."
BACKUP_MESSAGE=`curl -s --cookie $ATLASSIAN_COOKIE --header "X-Atlassian-Token: no-check" -H "X-Requested-With: XMLHttpRequest" -H "Content-Type: application/json" -X POST ${BACKUP_URL} -d '{"cbAttachments":"true" }' `
if [ `echo $BACKUP_MESSAGE | grep -i backup | wc -l` -ne 0 ]; then
echo $BACKUP_MESSAGE
exit 1
fi
rm $ATLASSIAN_COOKIE
fi
WAIT_OK=0
if [ $ARG_BACKUP -ne 0 ] && [ $ARG_DOWNLOAD -ne 0 ]; then
echo "Wait..."
#Checks if the backup exists in WebDAV every 10 seconds, 20 times. If you have a bigger instance with a larger backup file you'll probably want to increase that.
for (( c=1; c<=20; c++ )) do
wget --user=$ATLASSIAN_USER --password=$ATLASSIAN_PASS --spider ${DOWNLOAD_URL} >/dev/null 2>/dev/null
WAIT_OK=$?
if [ ${WAIT_OK} -eq 0 ]; then
break
fi
sleep 10
done
#If after 20 attempts it still fails it ends the script.
if [ $WAIT_OK -ne 0 ]; then
echo "Timeout error!"
exit 1
fi
fi
if [ $WAIT_OK -eq 0 ] && [ $ARG_DOWNLOAD -ne 0 ]; then
echo "Download..."
DOWNLOAD_MESSAGE=`wget --user=$ATLASSIAN_USER --password=$ATLASSIAN_PASS -t 0 --retry-connrefused ${DOWNLOAD_URL} -P ${DOWNLOAD_LOCATION} 2>&1 `
DOWNLOAD_OK=$?
if [ `echo $DOWNLOAD_MESSAGE | grep -i "404\|401" | wc -l` -ne 0 ]; then
echo $DOWNLOAD_MESSAGE
exit 1
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment