-
Notifications
You must be signed in to change notification settings - Fork 5
/
migration.sh
executable file
·136 lines (109 loc) · 4.36 KB
/
migration.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
set -eo pipefail
V1_JAR="h2-1.4.200.jar"
V2_JAR="h2-2.1.212.jar"
V3_JAR="h2-2.2.220.jar"
V1_URL="https://repo1.maven.org/maven2/com/h2database/h2/1.4.200/h2-1.4.200.jar"
V2_URL="https://repo1.maven.org/maven2/com/h2database/h2/2.1.212/h2-2.1.212.jar"
V3_URL="https://repo1.maven.org/maven2/com/h2database/h2/2.2.220/h2-2.2.220.jar"
sourceVer=v2
destVer=v3
while getopts f:u:p:s:d: flag
do
case "${flag}" in
f) file=${OPTARG};;
u) username=${OPTARG};;
p) password=${OPTARG};;
s) sourceVer=${OPTARG};;
d) destVer=${OPTARG};;
esac
done
if [ ! -f "$file.mv.db" ]; then
echo "===> $file.mv.db is not a valid path. Please use -f option to provide the path to the database file. e.g. migration.sh -f path/to/database/file without extension"
exit 255
fi
# Check sourceVer has values v1, v2 or v3
if [ "$sourceVer" != "v1" ] && [ "$sourceVer" != "v2" ] && [ "$sourceVer" != "v3" ]; then
echo "===> $sourceVer is not a valid source version. Please use -s option to provide the source version. e.g. migration.sh -s v2 -d v3"
exit 255
fi
# Check destVer has values v1, v2 or v3
if [ "$destVer" != "v1" ] && [ "$destVer" != "v2" ] && [ "$destVer" != "v3" ]; then
echo "===> $destVer is not a valid destination version. Please use -d option to provide the destination version. e.g. migration.sh -s v2 -d v3"
exit 255
fi
echo "===> Start to migrate h2 $sourceVer database from file: $file to $destVer"
echo "===> Use db username: $username"
echo "===> Use db password: $password"
echo "===> Check output directory."
OUTPUT_DIR=output
if [ -d "$OUTPUT_DIR" ]; then
# Take action if $DIR exists. #
echo "===> $OUTPUT_DIR directory exists."
else
mkdir $OUTPUT_DIR
echo "===> Create the $OUTPUT_DIR."
fi
if [ -d "$OUTPUT_DIR/$destVer" ]; then
echo "===> Remove generated data from last run"
rm -rf "$OUTPUT_DIR/$destVer"
fi
echo "===> Start to download JDBC drivers: "
EXTRA_IMPORT_OPTS=""
# Check source version
case $sourceVer in
v1)
EXTRA_IMPORT_OPTS+=" -options FROM_1X"
SOURCE_JAR=$OUTPUT_DIR/$V1_JAR
curl $V1_URL --output $SOURCE_JAR
;;
v2)
SOURCE_JAR=$OUTPUT_DIR/$V2_JAR
curl $V2_URL --output $SOURCE_JAR
;;
v3)
SOURCE_JAR=$OUTPUT_DIR/$V3_JAR
curl $V3_URL --output $SOURCE_JAR
;;
esac
echo "===> Downloaded source version driver: $SOURCE_JAR"
# Get dest version driver
case $destVer in
v1)
DEST_JAR=$OUTPUT_DIR/$V1_JAR
curl $V1_URL --output $DEST_JAR
;;
v2)
DEST_JAR=$OUTPUT_DIR/$V2_JAR
curl $V2_URL --output $DEST_JAR
;;
v3)
DEST_JAR=$OUTPUT_DIR/$V3_JAR
curl $V3_URL --output $DEST_JAR
;;
esac
echo "===> Downloaded dest version driver: $DEST_JAR"
echo "===> Jdbc drivers downloaded."
echo ""
echo "===> Start to export current database into SQL script: "
java -cp $SOURCE_JAR org.h2.tools.Script -url "jdbc:h2:$file" -user "$username" -password "$password" -script "./$OUTPUT_DIR/backup.$sourceVer.sql"
echo "===> Done export."
echo ""
echo "===> Start to create h2 $destVer database from the exported SQL script: "
java -cp $DEST_JAR org.h2.tools.RunScript -url "jdbc:h2:./$OUTPUT_DIR/$destVer/data/grailsdb" -user "$username" -password "$password" -script "./$OUTPUT_DIR/backup.$sourceVer.sql" $EXTRA_IMPORT_OPTS
echo "===> $destVer database has been created at ./$OUTPUT_DIR/$destVer/data/grailsdb.mv.db"
echo ""
# If source version is v1, then we need to fix the database change logs
if [ "$sourceVer" == "v1" ]; then
echo "===> Fix databsae change logs"
java -cp $DEST_JAR org.h2.tools.RunScript -url "jdbc:h2:./$OUTPUT_DIR/$destVer/data/grailsdb" -user "$username" -password "$password" -script "./changelog_backup.sql"
echo "===> Database change logs has been fixed."
echo ""
fi
echo "================================================================================================================="
echo ""
echo "Please copy the new database file ./$OUTPUT_DIR/$destVer/data/grailsdb.mv.db to your {RUNDECK_HOME}/server/data/ folder to replace the old version database"
echo ""
echo "Please run command \`chown rundeck:root {RUNDECK_HOME}/server/data/grailsdb.mv.db\` to set the right permission on the new database file"
echo ""
echo "================================================================================================================="