This commit is contained in:
Adrian Amaglio 2023-04-27 14:39:34 +02:00
parent f65caf2b6b
commit 89f29aab66
7 changed files with 51 additions and 9 deletions

View File

@ -9,7 +9,7 @@ RUN adduser -D -h /usr/local/app backup
WORKDIR /usr/local/app 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 ENV USER=backup

1
TODO Normal file
View File

@ -0,0 +1 @@
- Clean old files if expiration age or iteration number is given

View File

@ -3,7 +3,7 @@
set -euo pipefail set -euo pipefail
if [ "$#" -ne 1 ] ; then if [ "$#" -ne 1 ] ; then
usage "$0 <id>" echo "usage: $0 <id>"
exit 1 exit 1
fi fi
@ -17,15 +17,17 @@ function extract() {
echo "select $1 from files where id=$id" | sqlite3 db/db.sqlite echo "select $1 from files where id=$id" | sqlite3 db/db.sqlite
} }
pass_file="$(mktemp)"
# Extract variables from database # Extract variables from database
username="$(extract username)" username="$(extract username)"
password="$(extract password)" extract password > "$pass_file"
server="$(extract server)" server="$(extract server)"
export_path="$(extract export_path)" export_path="$(extract export_path)"
backup_url="$(extract backup_url)" backup_url="$(extract backup_url)"
# Create destination directory (fails if exists) # 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")" name="$(basename "$backup_url")"
# Insert date before file extention # Insert date before file extention
@ -36,6 +38,6 @@ tmp="$(mktemp)"
curl "$backup_url" > "$tmp" curl "$backup_url" > "$tmp"
echo "Uploading as $export_path/$new_name" 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"

5
build Executable file
View File

@ -0,0 +1,5 @@
#!/bin/bash
set -euo pipefail
docker build . --network host -t jeancloud/backup-to-nextcloud
docker push jeancloud/backup-to-nextcloud

View File

@ -32,7 +32,7 @@ while true ; do
echo "Invalid cron times for id=$id : '$cron_raw'" echo "Invalid cron times for id=$id : '$cron_raw'"
continue continue
fi fi
echo "$id 5 $cron backup.sh $id" >> "$cronfile" echo "$(expr $id % 60) 5 $cron /usr/local/app/backup.sh $id" >> "$cronfile"
done <"$tmp" done <"$tmp"
rm "$tmp" rm "$tmp"

View File

@ -5,5 +5,7 @@ CREATE TABLE files (
server text NOT NULL, server text NOT NULL,
export_path text NOT NULL, export_path text NOT NULL,
backup_url 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
);

32
insert.sh Executable file
View File

@ -0,0 +1,32 @@
#!/bin/sh
set -euo pipefail
if [ "$#" -ne 5 ] ; then
echo "Usage: $0 <username> <server> <export_path> <backup_url> <crontab_time>"
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