]> git.sur5r.net Git - bacula/bacula/blob - bacula/examples/database/sqlite2pgsql
Implement RestoreObject for sqlite + cleanups
[bacula/bacula] / bacula / examples / database / sqlite2pgsql
1 #!/bin/bash
2
3 # Import an SQLite dump of a Bacula catalog into Postgres
4 # Designed for v1.63.3 (as found on Debian sarge)
5 #
6 # v0.5
7
8 # Copyright (c) 2006 Russell Howe <russell_howe@wreckage.org>
9
10 # Permission is hereby granted, free of charge, to any person obtaining a
11 # copy of this software and associated documentation files (the "Software"),
12 # to deal in the Software without restriction, including without limitation
13 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 # and/or sell copies of the Software, and to permit persons to whom the
15 # Software is furnished to do so, subject to the following conditions:
16
17 # The above copyright notice and this permission notice shall be included in
18 # all copies or substantial portions of the Software.
19
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 # DEALINGS IN THE SOFTWARE.
27
28 FILE=bacula.sql
29 # Tables, in order of size
30 TABLES=(File Filename Path Job Media Pool CDImages Counters Version Client FileSet JobMedia NextId UnsavedFiles BaseFiles)
31 # Tables, in insert order
32 TABLESINSERT=(Pool CDImages Client Counters FileSet Filename Job Media JobMedia NextId Path File UnsavedFiles Version BaseFiles)
33 DBNAME=bacula
34 LOGFILE="/var/tmp/sqlite2pgsql.$$.log"
35
36 importdata() {
37         if [ "x" == "x$1" ]; then
38                 echo "Error: importdata() called without an argument. Aborting."
39                 exit 1
40         fi
41         
42         SQLFILE="$1"
43
44         if [ ! -r "$SQLFILE" ]; then
45                 echo "Error: Cannot read from $SQLFILE. Aborting."
46                 exit 1
47         fi
48
49         echo -n "Loading $SQLFILE into database $DBNAME..."
50         psql -d "$DBNAME" -f "$SQLFILE" || (
51                 echo "Failed to load $SQLFILE into database $DBNAME. psql exited with return code $?. Aborting."
52                 exit 1
53         )
54 }
55
56
57 # Go through each of the table names, splitting the INSERT statements off
58 # into seperate files
59 for table in ${TABLES[@]}; do
60         SRC="$FILE.other"
61         if [ ! -f "$FILE.other" ]; then
62                 SRC="$FILE"
63         fi
64         PATTERN="^INSERT INTO $table "
65         if [ ! -f "$FILE.data.$table" ]; then
66                 echo -n "Separating $table table from database dump..."
67
68                 echo "BEGIN;" > "$FILE.data.$table.tmp"
69                 grep "$PATTERN" "$SRC" >> "$FILE.data.$table.tmp"
70                 echo "COMMIT;" >> "$FILE.data.$table.tmp"
71                 
72                 mv "$FILE.data.$table.tmp" "$FILE.data.$table"
73                 echo "done. ($FILE.data.$table)"
74                 echo -n "Stripping matched lines from the source file to speed up the next round..."
75                 grep -v "$PATTERN" "$SRC" > "$FILE.other.tmp"
76                 mv "$FILE.other.tmp" "$FILE.other"
77                 echo "done."
78         else
79                 echo "$FILE.data.$table already exists. Assuming this table has already been split"
80                 echo "off from the main dump. Not regenerating."
81         fi
82 done
83
84 echo "Seperating DDL statements from INSERT statements"
85
86 grep -v "^INSERT" "$FILE.other" > "$FILE.ddl"
87 echo "DDL statements are now in $FILE.ddl"
88
89 grep "^INSERT" "$FILE.other" > "$FILE.data.other"
90 echo "Any remaining INSERT statements are now in $FILE.data.other"
91
92 echo "Fixing up datatypes used in the DDL..."
93
94 sed -e 's/TINYINT/SMALLINT/g' \
95     -e 's/DATETIME/TIMESTAMP/g' \
96     -e 's/INTEGER UNSIGNED/INTEGER/g' \
97     -e 's/BIGINT UNSIGNED/BIGINT/g' \
98     -e 's/INTEGER AUTOINCREMENT/SERIAL/g' \
99     -e s/\ DEFAULT\ \"\"/\ DEFAULT\ \'\'/g \
100     -e s#\ TIMESTAMP\ DEFAULT\ 0#\ TIMESTAMP\ DEFAULT\ \'1/1/1970\'#g "$FILE.ddl" > "$FILE.ddl.postgres"
101
102 echo "Fixing Pool table..."
103
104 sed -e 's/,0,0);$/,NULL,NULL);/' "$FILE.data.Pool" > "$FILE.data.Pool.fixed"
105
106 echo "Fixing removing entries from Job table which no longer have a Pool to link to"
107
108 # Remove jobs which refer to nonexistent pools, and fix up invalid start and end times to be 1/1/1970
109 grep -vE '([2589]|1[0-5]),[0-9]+,[0-9]+,[0-9]+\);' "$FILE.data.Job" \
110         |sed -e s@^\\\(INSERT\ INTO\ Job\ VALUES\(\\\(\[^,\]\\\+,\\\)\\\{8\\\}\\\)0,@\\1NULL,@ \
111         -e s@^\\\(INSERT\ INTO\ Job\ VALUES\(\\\(\[^,\]\\\+,\\\)\\\{9\\\}\\\)0,@\\1\NULL,@ \
112         -e s@^\\\(INSERT\ INTO\ Job\ VALUES\(\\\(\[^,\]\\\+,\\\)\\\{17\\\}\\\)0,@\\1\NULL,@ \
113         -e s@^\\\(INSERT\ INTO\ Job\ VALUES\(\\\(\[^,\]\\\+,\\\)\\\{18\\\}\\\)0,@\\1\NULL,@ \
114         -e s@^\\\(INSERT\ INTO\ Job\ VALUES\(\\\(\[^,\]\\\+,\\\)\\\{5\\\}\\\)0,@\\1NULL,@ > "$FILE.data.Job.fixed"
115
116 # Remove JobMedia entries which refer to nonexistent Jobs
117
118 echo "Cleaning up the dump of the JobMedia table..."
119
120 grep -vE 'INSERT INTO JobMedia VALUES\([0-9]+,([12589]|1[0-4]),' "$FILE.data.JobMedia" > "$FILE.data.JobMedia.fixed"
121
122 # Remove File entries which refer to nonexistent Jobs
123
124 echo "Cleaning up the dump of the File table..."
125
126 grep -vE 'INSERT INTO File VALUES\([0-9]+,[0-9]+,([12589]|1[0-4]),' "$FILE.data.File" > "$FILE.data.File.fixed"
127
128 echo "OK, we should be ready to import data into PostgreSQL now. DDL first..."
129 echo "This will probably fail the first time. You will have to edit $FILE.other"
130 echo "and rearrange the CREATE TABLE statements so that the tables are created"
131 echo "in the correct order."
132 echo "After editing $FILE.other, simply rerun this script and it will carry on"
133 echo "where it left off."
134
135 importdata "$FILE.ddl.postgres"
136
137 for table in ${TABLESINSERT[@]} other; do
138         IMPORTFILE="$FILE.data.$table"
139         if [ -f "$FILE.data.$table.fixed" ]; then
140                 IMPORTFILE="$FILE.data.$table.fixed"
141         fi
142         importdata "$IMPORTFILE" 2>&1 |tee -a "$LOGFILE"
143 done
144
145 echo "All done! Check $LOGFILE for errors."
146