Last active
November 15, 2022 08:52
-
-
Save ansemjo/923141bc58937befe8e143ee3d3c8277 to your computer and use it in GitHub Desktop.
Create external backup of artfiles.de databases via phpMyAdmin panel behind DCP login.
This file contains 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
#!/usr/bin/env bash | |
# Script to backup an artfiles database via phpMyAdmin behind DCP. | |
# This is mainly useful for their "Private Web medium" plan because the | |
# next larger plan includes SSH access, so you can just use mysqldump directly. | |
# The exported SQL is piped to stdout and must be saved to a file or | |
# piped further for compression, encryption, etc. | |
# $ ./dcpsqldump.sh > database_$(date +%F).sql | |
set -eu -o pipefail | |
# -------- <TODO> -------- | |
# configure these credentials for your account and database | |
# artfiles control panel authentication details | |
dcp_host="https://dcp125.c.artfiles.de" | |
dcp_user="aXXXXX" | |
dcp_pass="XXXXXXXXXXXXXXXXXX" | |
# database details | |
# hint: create a separate database user with only 'select' permission | |
db_user="dbXXXXXXXXX" | |
db_name="dbXXXXXXXXX" | |
db_pass="XXXXXXXXXXXXXXXXXX" | |
# ------- </TODO> -------- | |
# check if output is a terminal | |
if [[ -t 1 ]]; then | |
echo "ERR: won't output SQL dump to terminal" >&2 | |
exit 1 | |
fi | |
# enter a temporary directory | |
tmp="$(mktemp --tmpdir --directory dcpsqldump_XXXXXXXX)" | |
trap "rm -rf '${tmp}'" EXIT RETURN | |
# temporary files | |
cookies="${tmp}/cookies.txt" | |
token="${tmp}/token" | |
# login to dcp | |
printf 'DCP Login ... ' >&2 | |
curl "${dcp_host}/login/ajax/update.html" \ | |
--header "Accept-Language: en-US,en;q=0.5" \ | |
--header "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" \ | |
--header "Cookie: DCP_COOKIE_TEST=MQ" \ | |
--cookie-jar "${cookies}" \ | |
--silent \ | |
--data-urlencode "user=${dcp_user}" \ | |
--data-urlencode "password=${dcp_pass}" \ | |
| grep ^OK$ >/dev/null | |
echo "OK" >&2 | |
# login to phpmyadmin | |
printf 'phpMyAdmin Login ... ' >&2 | |
curl "${dcp_host}/phpmyadmin/" \ | |
--header "Accept-Language: en-US,en;q=0.5" \ | |
--header "Accept: text/html,application/xhtml+xml,application/xml" \ | |
--cookie "${cookies}" \ | |
--cookie-jar "${cookies}" \ | |
--silent --fail \ | |
--user "${db_user}:${db_pass}" \ | |
| sed -n 's/.*token:"\([^"]\+\)".*/\1/p' > "${token}" | |
echo "OK" >&2 | |
# download database export | |
printf 'Export database ... ' >&2 | |
curl "${dcp_host}/phpmyadmin/index.php?route=/export" \ | |
--header "Accept-Language: en-US,en;q=0.5" \ | |
--header "Accept: text/x-sql" \ | |
--header "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" \ | |
--cookie "${cookies}" \ | |
--silent --fail \ | |
--user "${db_user}:${db_pass}" \ | |
--data-urlencode "token=$(< "${token}")" \ | |
--data-urlencode "db=" \ | |
--data-urlencode "table=" \ | |
--data-urlencode "export_type=server" \ | |
--data-urlencode "db_select[]=${db_name}" \ | |
--data-urlencode "export_method=quick" \ | |
--data-urlencode "template_id=" \ | |
--data-urlencode "quick_or_custom=quick" \ | |
--data-urlencode "what=sql" \ | |
--data-urlencode "aliases_new=" \ | |
--data-urlencode "output_format=sendit" \ | |
--data-urlencode "filename_template=@DATABASE@" \ | |
--data-urlencode "remember_template=on" \ | |
--data-urlencode "charset=utf-8" \ | |
--data-urlencode "compression=none" \ | |
--data-urlencode "maxsize=" \ | |
--data-urlencode "sql_include_comments=yes" \ | |
--data-urlencode "sql_header_comment=" \ | |
--data-urlencode "sql_use_transaction=yes" \ | |
--data-urlencode "sql_compatibility=NONE" \ | |
--data-urlencode "sql_structure_or_data=structure_and_data" \ | |
--data-urlencode "sql_create_table=yes" \ | |
--data-urlencode "sql_auto_increment=yes" \ | |
--data-urlencode "sql_create_view=yes" \ | |
--data-urlencode "sql_procedure_function=yes" \ | |
--data-urlencode "sql_create_trigger=yes" \ | |
--data-urlencode "sql_backquotes=yes" \ | |
--data-urlencode "sql_type=INSERT" \ | |
--data-urlencode "sql_insert_syntax=both" \ | |
--data-urlencode "sql_max_query_size=50000" \ | |
--data-urlencode "sql_hex_for_binary=yes" \ | |
--data-urlencode "sql_utc_time=yes" | |
echo "OK" >&2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment