From 89f29aab66a76b214946921bea569b12e799ce75 Mon Sep 17 00:00:00 2001 From: Adrian Amaglio Date: Thu, 27 Apr 2023 14:39:34 +0200 Subject: [PATCH] update --- Dockerfile | 2 +- TODO | 1 + backup.sh | 12 +++++++----- build | 5 +++++ entrypoint.sh | 2 +- initdb.sql | 6 ++++-- insert.sh | 32 ++++++++++++++++++++++++++++++++ 7 files changed, 51 insertions(+), 9 deletions(-) create mode 100644 TODO create mode 100755 build create mode 100755 insert.sh diff --git a/Dockerfile b/Dockerfile index 7516a2a..58525d0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,7 +9,7 @@ RUN adduser -D -h /usr/local/app backup WORKDIR /usr/local/app -COPY entrypoint.sh backup.sh reload.sh initdb.sql /usr/local/app/ +COPY entrypoint.sh backup.sh reload.sh insert.sh initdb.sql /usr/local/app/ ENV USER=backup diff --git a/TODO b/TODO new file mode 100644 index 0000000..d29df2c --- /dev/null +++ b/TODO @@ -0,0 +1 @@ +- Clean old files if expiration age or iteration number is given diff --git a/backup.sh b/backup.sh index 918fcb1..9fa5eb1 100755 --- a/backup.sh +++ b/backup.sh @@ -3,7 +3,7 @@ set -euo pipefail if [ "$#" -ne 1 ] ; then - usage "$0 " + echo "usage: $0 " exit 1 fi @@ -17,15 +17,17 @@ function extract() { echo "select $1 from files where id=$id" | sqlite3 db/db.sqlite } +pass_file="$(mktemp)" + # Extract variables from database username="$(extract username)" -password="$(extract password)" +extract password > "$pass_file" 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" +curl -u "$username" -X MKCOL "$server/remote.php/dav/files/$username/$export_path" < "$pass_file" name="$(basename "$backup_url")" # Insert date before file extention @@ -36,6 +38,6 @@ 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" +curl -u "$username" -X PUT --data-binary "@$tmp" "$server/remote.php/dav/files/$username/$export_path/$new_name" < "$pass_file" -rm "$tmp" +rm "$tmp" "$pass_file" diff --git a/build b/build new file mode 100755 index 0000000..22bb7fd --- /dev/null +++ b/build @@ -0,0 +1,5 @@ +#!/bin/bash + +set -euo pipefail +docker build . --network host -t jeancloud/backup-to-nextcloud +docker push jeancloud/backup-to-nextcloud diff --git a/entrypoint.sh b/entrypoint.sh index 30327ce..43a208f 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -32,7 +32,7 @@ while true ; do echo "Invalid cron times for id=$id : '$cron_raw'" continue fi - echo "$id 5 $cron backup.sh $id" >> "$cronfile" + echo "$(expr $id % 60) 5 $cron /usr/local/app/backup.sh $id" >> "$cronfile" done <"$tmp" rm "$tmp" diff --git a/initdb.sql b/initdb.sql index 753615e..1f2be90 100644 --- a/initdb.sql +++ b/initdb.sql @@ -5,5 +5,7 @@ CREATE TABLE files ( server text NOT NULL, export_path text NOT NULL, backup_url text NOT NULL, - crontab_time text NOT NULL -) + crontab_time text NOT NULL, + expiration_age int DEFAULT NULL, + expiration_number int DEFAULT NULL +); diff --git a/insert.sh b/insert.sh new file mode 100755 index 0000000..6c43a84 --- /dev/null +++ b/insert.sh @@ -0,0 +1,32 @@ +#!/bin/sh + +set -euo pipefail + +if [ "$#" -ne 5 ] ; then + echo "Usage: $0 " + exit 1 +fi + +username="$1" +password="" +server="$2" +export_path="$3" +backup_url="$4" +crontab_time="$5" + +pass_file="$(mktemp)" +while [ ! -s "$pass_file" ] ; do + echo -n 'Type password in: ' + head -qn 1 /dev/stdin | tr -d '\n' > "$pass_file" +done + +sql_req="$(mktemp)" +echo -n "insert into files (username, password, server, export_path, backup_url, crontab_time) values ('$username','" > "$sql_req" +cat "$pass_file" >> "$sql_req" +echo -n "','$server','$export_path', '$backup_url', '$crontab_time');" >> "$sql_req" +sqlite3 db/db.sqlite < "$sql_req" +echo "Enregistrement terminé" + +rm "$sql_req" "$pass_file" + +./reload.sh