Skip to content

Instantly share code, notes, and snippets.

@jmakeig
Created March 24, 2017 18:17
Show Gist options
  • Save jmakeig/ae8bf757244975d75f40cc77e3707d81 to your computer and use it in GitHub Desktop.
Save jmakeig/ae8bf757244975d75f40cc77e3707d81 to your computer and use it in GitHub Desktop.
cURL script to export MarkLogic Query Console workspaces
#!/usr/bin/env bash
# Downloads Query Console workspaces to the current working directory in the
# format `{host}_{workspace id}_{timestamp}.workspace`.
#
# Usage: ./query-console.sh localhost admin '******'
#
if command -v jq >/dev/null; then
:
else
echo 'You need jq in order to parse JSON. <http://stedolan.github.io/jq/>'
exit 1
fi
#HOST=jmakeig-centos6-virtualbox.localdomain
#USER=admin
#PASSWORD='********'
# TODO: Make these proper params with sensible defaults
HOST="$1"
USER="$2"
PASSWORD="$3"
# TODO: Make the output path user definable. Defaults to ./ today.
TOKEN_NAME="csrf-token-8000-$USER"
RAW_TOKEN=$(curl -IsS "http://$HOST:8000/qconsole/" --digest --user "$USER":"$PASSWORD" | grep -Fi "csrf-token-" | sed -En 's/^Set-Cookie: csrf-token-.+=([a-zA-Z0-9]+)/\1/p')
#TOKEN="2141bcb52e864afcf6716884e200228bc189a97d2a2c9b0be8512ad156494f55"
TOKEN=${RAW_TOKEN:0:64} # Argh!
#echo ${#TOKEN}
#curl --verbose -fsS -H "X-CSRF-Token: $TOKEN" -H "Origin: http://localhost:8000/" "http://$HOST:8000/qconsole/endpoints/workspaces.xqy" --digest --user "$USER":"$PASSWORD"
#exit 0;
curl -fsS -H "X-CSRF-Token: $TOKEN" "http://$HOST:8000/qconsole/endpoints/workspaces.xqy" --digest --user "$USER":"$PASSWORD" | jq --raw-output '.[].workspace[].id' | while read -r line; do
# Export
printf "Exporting workspace $line from $HOST\n"
curl -fsS -H "X-CSRF-Token: $TOKEN" "http://$HOST:8000/qconsole/endpoints/workspaces.xqy?wsid=$line&format=export" --digest --user "$USER":"$PASSWORD" > "$HOST"_"$line"_$(date +"%Y-%m-%dT%H:%M:%SZ").workspace
# Delete
#printf "Deleting workspace $line from $HOST\n"
#curl -fsS "http://$HOST:8000/qconsole/endpoints/workspaces.xqy?wsid=$line" -X DELETE --digest --user "$USER":"$PASSWORD" 1>/dev/null
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment