-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathspotify-backup.sh
executable file
·53 lines (40 loc) · 1.32 KB
/
spotify-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
#!/usr/bin/env bash
# Wrapper for spotify-backup https://github.com/caseychu/spotify-backup
# Requirements: jq(1)
# Add a crontab entry like:
# # Run every Sunday evening
# 0 20 * * 0 if_fail_do_notification spotify-backup.sh
spot_username=erikwestrup
date=$(date "+%Y-%m-%d-%H%M%S")
outdest=$HOME/bak/spotify
outfile="$outdest/${spot_username}_${date}"
outtxt=$outfile.txt
outjson=$outfile.json
backupper=$HOME/src/github.com/caseychu/spotify-backup/spotify-backup.py
# Redirect stdout ( > ) into a named pipe ( >() ) running "tee" to a file, so we can observe the status by simply tailing the log file.
me=$(basename "$0")
log_dir=${XDG_STATE_HOME:-$HOME/.local/state}/spotify-backup
log_file="${log_dir}/${date}_${me}.$$.log"
test -d $log_dir || mkdir -p $log_dir
exec > >(tee -i $log_file)
exec 2>&1
exec_with_retry() {
local n_tries="$1"
shift
local cmd="$@"
local is_done=0
while [ $is_done -ne 1 ] && [ $n_tries -ge 0 ]; do
echo "> ${n_tries} tries left."
eval $cmd
[ $? -eq 0 ] && is_done=1
n_tries=$((n_tries-1))
done
}
[ -d $outdest ] || mkdir -p $outdest
echo "> Backing up to $outtxt"
exec_with_retry 2 $backupper --format txt $outtxt
echo -e "\n\n\n\n"
echo "> Backing up to $outjson"
exec_with_retry 2 $backupper --format json $outjson.ugly
jq < $outjson.ugly > $outjson
rm -f $outjson.ugly