-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtakesnapshot.sh
More file actions
180 lines (137 loc) · 6.02 KB
/
Copy pathtakesnapshot.sh
File metadata and controls
180 lines (137 loc) · 6.02 KB
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
#!/bin/bash
# Local Folder Params #####################################################
datafolder=/root/data
statehistoryfolder=$datafolder/state-history
blocksfolder=$datafolder/blocks
snapshotsfolder=$datafolder/snapshots
compressedfolder=$snapshotsfolder/compressed/
#Script files #############################################################
shcreate=$snapshotsfolder/compressandsendlastsnapshot.sh
shcreatefull=$snapshotsfolder/compressandsendlastsnapshot_full.sh
shcreatefullstate=$snapshotsfolder/compressandsendlastsnapshot_fullstate.sh
testsnapshot=0
testblocks=0
teststatehistory=0
#Either a remote server can be used to send the snapshots to, or
#if you have apache installed locally then you can send to that instead
remotewebserver=0
#Remote Server Params #####################################################
remote_server_folder=/var/www/geordier.co.uk/snapshots
sshportno=22
# Local Web Server Params (if used)
local_server_folder=/var/www/remsnapshots.geordier.co.uk/snapshots
#Misc Params ##############################################################
datename=$(date +%Y-%m-%d_%H-%M)
filename="$compressedfolder$datename"
chainstopped=0
#End of Parameters ########################################################
mkdir -p $compressedfolder
chmod +x $compressedfolder
thehour=$(date +"%-H")
#thehour=0
#Run every 3 hours a day by MOD the hour by 3
if [[ $(($thehour%3)) -eq 0 ]] || [[ $testsnapshot -eq 1 ]]
then
echo "# Snapshot Only Start. Hour is $thehour #"
snapname=$(curl http://127.0.0.1:8888/v1/producer/create_snapshot | jq '.snapshot_name')
rm -f $shcreate
touch $shcreate && chmod +x $shcreate
if [[ $remotewebserver -eq 0 ]]
then
echo "Creating local snapshot..."
echo "find $local_server_folder -name \"*.gz\" -type f -size -1000k -delete" >> $shcreate
echo "ls -F $local_server_folder/*.gz | head -n -8 | xargs -r rm" >> $shcreate
echo "tar -Scvzf $filename-snaponly.tar.gz $snapname" >> $shcreate
echo "mv $filename-snaponly.tar.gz $local_server_folder" >> $shcreate
else
echo "tar -Scvzf $filename-snaponly.tar.gz $snapname" >> $shcreate
echo "ssh -p $sshportno $remote_user 'find $remote_server_folder -name \"*.gz\" -type f -size -1000k -delete'" >> $shcreate
echo "ssh -p $sshportno $remote_user 'ls -F $remote_server_folder/*.gz | head -n -8 | xargs -r rm'" >> $shcreate
echo "rsync -rv -e 'ssh -p $sshportno' --progress $filename-snaponly.tar.gz $remote_user:$remote_server_folder" >> $shcreate
echo "Sending snapshot only..."
fi
$shcreate
else
echo "Snapshot is not due..Aborting"
fi
#Run twice a day by MOD the hour by 12
if [[ $(($thehour%12)) -eq 0 ]] || [[ $testblocks -eq 1 ]]
then
echo "Get Head and Irreversible Block Numbers"
head_block_num=$(remcli get info | jq '.head_block_num')
last_irr_block_num=$(remcli get info | jq '.last_irreversible_block_num')
# Now we wait for last irreversible block to pass our snapshot taken
while [ $last_irr_block_num -le $head_block_num ]
do
last_irr_block_num=$(remcli get info | jq '.last_irreversible_block_num')
ans=$(($head_block_num-$last_irr_block_num))
echo "Last Irreversible Block Reached In $ans Blocks"
sleep 10
done
echo "Last Irreversible Block Number Passed - Great, lets stop the chain now"
~/stop.sh
chainstopped=1
echo "#Blocks Log Start #"
rm -f $shcreatefull
touch $shcreatefull && chmod +x $shcreatefull
if [[ $remotewebserver -eq 0 ]]
then
echo "Creating blocks log..."
echo "find $local_server_folder/blocks -name \"*.gz\" -type f -size -1000k -delete" >> $shcreatefull
echo "ls -F $local_server_folder/blocks/*.gz | head -n -1 | xargs -r rm" >> $shcreatefull
echo "tar -Scvzf $filename-blockslog.tar.gz $blocksfolder/blocks.log $blocksfolder/blocks.index" >> $shcreatefull
echo "mv $filename-blockslog.tar.gz $local_server_folder/blocks" >> $shcreatefull
else
echo "tar -Scvzf $filename-blockslog.tar.gz $blocksfolder/blocks.log $blocksfolder/blocks.index" >> $shcreatefull
echo "ssh -p $sshportno $remote_user 'find $remote_server_folder/blocks -name \"*.gz\" -type f -size -1000k -delete'" >> $shcreatefull
echo "ssh -p $sshportno $remote_user 'ls -F $remote_server_folder/blocks/*.gz | head -n -1 | xargs -r rm'" >> $shcreatefull
echo "rsync -rv -e 'ssh -p $sshportno' --progress $filename-blockslog.tar.gz $remote_user:$remote_server_folder/blocks" >> $shcreatefull
fi
echo "Sending blocks..."
$shcreatefull
else
echo "Blocks Log is not due..Aborting"
fi
#echo "exiting..."
#exit 1
#Run twice a day by MOD the hour by 12
if [[ $(($thehour%12)) -eq 0 ]] || [[ $teststatehistory -eq 1 ]]
then
echo "#State History Start #"
rm -f $shcreatefullstate
touch $shcreatefullstate && chmod +x $shcreatefullstate
if [[ $remotewebserver -eq 0 ]]
then
echo "find $local_server_folder/state-history -name \"*.gz\" -type f -size -1000k -delete" >> $shcreatefullstate
echo "ls -F $local_server_folder/state-history/*.gz | head -n -1 | xargs -r rm" >> $shcreatefullstate
echo "tar -Scvzf $filename-statehistory.tar.gz $statehistoryfolder " >> $shcreatefullstate
echo "mv $filename-statehistory.tar.gz $local_server_folder/state-history" >> $shcreatefullstate
else
echo "tar -Scvzf $filename-statehistory.tar.gz $statehistoryfolder " >> $shcreatefullstate
echo "ssh -p $sshportno $remote_user 'find $remote_server_folder/state-history -name \"*.gz\" -type f -size -1000k -delete'" >> $shcreatefullstate
echo "ssh -p $sshportno $remote_user 'ls -F $remote_server_folder/state-history/*.gz | head -n -1 | xargs -r rm'" >> $shcreatefullstate
echo "rsync -rv -e 'ssh -p $sshportno' --progress $filename-statehistory.tar.gz $remote_user:$remote_server_folder/state-history" >> $shcreatefullstate
fi
echo "Sending state history..."
$shcreatefullstate
else
echo "State History is not due...Aborting"
fi
############ Cleanup ##############
echo "Cleaning Up..."
rm -f $shcreate
rm -f $shcreatefull
rm -f $shcreatefullstate
rm -R $snapshotsfolder/*.gz
rm -R $snapshotsfolder/*.bin
rm -R $compressedfolder/*.gz
rm -R $statehistoryfolder/*.gz
rm -R $blocksfolder/*.gz
if [[ $chainstopped -eq 1 ]]
then
echo "Starting chain..."
cd ~
./start.sh
fi
echo "We are done"