From defeac80ca97fedcb19bdcddc516fd8f6e55fe8c Mon Sep 17 00:00:00 2001
From: Mathias Petermann <mathias.petermann@gmail.com>
Date: Wed, 1 Jun 2022 10:27:55 +0200
Subject: [PATCH] Add backup cli command, exporting all notes for current user

Signed-off-by: Mathias Petermann <mathias.petermann@gmail.com>
---
 bin/hedgedoc | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)

diff --git a/bin/hedgedoc b/bin/hedgedoc
index f6fa78f..b6b8173 100755
--- a/bin/hedgedoc
+++ b/bin/hedgedoc
@@ -77,7 +77,7 @@ Config:
     \$HEDGEDOC_CONFIG_DIR   (defaults to $XDG_CONFIG_HOME/$SCRIPTNAME)
     \$HEDGEDOC_COOKIES_FILE (defaults to \$HEDGEDOC_CONFIG_DIR/key.conf)
 
-Usage examples: 
+Usage examples:
 
     \$ export HEDGEDOC_SERVER='$HEDGEDOC_SERVER'
     \$ export HEDGEDOC_COOKIES_FILE=$HEDGEDOC_COOKIES_FILE
@@ -174,7 +174,7 @@ function import_note() {
         echo "Hint: You may need to run '$SCRIPTNAME login' to authenticate first." >&2
         return 2
     fi
-    echo "$note_id" 
+    echo "$note_id"
 }
 
 function export_note() {
@@ -230,7 +230,7 @@ function export_note() {
             output_path="${output_path:-$note_id.zip}"
             # hack: there is no way to get slide output as zipfile directly.
             #       we dump it to folder of html files first then zip it.
-            
+
             # check for any existing conflicting file/dir that wget downloads
             local temp_folder_name="$(
                 echo "$HEDGEDOC_SERVER" \
@@ -259,6 +259,17 @@ function export_note() {
     esac
 }
 
+function backup_all_notes() {
+    output_path="${1:-archive.zip}"
+
+    curl \
+        --silent \
+        --cookie "$HEDGEDOC_COOKIES_FILE" \
+        --output "$output_path" \
+        "${HEDGEDOC_SERVER}/me/export" \
+    && echo "$output_path"
+}
+
 function user_login() {
     local method="${1#--}" username="${2:-}" password="${3:-}" username_arg=""
     case "$method" in
@@ -349,7 +360,7 @@ function user_profile() {
 
 function user_history() {
     assert_is_authenticated
-    
+
     curl \
         --silent \
         --cookie "$HEDGEDOC_COOKIES_FILE" \
@@ -390,6 +401,9 @@ function main() {
         export)
             export_note "${1:-}" "${2:-}" "${3:-}"
             ;;
+        backup)
+            backup_all_notes "${1:-}"
+            ;;
         delete)
             delete_note "${1:-}"
             ;;
@@ -416,7 +430,7 @@ function main() {
 # Do the usage function of no Command line arguments are given.
 if [[ $# -eq 0 ]]; then
   set -- "help"
-fi  
+fi
 
 # Allow importing funcs without running main by using `source ./bin/hedgedoc --import`
 if [[ "${1:-}" != "--import" ]]; then
-- 
GitLab