#!/bin/bash # Outputs or writes to file a JSON snippet of the last time files were backed # up (created by backup-tool.sh; .pgp) in the specified users' home directories. # Usage: last-backups [output file] USERS=(backups-vaultwarden backups-gitea backups-mystats backups-media) lastForDir() { # Alternative datetime format: '%TY-%Tm-%Td %TT', tho sort has to be used differently in that case datetime="$(find "/home/$1" -name '*.pgp' -type f -printf '%T@\n' | sort -nr | grep -m 1 '')" echo $'\t'"\"$1\": $datetime," } for user in ${USERS[@]}; do output+="$(lastForDir $user)\n" done # Remove last comma output="${output::-3}\n" # Print to stdout if no output file was provided if [ -z "$1" ]; then echo -e "{\n$output}" exit 0 fi # Write to file if not exists if [ ! -f "$1" ]; then echo -e "{\n$output}" > "$1" exit 0 fi # Calc md5 for old file and new output old=$(md5sum "$1" | cut -d' ' -f1) new=$(echo -e "{\n$output}" | md5sum | cut -d' ' -f1) # Compare md5 to only write to output if updated if [[ "$old" != "$new" ]]; then echo -e "{\n$output}" > "$1" fi