commit 774a6dfeeaa6aca619a52a065cdc75eb534f3581
parent 59447488ccc547bee14a3e5fc431b03fc6d8b9aa
Author: Oscar Benedito <oscar@oscarbenedito.com>
Date:   Fri,  4 Sep 2020 19:47:33 +0200

Website backups reorganized

Diffstat:
Mwebsite-backup.sh | 32+++++++++++++++++---------------
1 file changed, 17 insertions(+), 15 deletions(-)

diff --git a/website-backup.sh b/website-backup.sh @@ -20,24 +20,26 @@ FILE_DIR="$(dirname "$(realpath "$0")")" . "$FILE_DIR/notify.sh" -URL_FILE="$FILE_DIR/urls.txt" -BACKUP_PATH="$HOME/backups" +URLS="$FILE_DIR/urls.txt" +BACKUPS="$HOME/backups" -save() { curl -s -X GET -H "X-Auth-Token: $token" "$url" > "$output" ; } +[ ! -f "$URLS" ] && echo "Error: $URLS is not a file." && exit 1 -delete_duplicates() { cmp -s "$output" "$last" && rm "$last" ; } - -error_message () { - TITLE="Website backup error" - MESSAGE="Error backing up $file" - notify "$TITLE" "$MESSAGE" +backup() { + mkdir -p "$BACKUPS/$2" + output="$BACKUPS/$2/$(date +"%Y-%m-%d")-$2" + last="$BACKUPS/$2/$(date --date="yesterday" +"%Y-%m-%d")-$2" + # save new copy + curl -s -X GET -H "X-Auth-Token: $3" "$1" > "$output" \ + || notify "Website backup error" "Error backing up $2" + # delete last if duplicated + cmp -s "$output" "$last" && rm "$last" } while read -r url file token do - mkdir -p "$BACKUP_PATH/$file" - output="$BACKUP_PATH/$file/$(date +"%Y-%m-%d")-$file" - last="$BACKUP_PATH/$file/$(date --date="yesterday" +"%Y-%m-%d")-$file" - save || error_message - delete_duplicates -done < "$URL_FILE" + backup "$url" "$file" "$token" +done < "$URLS" + +# Can also be used by calling backup directly. Example: +# backup url file [token]