-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathrM-sync.sh
executable file
·137 lines (117 loc) · 3.97 KB
/
rM-sync.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/bash
# Sync script for the reMarkable reader
# Version: 0.1
# Author: Simon Schilling
# Licence: MIT
# Remote configuration
RMDIR="/home/root/.local/share/remarkable/xochitl/"
RMUSER="root"
RMIP="10.11.99.1"
SSHPORT="22"
# Local configuration
MAINDIR="$HOME/rM"
BACKUPDIR="$MAINDIR/backup/" # rotating backups of all rM contents
UPLOADDIR="$MAINDIR/upload/" # all files here will be sent to rM
OUTPUTDIR="$MAINDIR/files/" # PDFs of everything on the rM
LOG="sync.log" # Log file name in $MAINDIR
BACKUPLIST="files.json"
# Behaviour
notification() {
if [ "$(uname)" == "Linux" ]; then
/usr/bin/notify-send $1 $2 # Notification script
elif [ "$(uname)" == "Darwin" ]; then
osascript -e "display notification \"$2\" with title \"$1\""
fi
}
LOG="$MAINDIR/$(date +%y%m%d)-$LOG"
# Create MAINDIR if it does not exist
mkdir -p $MAINDIR
echo $'\n' >> $LOG
date >> $LOG
S="ssh -p $SSHPORT -l $RMUSER";
# check for rM
$S $RMIP -q exit
if [ $? == "0" ]; then
TODAY=$(date +%y%m%d)
while getopts bdu opt
do
case $opt in
b)
# Backup files
echo "BEGIN BACKUP" | tee -a $LOG
mkdir -p "$BACKUPDIR$TODAY"
echo "scp \"$RMUSER@$RMIP:$RMDIR\" $BACKUPDIR$TODAY" >> $LOG
scp -r "$RMUSER@$RMIP:\"$RMDIR\"*" "$BACKUPDIR"$TODAY >> $LOG 2>&1
if [ $? -ne 0 ]; then
ERRORREASON=$ERRORREASON$'\n scp command failed'
ERROR=1
fi
# sed -s does not work on macOS (https://unix.stackexchange.com/a/131940)
if [ "$(uname)" == "Linux" ]; then
echo "[" > "$BACKUPDIR$TODAY$BACKUPLIST"
find "$BACKUPDIR$TODAY" -name *.metadata -type f -exec sed -s '$a,' {} + | sed '$d' >> "$BACKUPDIR$TODAY$BACKUPLIST"
echo "]" >> "$BACKUPDIR$TODAY$BACKUPLIST"
fi
echo "BACKUP END" | tee -a $LOG
;;
d)
# Download files
echo "BEGIN DOWNLOAD" | tee -a $LOG
mkdir -p "$OUTPUTDIR"
ls -1 "$BACKUPDIR$TODAY" | sed -e 's/\..*//g' | awk '!a[$0]++' > "$OUTPUTDIR/index"
echo "[" > "$OUTPUTDIR/index.json";
for file in "$BACKUPDIR$TODAY"/*.metadata;
do
[ -e "$file" ] || continue
echo "{" >> "$OUTPUTDIR/index.json";
echo " \"id\": \"$(basename "$file" .metadata)\"," >> "$OUTPUTDIR/index.json";
tail --lines=+2 "$file" >> "$OUTPUTDIR/index.json";
echo "," >> "$OUTPUTDIR/index.json";
done
truncate -s-2 "$OUTPUTDIR/index.json"; #Remove last koma
echo "]" >> "$OUTPUTDIR/index.json";
echo "Downloading" $(wc -l < "$OUTPUTDIR/index") "files." | tee -a $LOG
# http://$RMIP/download/$FILEUID/placeholder
while read -r line
do
FILEUID="$line"
#curl -s -O -J -L "http://$RMIP/download/$FILEUID/placeholder"
if [ $? -ne 0 ]; then
ERRORREASON=$ERRORREASON$'\n Download failed'
ERROR=1
fi
done < "$OUTPUTDIR/index"
echo "DOWNLOAD END" | tee -a $LOG
;;
u)
# Upload files
echo "BEGIN UPLOAD" | tee -a $LOG
for file in "$UPLOADDIR"/*;
do
[ -e "$file" ] || continue
echo -n $(basename "$file") ": "
curl --form "file=@\"$file\"" http://$RMIP/upload
echo "."
if [ 0 -eq $? ]; then rm "$file"; fi;
done
if [ $? -ne 0 ]; then
ERRORREASON=$ERRORREASON$'\n Upload failed'
ERROR=1
fi
echo "UPLOAD END" | tee -a $LOG
;;
esac
done
else
echo "reMarkable not connected" | tee -a $LOG
ERRORREASON=$ERRORREASON$'\n reMarkable not connected'
ERROR=1
fi
$DATE >> $LOG
if typeset -f notification > /dev/null; then
if [ $ERROR ]; then
notification "ERROR in rM Sync!" "$ERRORREASON"
else
notification "rM Sync Successfull"
fi
fi