This repository has been archived by the owner on Jan 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.sh
75 lines (62 loc) · 1.67 KB
/
backup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash
VERSION=2.0.0
usage() {
echo "usage: $0 <API_KEY> <BACKUP_DIR> <KEEP_THIS_MANY>"
echo
echo "BACKUPS GO IN THE BACKUP_DIR (DUH). YOU MUST SUPPLY AN API KEY"
echo "AN IT MUST HAVE THE :tips.search: PERMISSION (ALSO, DUH)."
echo
echo "YOU MUST ALSO SPECIFY HOW MANY BACKUPS WILL BE KEPT WITH KEEP_THIS_MANY."
echo "OLDER BACKUPS WILL BE PRUNED LOVINGLY BY A TINY JAPANESE GARDENER."
echo
echo "THIS IS A SHELL SCRIPT SO IT'S ALREADY SHITTY AND FULL OF BUGS."
echo "IT'S LIKE GOD FORGOT ABOUT HIS EMBARRASING TEENAGE CREATION BUT"
echo "SOMEHOW BESTOWED IT UPON MAN WITH GREAT CEREMONY."
echo
echo "Optional arguments"
echo " -v, --version SHOW THE CURRENT VERSION AND EXIT"
echo " -h, --help SHOW THIS HELP"
exit 1
}
echoerr() {
printf "%s\n" "$*" >&2
}
if [[ "$#" -eq "0" ]]; then
usage
fi
OPTS=$(getopt -o v::h:: --long version,help -n $0 -- "$@")
while true; do
case "$1" in
-v|--version)
echo "$0, version $VERSION"
exit
;;
-h|--help)
usage
;;
*)
break
;;
esac
done
API_KEY=$1
BACKUP_DIR=$2
KEEP_THIS_MANY=720
URL="https://frog.tips/api/2/tips/search"
CURL_OUT="$BACKUP_DIR/$(date -u +'%Y-%m-%dT%H%M%SZ').json"
[ -d $BACKUP_DIR ] || mkdir $BACKUP_DIR
echoerr "---> DOWNLOADING JSON"
curl -H "Content-Type: application/json" -H "Authorization: $API_KEY" \
--fail \
-X POST \
-d '{}' \
--output $CURL_OUT \
$URL > /dev/null 2>&1 && \
gzip $CURL_OUT
if [[ "$?" -ne "0" ]]; then
echoerr "ERROR: JSON COULD NOT BE DOWNLOADED."
exit 2
fi
echoerr "---> ROTATING BACKUPS"
ls -1 $BACKUP_DIR/* | sort -r | tail -n +$KEEP_THIS_MANY | xargs rm > /dev/null 2>&1
echoerr "DONE."