Last active
February 18, 2022 16:21
-
-
Save Laura7089/fed52468d7f85eae42404bc37a8db876 to your computer and use it in GitHub Desktop.
Shell script to download raw JSON of a set of Wekan boards
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -eo pipefail | |
# Based on https://wekan.github.io/api/v2.55/#wekan-rest-api | |
if [[ $# < 3 ]]; then | |
printf "Usage: | |
$0 <server address> <export directory> <board>...\n" | |
exit 1 | |
fi | |
ADDRESS="$1" | |
DATA_DIR="$2" | |
shift && shift | |
TARGET_BOARDS="$@" | |
if [[ ! -d ${DATA_DIR} ]]; then | |
printf "Data dir '%s' does not exist!\n" ${DATA_DIR} | |
exit 1 | |
fi | |
BACKUP_TIME=$(date --iso-8601=minutes) | |
LOGIN_RESP_TEMP=$(mktemp /tmp/wekan_login.XXXXXXX.txt) | |
# From https://stackoverflow.com/questions/64786/error-handling-in-bash | |
cleanup() { | |
rm -f ${LOGIN_RESP_TEMP} | |
} | |
trap cleanup 0 | |
# Get creds | |
# `read` from https://ryanstutorials.net/bash-scripting-tutorial/bash-input.php | |
if [[ -z ${WEKAN_USERNAME} ]]; then | |
read -p "Username not in env, please enter: " WEKAN_USERNAME | |
fi | |
if [[ -z ${WEKAN_PASSWORD} ]]; then | |
read -sp "Password not in env, please enter: " WEKAN_PASSWORD | |
printf "\n" | |
fi | |
# Login | |
printf "\nLogging in..." | |
curl -fs -X POST "${ADDRESS}/users/login" \ | |
-H "Content-Type: application/x-www-form-urlencoded" \ | |
-H "Accept: application/json" \ | |
-d "username=${WEKAN_USERNAME}&password=${WEKAN_PASSWORD}" \ | |
-o ${LOGIN_RESP_TEMP} | |
printf "success\n" | |
printf "\nExtracting login token..." | |
token=$(jq .token ${LOGIN_RESP_TEMP} | sed 's/"//g') | |
printf "success\n\n" | |
cleanup | |
# Download boards | |
for board in ${TARGET_BOARDS}; do | |
printf "Getting board %s..." ${board} | |
curl -sf -X GET "${ADDRESS}/api/boards/${board}/export" \ | |
-H "Accept: application/json" \ | |
-H "Authorization: Bearer ${token}" \ | |
-o "${DATA_DIR}/${board}_${BACKUP_TIME}.json" | |
printf "success\n" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment