You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
2.4 KiB

#!/bin/bash
# author: phga
# date: 2020-10-07
# desc: short rudimentary script to create backups for my bachelor thesis
# and push them onto a webdav share
# required: ripgrep (rg), otherwise change lines 22, 23 to your grep alternative
# that supports capture groups
# gpg encrypted password for nextcloud in same dir as script (./backup_ba_pw.gpg)
me=$(dirname "$0")
tmp_path=$(mktemp -d)
today=$(date +%Y-%m-%d)
archive_name_full="${today}_BA_full.tgz"
archive_name_part="${today}_BA_part.tgz"
# exclude .git, etc.
tar_cmd="tar --exclude-vcs -czf"
# CHANGE THESE TO YOUR REQUIRED DIRS/FILES/PATHS/URLS/USERS
part_backup_files="/home/phga/sync/docs/study/BA/proposal /home/phga/sync/docs/study/BA/BA_WIP.org"
full_backup_files="/home/phga/sync/docs/study/BA"
backup_path="https://nx.phga.de/remote.php/dav/files/toerd/external/no_sync/backups/study_backups/BA/"
nxt_user=toerd
# PASSWORD: see desc in header (./backup_ba_pw.gpg)
# get latest backup date (from filename, order in webdav seems to be asc - lucky for us)
last_full_backup=$(curl -s -u toerd:$(gpg -d $me/backup_ba_pw.gpg) -X PROPFIND "$backup_path" | rg -o -e "(\d{4}-\d{2}-\d{2})_BA_full" -r '$1' | tail -1)
last_part_backup=$(curl -s -u toerd:$(gpg -d $me/backup_ba_pw.gpg) -X PROPFIND "$backup_path" | rg -o -e "(\d{4}-\d{2}-\d{2})_BA_part" -r '$1' | tail -1)
datediff_full_backup=$((($(date +%s) - $(date -d $last_full_backup +%s)) / (3600 * 24)))
# Info output
echo --------------------------------------
echo Last full backup was $datediff_full_backup days ago
echo Date last full backup: $last_full_backup
echo Date last part backup: $last_part_backup
echo --------------------------------------
# If 14 days have passed, create a new full backup
if [ -z $last_full_backup ] || [ $datediff_full_backup -gt 14 ]; then
$tar_cmd "$tmp_path$archive_name_full" $full_backup_files
curl -s -u $nxt_user:$(gpg -d $me/backup_ba_pw.gpg) -T "$tmp_path$archive_name_full" "$backup_path$archive_name_full"
else
echo "---- No full backup required yet! ----"
fi
# if there is no important backup from this day, create one
if [ -z $last_part_backup ] || [ $today != $last_part_backup ]; then
$tar_cmd "$tmp_path$archive_name_part" $part_backup_files
curl -s -u $nxt_user:$(gpg -d $me/backup_ba_pw.gpg) -T "$tmp_path$archive_name_part" "$backup_path$archive_name_part"
else
echo "---- No part backup required yet! ----"
fi
# remove temp path
rm -r $tmp_path