website-backup.sh (1799B) - raw


      1 #!/bin/sh
      2 # Copyright (C) 2020 Oscar Benedito <oscar@oscarbenedito.com>
      3 #
      4 # This program is free software: you can redistribute it and/or modify
      5 # it under the terms of the GNU Affero General Public License as
      6 # published by the Free Software Foundation, either version 3 of the
      7 # License, or (at your option) any later version.
      8 #
      9 # This program is distributed in the hope that it will be useful,
     10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12 # GNU Affero General Public License for more details.
     13 #
     14 # You should have received a copy of the GNU Affero General Public License
     15 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
     16 
     17 # Script to backup content on the Internet. It gets a list of URLs and
     18 # destination files and puts each document in the corresponding file,
     19 # adding a date to the filename.
     20 
     21 # This scripts assumes there is an executable called "notify" in your PATH that
     22 # takes two arguments (the first one is the notification title and the second
     23 # one is the message).
     24 
     25 URLS="${XDG_CONFIG_HOME:-$HOME/.config}/osf/urls-backup.txt"
     26 BACKUPS="${XDG_DATA_HOME:-$HOME/.local/share}/osf/website-backup"
     27 
     28 [ ! -f "$URLS" ] && echo "Error: $URLS is not a file." && exit 1
     29 
     30 backup() {
     31   mkdir -p "$BACKUPS/$2"
     32   output="$BACKUPS/$2/$(date +"%Y-%m-%d")-$2"
     33   last="$BACKUPS/$2/$(date --date="yesterday" +"%Y-%m-%d")-$2"
     34   # save new copy
     35   curl -s -X GET -H "X-Auth-Token: $3" "$1" > "$output" \
     36     || notify "Website backup error" "Error backing up $2"
     37   # delete last if duplicated
     38   cmp -s "$output" "$last" && rm "$last"
     39 }
     40 
     41 while read -r url file token
     42 do
     43   backup "$url" "$file" "$token"
     44 done < "$URLS"
     45 
     46 # Can also be used by calling backup directly. Example:
     47 # backup url file [token]