forked from mt1381/oxy-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oxy-snapshot.sh
executable file
·133 lines (115 loc) · 3.72 KB
/
oxy-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
#!/bin/bash
VERSION="0.1"
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
#============================================================
#= snapshot.sh v0.2 created by mrgr =
#= Please consider voting for delegate mrgr =
#============================================================
echo " "
if [ ! -f ../oxy-node/app.js ]; then
echo "Error: No OXYCOIN installation detected. Exiting."
exit 1
fi
if [ "\$USER" == "root" ]; then
echo "Error: oxy-snapshot should not be run be as root. Exiting."
exit 1
fi
OXY_CONFIG=~/oxy-node/config.json
DB_NAME="$(grep "database" $OXY_CONFIG | cut -f 4 -d '"')"
DB_USER="$(grep "user" $OXY_CONFIG | cut -f 4 -d '"')"
DB_PASS="$(grep "password" $OXY_CONFIG | cut -f 4 -d '"' | head -1)"
SNAPSHOT_COUNTER=snapshot/counter.json
SNAPSHOT_LOG=snapshot/snapshot.log
if [ ! -f "snapshot/counter.json" ]; then
sudo rm /home/snap/snapshot -R
echo "Error No problem"
mkdir -p snapshot
sudo chmod a+x oxy-snapshot.sh
echo "0" > $SNAPSHOT_COUNTER
sudo chown postgres:${USER:=$(/usr/bin/id -run)} snapshot
sudo chmod -R 777 snapshot
fi
SNAPSHOT_DIRECTORY=snapshot/
NOW=$(date +"%d-%m-%Y - %T")
################################################################################
create_snapshot() {
export PGPASSWORD=$DB_PASS
echo " + Creating snapshot"
echo "--------------------------------------------------"
echo "..."
sudo su postgres -c "pg_dump -Fp $DB_NAME > $SNAPSHOT_DIRECTORY'blockchain.db'"
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'));"`
gzip snapshot/blockchain.db
sudo apt-get install apache2 -y
sudo cp snapshot/blockchain.db.gz /var/www/html
if [ $? != 0 ]; then
echo "X Failed to create snapshot." | tee -a $SNAPSHOT_LOG
exit 1
else
echo "$NOW -- OK snapshot created successfully at block $blockHeight ($dbSize)." | tee -a $SNAPSHOT_LOG
echo "If you want to stop the webserver run: sudo service apache2 stop"
fi
}
restore_snapshot(){
echo " + Restoring snapshot"
echo "--------------------------------------------------"
SNAPSHOT_FILE=`ls -t $SNAPSHOT_DIRECTORY$DB_NAME* | head -1`
if [ -z "$SNAPSHOT_FILE" ]; then
echo "****** No snapshot to restore, please consider create it first"
echo " "
exit 1
fi
echo "Snapshot to restore = $SNAPSHOT_FILE"
read -p "Please stop node app.js first, are you ready (y/n)? " -n 1 -r
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
echo "***** Please stop node.js first.. then execute restore again"
echo " "
exit 1
fi
#snapshot restoring..
export PGPASSWORD=$DB_PASS
pg_restore -d $DB_NAME "$SNAPSHOT_FILE" -U $DB_USER -h localhost -c -n public
if [ $? != 0 ]; then
echo "X Failed to restore."
exit 1
else
echo "OK snapshot restored successfully."
fi
}
show_log(){
echo " + Snapshot Log"
echo "--------------------------------------------------"
cat snapshot/snapshot.log
echo "--------------------------------------------------END"
}
################################################################################
case $1 in
"create")
create_snapshot
;;
"restore")
restore_snapshot
;;
"log")
show_log
;;
"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 "Error: Unrecognized command."
echo ""
echo "Available commands are: create, restore, log, help"
echo "Try: bash oxy-snapshot.sh help"
;;
esac