This repository has been archived by the owner on Jul 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restore.sh
executable file
·176 lines (144 loc) · 4.96 KB
/
restore.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
#!/bin/bash
function printUsage {
echo ""
echo "Usage:"
echo " restore.sh [--backupDir=<backup_directory_path>] [--configDir=<config_directory_path>]"
echo "Options:"
echo " --backupDir=<backup_directory_path>\
Specify the path to the folder containing the backup data on your device. Defaults to the folder with the most recent timestamp inside the backup/ directory"
echo " --backupDir=<config_directory_path>
Specify the path to the folder inside the docker container where configuration files are stored. Defaults to /ot-node/data/"
echo ""
}
BACKUPDIR="backup"
CONFIGDIR="none"
CONTAINER_NAME="otnode"
for i in "$@"
do
case $i in
-h|--help=*)
printUsage
exit 0
# past argument=value
;;
--configDir=*)
CONFIGDIR="${i#*=}"
shift # past argument=value
;;
--backupDir=*)
BACKUPDIR="${i#*=}"
shift # past argument with no value
;;
--containerName=*)
CONTAINER_NAME="${i#*=}"
shift # past argument with no value
;;
*)
echo "Unknown option detected ${i}"
printUsage
exit 2
;;
esac
done
# Load backup directory path
if [ ${BACKUPDIR} == "none" ]
then
echo "No backup directory specified, loading last backup from ./backup folder"
echo ""
# Find the latest backup file
dateExpression=[1-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]T
allBackups=($(ls -dr backup/* | grep "$backup/$dateExpression"))
latestBackupDir=${allBackups[@]:0:1}
BACKUPDIR=${latestBackupDir}
fi
if [ -d ${BACKUPDIR} ]
then
echo "Using ${BACKUPDIR} as the backup directory"
echo ""
else
echo "Given backup directory parameter ${BACKUPDIR} is not a directory!"
printUsage
exit 1
fi
# Load config directory path
if [ ${CONFIGDIR} == "none" ]
then
echo "No config directory specified, using /ot-node/data as default"
echo ""
CONFIGDIR="/ot-node/data"
else
echo "Using ${CONFIGDIR} as the data directory"
echo ""
fi
temp_folder=$BACKUPDIR
for file in `ls ${BACKUPDIR}`; do
if [ ! ${file}] == "arangodb" ]
then
sourcePath="${BACKUPDIR}/${file}"
destinationPath="${CONTAINER_NAME}:${CONFIGDIR}/"
sourcePath=./${temp_folder}/${file}
echo "docker cp ${sourcePath} ${destinationPath}"
docker cp ${sourcePath} ${destinationPath}
fi
done
sourcePath="${BACKUPDIR}/.origintrail_noderc"
destinationPath="${CONTAINER_NAME}:/ot-node/current/"
sourcePath=./${temp_folder}/.origintrail_noderc
echo "docker cp ${sourcePath} ${destinationPath}"
docker cp ${sourcePath} ${destinationPath}
identitiesDir="${BACKUPDIR}/identities"
if [ -d ${identitiesDir} ]
then
sourcePath="${BACKUPDIR}/identities"
destinationPath="${CONTAINER_NAME}:${CONFIGDIR}/"
sourcePath=./${temp_folder}/identities
echo "docker cp ${sourcePath} ${destinationPath}"
docker cp ${sourcePath} ${destinationPath}
fi
certFiles=(fullchain.pub privkey.pem)
if [ -e "${BACKUPDIR}/fullchain.pem" ] && [ -e "${BACKUPDIR}/privkey.pem" ]
then
echo "mkdir ${temp_folder}/certs"
mkdir ${temp_folder}/certs
echo "cp ${BACKUPDIR}/fullchain.pem ./${temp_folder}/certs/"
cp ${BACKUPDIR}/fullchain.pem ./${temp_folder}/certs
echo "cp ${BACKUPDIR}/privkey.pem ./${temp_folder}/certs/"
cp ${BACKUPDIR}/privkey.pem ./${temp_folder}/certs
echo "docker cp ${temp_folder}/certs ${CONTAINER_NAME}:/ot-node/"
docker cp ${temp_folder}/certs otnode:/ot-node/
else
echo "Cert files do not exits, skipping..."
fi
migrationDir="${BACKUPDIR}/migrations"
if [ -d ${migrationDir} ]
then
sourcePath="${BACKUPDIR}/migrations"
destinationPath="${CONTAINER_NAME}:${CONFIGDIR}/"
sourcePath=./${temp_folder}/migrations
echo "docker cp ${sourcePath} ${destinationPath}"
docker cp ${sourcePath} ${destinationPath}
fi
echo docker cp ${CONTAINER_NAME}:/ot-node/current/config/config.json ./
docker cp ${CONTAINER_NAME}:/ot-node/current/config/config.json ./
rm config.json
databaseName=$(cat ${BACKUPDIR}/arangodb/database.txt)
echo "database name ${databaseName}"
databaseUsername=$(cat ${BACKUPDIR}/arangodb/username.txt)
echo "database username ${databaseUsername}"
echo "docker cp ${temp_folder}/arangodb ${CONTAINER_NAME}:${CONFIGDIR}/"
docker cp "${temp_folder}/arangodb" ${CONTAINER_NAME}:${CONFIGDIR}/
echo rm -rf ${temp_folder}
rm -rf ${temp_folder}
echo docker start ${CONTAINER_NAME}
docker start ${CONTAINER_NAME}
echo sleep 30
sleep 30
docker cp ${CONTAINER_NAME}:${CONFIGDIR}/arango.txt arango.txt
databasePassword=$(cat arango.txt)
rm arango.txt
echo "docker exec ${CONTAINER_NAME} arangorestore --server.database ${databaseName} --server.username ${databaseUsername} --server.password \"${databasePassword}\" --input-directory ${CONFIGDIR}/arangodb/ --overwrite true"
docker exec ${CONTAINER_NAME} arangorestore --server.database ${databaseName} --server.username ${databaseUsername} --server.password "${databasePassword}" --input-directory ${CONFIGDIR}/arangodb/ --overwrite true
echo docker restart ${CONTAINER_NAME}
docker restart ${CONTAINER_NAME}
echo docker exec ${CONTAINER_NAME} rm -rf ${CONFIGDIR}/arangodb
docker exec ${CONTAINER_NAME} rm -rf ${CONFIGDIR}/arangodb