-
Notifications
You must be signed in to change notification settings - Fork 8
/
shift-snapshot.sh
executable file
·127 lines (109 loc) · 3.48 KB
/
shift-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
#!/bin/bash
VERSION="0.2"
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 ../shift/app.js ]; then
echo "Error: No shift installation detected. Exiting."
exit 1
fi
if [ "\$USER" == "root" ]; then
echo "Error: SHIFT should not be run be as root. Exiting."
exit 1
fi
SHIFT_CONFIG=~/shift/config.json
DB_NAME="$(grep "database" $SHIFT_CONFIG | cut -f 4 -d '"')"
DB_USER="$(grep "user" $SHIFT_CONFIG | cut -f 4 -d '"')"
DB_PASS="$(grep "password" $SHIFT_CONFIG | cut -f 4 -d '"' | head -1)"
SNAPSHOT_COUNTER=snapshot/counter.json
SNAPSHOT_LOG=snapshot/snapshot.log
if [ ! -f "snapshot/counter.json" ]; then
mkdir -p snapshot
sudo chmod a+x shift-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 -Ft $DB_NAME > $SNAPSHOT_DIRECTORY'shift_db$NOW.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
echo "$NOW -- OK snapshot created successfully at block$blockHeight ($dbSize)." | tee -a $SNAPSHOT_LOG
fi
}
restore_snapshot(){
echo " + Restoring snapshot"
echo "--------------------------------------------------"
SNAPSHOT_FILE=`ls -t snapshot/shift_db* | 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 shift-snapshot.sh help"
;;
esac