From 7ed49496301a5278e5e05f69f5b46afcd603fc92 Mon Sep 17 00:00:00 2001 From: Adrian Amaglio Date: Thu, 2 Feb 2023 10:45:52 +0100 Subject: [PATCH] init --- .gitignore | 2 ++ backup.sh | 41 +++++++++++++++++++++++++++++++++++++++++ initdb.sql | 9 +++++++++ 3 files changed, 52 insertions(+) create mode 100644 .gitignore create mode 100755 backup.sh create mode 100644 initdb.sql diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..43c0f63 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.env +db.sqlite diff --git a/backup.sh b/backup.sh new file mode 100755 index 0000000..2e51687 --- /dev/null +++ b/backup.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +set -euo pipefail + +if [ "$#" -ne 1 ] ; then + usage "$0 " + exit 1 +fi + +id="$1" + +function extract() { + if [ "$#" -ne 1 ] ; then + usage "$0 " + exit 1 + fi + echo "select $1 from files where id=$id" | sqlite3 db.sqlite +} + +# Extract variables from database +username="$(extract username)" +password="$(extract password)" +server="$(extract server)" +export_path="$(extract export_path)" +backup_url="$(extract backup_url)" + +# Create destination directory (fails if exists) +curl -u "$username:$password" -X MKCOL "$server/remote.php/dav/files/$username/$export_path" + +name="$(basename "$backup_url")" +# Insert date before file extention +new_name="$(echo $name | sed "s/\(\.[^\.]\+\)$/_$(date +%F)\1/")" + +echo "Downloading $backup_url" +tmp="$(mktemp)" +curl "$backup_url" > "$tmp" + +echo "Uploading as $export_path/$new_name" +curl -u "$username:$password" -X PUT --data-binary "@$tmp" "$server/remote.php/dav/files/$username/$export_path/$new_name" + +rm "$tmp" diff --git a/initdb.sql b/initdb.sql new file mode 100644 index 0000000..753615e --- /dev/null +++ b/initdb.sql @@ -0,0 +1,9 @@ +CREATE TABLE files ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + username text NOT NULL, + password text NOT NULL, + server text NOT NULL, + export_path text NOT NULL, + backup_url text NOT NULL, + crontab_time text NOT NULL +)