-
Notifications
You must be signed in to change notification settings - Fork 14
/
lnd-channel-backup-dropbox.sh
176 lines (144 loc) · 3.97 KB
/
lnd-channel-backup-dropbox.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/bin/bash
# This script listens for changes to lnd's channel backup file
# and when changes are detected, uploads the timestamped file to dropbox
# Forked from https://gist.github.com/vindard/e0cd3d41bb403a823f3b5002488e3f90
# See above link for dropbox API setup guide and systemd setup guide
# SET DROPBOX API KEY FOR UPLOADS
DROPBOX_APITOKEN="ADD_OAUTH_LONG_LIVED_TOKEN_WITH_WRITE_ACCESS_HERE"
# OPTIONAL SET A DEVICE NAME TO BE USED FOR BACKUPS (DEFAULTS TO /etc/hostname)
DEVICE=""
# INOTIFY CHECK
# --------------
install_inotify () {
sudo apt update
sudo apt install -y inotify-tools
}
inotifycheck () {
dpkg -s "inotify-tools" &> /dev/null
if [ ! $? -eq 0 ]; then
install_inotify
fi
}
install_jq () {
sudo apt update
sudo apt install -y jq
}
jqcheck () {
dpkg -s "jq" &> /dev/null
if [ ! $? -eq 0 ]; then
install_jq
fi
}
# SETUP
# --------------
setup_files_and_folders () {
# Fetches the user whose home folder the directories will be stored under
ADMINUSER=( $(ls /home | grep -v bitcoin) )
if [ -z "$DEVICE" ] ; then
DEVICE=$(echo $(cat /etc/hostname))
fi
DEVICE=$(echo $DEVICE | awk '{print tolower($0)}' | sed -e 's/ /-/g')
# Setup folders and filenames
DATADIR=/home/$ADMINUSER/.lnd
WORKINGDIR=/home/$ADMINUSER/.lnd/channel-backups
BACKUPFOLDER=$DEVICE
# channel.backup file details
CHANFILEDIR=data/chain/bitcoin/mainnet
BACKUPFILE=channel.backup
SOURCEFILE=$DATADIR/$CHANFILEDIR/$BACKUPFILE
# Make sure necessary folders exist
if [[ ! -e ${WORKINGDIR} ]]; then
mkdir -p ${WORKINGDIR}
fi
cd ${WORKINGDIR}
if [[ ! -e ${BACKUPFOLDER} ]]; then
mkdir -p ${BACKUPFOLDER}
fi
cd ${BACKUPFOLDER}
}
# CHECKS
# --------------
online_check () {
wget -q --tries=10 --timeout=20 --spider http://google.com
if [[ $? -eq 0 ]]; then
ONLINE=true
else
ONLINE=false
fi
#echo "Online: "$ONLINE
}
dropbox_api_check () {
VALID_DROPBOX_APITOKEN=false
curl -s -X POST https://api.dropboxapi.com/2/users/get_current_account \
--header "Authorization: Bearer "$DROPBOX_APITOKEN | grep rror
if [[ ! $? -eq 0 ]] ; then
VALID_DROPBOX_APITOKEN=true
else
echo "Invalid Dropbox API Token!"
fi
}
dropbox_upload_check () {
UPLOAD_TO_DROPBOX=false
if [ ! -z $DROPBOX_APITOKEN ] ; then
online_check
if [ $ONLINE = true ] ; then
dropbox_api_check
else
echo "Please check that the internet is connected and try again."
fi
if [ $VALID_DROPBOX_APITOKEN = true ] ; then
UPLOAD_TO_DROPBOX=true
fi
fi
}
# UPLOAD
# --------------
upload_to_dropbox () {
FINISH=$(curl -s -X POST https://content.dropboxapi.com/2/files/upload \
--header "Authorization: Bearer "${DROPBOX_APITOKEN}"" \
--header "Dropbox-API-Arg: {\"path\": \"/"$BACKUPFOLDER"/"$1"\",\"mode\": \"overwrite\",\"autorename\": true,\"mute\": false,\"strict_conflict\": false}" \
--header "Content-Type: application/octet-stream" \
--data-binary @$1)
#echo $FINISH
UPLOADTIME=$(echo $FINISH | jq -r .server_modified)
if [ ! -z $UPLOADTIME ] ; then
echo "Successfully uploaded!"
else
echo "Unknown error when uploading..."
fi
}
# RUN CHECKS AND IF PASS, EXECUTE BACKUP TO DROPBOX
run_dropbox_backup () {
dropbox_upload_check
if [ $UPLOAD_TO_DROPBOX = true ] ; then
upload_to_dropbox $1
fi
}
##############
# RUN SCRIPT
##############
run_backup_on_change () {
echo "Copying backup file..."
BACKUPFILE_TIMESTAMPED=$BACKUPFILE-$(date +%s)
cp $SOURCEFILE $BACKUPFILE_TIMESTAMPED
md5sum $SOURCEFILE > $BACKUPFILE_TIMESTAMPED.md5
sed -i 's/\/.*\///g' $BACKUPFILE_TIMESTAMPED.md5
echo
echo "Uploading backup file: '"${BACKUPFILE_TIMESTAMPED}"'..."
run_dropbox_backup $BACKUPFILE_TIMESTAMPED
echo "---"
echo "Uploading signature: '"${BACKUPFILE_TIMESTAMPED}.md5"'..."
run_dropbox_backup $BACKUPFILE_TIMESTAMPED.md5
}
run () {
inotifycheck
jqcheck
setup_files_and_folders
run_backup_on_change
while true; do
inotifywait $SOURCEFILE
run_backup_on_change
echo
done
}
run