#!/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" # Add your own paths, users and password-files separated with a space backup_paths=("https://nx.phga.de/remote.php/dav/files/toerd/external/no_sync/backups/study_backups/BA/" "https://nc.frfe.de/remote.php/dav/files/toerd/backups/BA/") nxt_users=(toerd toerd) nxt_passwds=(backup_ba_pw_phga.de.gpg backup_ba_pw_frfe.de.gpg) # 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) i=0 for bp in ${backup_paths[@]}; do last_full_backup=$(curl -s -u ${nxt_users[$i]}:$(gpg -d $me/${nxt_passwds[$i]}) -X PROPFIND "$bp" | rg -o -e "(\d{4}-\d{2}-\d{2})_BA_full" -r '$1' | tail -1) last_part_backup=$(curl -s -u ${nxt_users[$i]}:$(gpg -d $me/${nxt_passwds[$i]}) -X PROPFIND "$bp" | rg -o -e "(\d{4}-\d{2}-\d{2})_BA_part" -r '$1' | tail -1) if [ ! -z $last_full_backup ]; then datediff_full_backup=$((($(date +%s) - $(date -d $last_full_backup +%s)) / (3600 * 24))) else datediff_full_backup=1337 fi # Info output echo -------------------------------------- echo $bp | rg -o -e ".*\.de" 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 # if no tar for this backup exists create one if [ ! -f "$tmp_path$archive_name_full" ]; then $tar_cmd "$tmp_path$archive_name_full" $full_backup_files fi curl -s -u ${nxt_users[$i]}:$(gpg -d $me/${nxt_passwds[$i]}) -T "$tmp_path$archive_name_full" "$bp$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 if [ ! -f "$tmp_path$archive_name_part" ]; then $tar_cmd "$tmp_path$archive_name_part" $part_backup_files fi curl -s -u ${nxt_users[$i]}:$(gpg -d $me/${nxt_passwds[$i]}) -T "$tmp_path$archive_name_part" "$bp$archive_name_part" else echo "---- No part backup required yet! ----" fi ((i++)) done # remove temp path rm -r $tmp_path