backup-to-nextcloud/backup.sh

44 lines
1021 B
Bash
Raw Normal View History

2023-02-03 18:31:24 +00:00
#!/bin/sh
2023-02-02 09:45:52 +00:00
set -euo pipefail
if [ "$#" -ne 1 ] ; then
2023-04-27 12:39:34 +00:00
echo "usage: $0 <id>"
2023-02-02 09:45:52 +00:00
exit 1
fi
id="$1"
function extract() {
if [ "$#" -ne 1 ] ; then
usage "$0 <key>"
exit 1
fi
2023-02-03 18:31:24 +00:00
echo "select $1 from files where id=$id" | sqlite3 db/db.sqlite
2023-02-02 09:45:52 +00:00
}
2023-04-27 12:39:34 +00:00
pass_file="$(mktemp)"
2023-02-02 09:45:52 +00:00
# Extract variables from database
username="$(extract username)"
2023-04-27 12:39:34 +00:00
extract password > "$pass_file"
2023-02-02 09:45:52 +00:00
server="$(extract server)"
export_path="$(extract export_path)"
backup_url="$(extract backup_url)"
# Create destination directory (fails if exists)
2023-04-27 12:39:34 +00:00
curl -u "$username" -X MKCOL "$server/remote.php/dav/files/$username/$export_path" < "$pass_file"
2023-02-02 09:45:52 +00:00
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"
2023-04-27 12:39:34 +00:00
curl -u "$username" -X PUT --data-binary "@$tmp" "$server/remote.php/dav/files/$username/$export_path/$new_name" < "$pass_file"
2023-02-02 09:45:52 +00:00
2023-04-27 12:39:34 +00:00
rm "$tmp" "$pass_file"