website-backup.sh (1164B) - raw
1 #!/bin/sh 2 # 2020 Oscar Benedito <oscar@oscarbenedito.com> 3 # License: CC0 1.0 Universal License 4 5 # Script to backup content on the Internet. It gets a list of URLs and 6 # destination files and puts each document in the corresponding file, 7 # adding a date to the filename. 8 9 # This scripts assumes there is an executable called "notify" in your PATH that 10 # takes two arguments (the first one is the notification title and the second 11 # one is the message). 12 13 URLS="${XDG_CONFIG_HOME:-$HOME/.config}/osf/urls-backup.txt" 14 BACKUPS="${XDG_DATA_HOME:-$HOME/.local/share}/osf/website-backup" 15 16 [ ! -f "$URLS" ] && echo "Error: $URLS is not a file." && exit 1 17 18 backup() { 19 mkdir -p "$BACKUPS/$2" 20 output="$BACKUPS/$2/$(date +"%Y-%m-%d")-$2" 21 last="$BACKUPS/$2/$(date --date="yesterday" +"%Y-%m-%d")-$2" 22 # save new copy 23 curl -s -X GET -H "X-Auth-Token: $3" "$1" > "$output" \ 24 || notify "Website backup error" "Error backing up $2" 25 # delete last if duplicated 26 cmp -s "$output" "$last" && rm "$last" 27 } 28 29 while read -r url file token 30 do 31 backup "$url" "$file" "$token" 32 done < "$URLS" 33 34 # Can also be used by calling backup directly. Example: 35 # backup url file [token]