/
skp-snapshot.sh
237 lines (194 loc) · 7.03 KB
/
skp-snapshot.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/bin/bash
VERSION="0.3"
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
echo " "
if [ ! -f ../skelpy-node/app.js ]; then
echo "Error: No skp installation detected. Exiting."
exit 1
fi
if [ "\$USER" == "root" ]; then
echo "Error: SKELPY should not be run be as root. Exiting."
exit 1
fi
DB_NAME="skp_mainnet"
DB_USER=$USER
DB_PASS="password"
SNAPSHOT_COUNTER=snapshot/counter.json
SNAPSHOT_LOG=snapshot/snapshot.log
if [ ! -f "snapshot/counter.json" ]; then
mkdir -p snapshot
sudo chmod a+x skp-snapshot.sh
echo "0" > $SNAPSHOT_COUNTER
sudo chown postgres:${USER:=$(/usr/bin/id -run)} snapshot
sudo chmod -R 777 snapshot
fi
SNAPSHOT_DIRECTORY="snapshot/"
# Find parent PID
function top_level_parent_pid {
# Look up the parent of the given PID.
pid=${1:-$$}
if [ "$pid" != "0" ]; then
stat=($(</proc/${pid}/stat))
ppid=${stat[3]}
# /sbin/init always has a PID of 1, so if you reach that, the current PID is
# the top-level parent. Otherwise, keep looking.
if [[ ${ppid} -eq 1 ]] ; then
echo ${pid}
else
top_level_parent_pid ${ppid}
fi
else
pid=0
fi
}
function proc_vars {
node=`pgrep -a "node" | grep skelpy-node | awk '{print $1}'`
if [ "$node" == "" ] ; then
node=0
fi
# Is Postgres running
pgres=`pgrep -a "postgres" | awk '{print $1}'`
# Find if forever process manager is runing
frvr=`pgrep -a "node" | grep forever | awk '{print $1}'`
# Find the top level process of node
top_lvl=$(top_level_parent_pid $node)
# Looking for skelpy-node installations and performing actions
skpdir=`locate -b skelpy-node`
# Getting the parent of the install path
parent=`dirname $skpdir 2>&1`
# Forever Process ID
forever_process=`forever --plain list | grep $node | sed -nr 's/.*\[(.*)\].*/\1/p'`
# Node process work directory
nwd=`pwdx $node 2>/dev/null | awk '{print $2}'`
}
NOW=$(date +"%d-%m-%Y - %T")
################################################################################
create_snapshot() {
export PGPASSWORD=$DB_PASS
echo " Instance of SKELPY Node found with:"
echo " System PID: $node, Forever PID $forever_process"
echo " Stopping SKELPY Node..."
forever stop $forever_process >&- 2>&-
echo " + Creating snapshot"
echo "--------------------------------------------------"
echo "..."
SnapshotFilename=$SNAPSHOT_DIRECTORY'skp_db.snapshot.tar'
pg_dump -O "$DB_NAME" -Fc -Z6 > "$SnapshotFilename"
#sudo su postgres -c "pg_dump -Ft $DB_NAME > $SNAPSHOT_DIRECTORY'skp_db.snapshot.tar'"
blockHeight=`psql -d $DB_NAME -U $DB_USER -h localhost -p 5432 -t -c "select height from blocks order by height desc limit 1;"`
dbSize=`psql -d $DB_NAME -U $DB_USER -h localhost -p 5432 -t -c "select pg_size_pretty(pg_database_size('$DB_NAME'));"`
if [ $? != 0 ]; then
echo "X Failed to create snapshot." | tee -a $SNAPSHOT_LOG
exit 1
else
SNAPSHOT_FILE=`ls -t snapshot/skp_db* | head -1`
echo "$NOW -- OK snapshot created successfully at block$blockHeight ($dbSize)." | tee -a $SNAPSHOT_LOG
fi
echo " Starting SKELPY Node..."
cd $skpdir
forever start app.js -c ./config.mainnet.json -g ./genesisBlock.mainnet.json >&- 2>&-
#forever start app.js --genesis genesisBlock.mainnet.json --config config.mainnet.json >&- 2>&-
echo " ✔ SKELPY Node was successfully started"
}
restore_snapshot(){
echo " Instance of SKELPY Node found with:"
echo " System PID: $node, Forever PID $forever_process"
echo " Stopping SKELPY Node..."
forever stop $forever_process >&- 2>&-
echo " + Restoring snapshot"
echo "--------------------------------------------------"
SNAPSHOT_FILE=`ls -t snapshot/skp_db* | head -1`
if [ -z "$SNAPSHOT_FILE" ]; then
echo "****** No snapshot to restore, please consider create it first"
echo " "
exit 1
fi
#snapshot restoring..
export PGPASSWORD=$DB_PASS
pg_restore -O -j 8 -d skp_mainnet $SNAPSHOT_FILE 2>/dev/null
#pg_restore -d $DB_NAME "$SNAPSHOT_FILE" -U $DB_USER -h localhost -c -n public
echo "OK snapshot restored successfully."
cd $skpdir
forever start app.js -c ./config.mainnet.json -g ./genesisBlock.mainnet.json >&- 2>&-
#forever start app.js --genesis genesisBlock.mainnet.json --config config.mainnet.json >&- 2>&-
echo " ✔ SKELPY Node was successfully started"
}
show_log(){
echo " + Snapshot Log"
echo "--------------------------------------------------"
cat snapshot/snapshot.log
echo "--------------------------------------------------END"
}
schedule_cron(){
echo "All your crontab settings will be overwritten."
read -p "Do you want to continue (y/n)?" -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo " "
case $1 in
"hourly")
echo "0 * * * * cd $(pwd) && bash $(pwd)/skp-snapshot.sh create >> $(pwd)/cron.log" > schedule
sudo crontab schedule
echo "The snapshot has been scheduled every hour";
;;
"daily")
echo "0 0 * * * cd $(pwd) && bash $(pwd)/skp-snapshot.sh create >> $(pwd)/cron.log" > schedule
sudo crontab schedule
echo "The snapshot has been scheduled once a day";
;;
"weekly")
echo "0 0 * * 0 cd $(pwd) && bash $(pwd)/skp-snapshot.sh create >> $(pwd)/cron.log" > schedule
sudo crontab schedule
echo "The snapshot has been scheduled once a week";
;;
"monthly")
echo "0 0 1 * * cd $(pwd) && bash $(pwd)/skp-snapshot.sh create >> $(pwd)/cron.log" > schedule
sudo crontab schedule
echo "The snapshot has been scheduled once a month";
;;
*)
echo "Error: Wrong parameter for cron option."
;;
esac
rm schedule
fi
}
################################################################################
proc_vars
case $1 in
"create")
create_snapshot
;;
"restore")
restore_snapshot
;;
"log")
show_log
;;
"schedule")
schedule_cron $2
;;
"hello")
echo "Hello my friend - $NOW"
;;
"help")
echo "Available commands are: "
echo " create - Create new snapshot"
echo " restore - Restore the last snapshot available in folder snapshot/"
echo " log - Display log"
echo " schedule - Schedule snapshot creation periodically, available parameters:"
echo " - hourly"
echo " - daily"
echo " - weekly"
echo " - monthly"
echo " Example $ bash skp-snapshot.sh schedule daily"
;;
*)
echo "Error: Unrecognized command."
echo ""
echo "Available commands are: create, restore, log, cron, help"
echo "Try: bash skp-snapshot.sh help"
;;
esac