website-backup.sh (1323B) - 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_FILE="${URLS_FILE:-${XDG_CONFIG_HOME:-$HOME/.config}/osf/urls-backup.txt}"
     14 TARGET_DIR="${TARGET_DIR:-${XDG_DATA_HOME:-$HOME/.local/share}/osf/website-backup}"
     15 
     16 [ ! -f "$URLS_FILE" ] && echo "Error: $URLS_FILE is not a file." && exit 1
     17 
     18 backup() {
     19   mkdir -p "$TARGET_DIR/$2"
     20   output="$TARGET_DIR/$2/$(date +"%Y-%m-%d")-$2"
     21   last="$TARGET_DIR/$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   # this is needed so that if the last cmp returns false, the function still succeeds
     29   return 0
     30 }
     31 
     32 while read -r url file token
     33 do
     34   backup "$url" "$file" "$token"
     35 done < "$URLS_FILE"
     36 
     37 # Can also be used by calling backup directly. Example:
     38 # backup url file [token]