Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

fix mysql2pgsql migration script

  • Loading branch information...
commit 100396e895f3e272a4ab5913a4d6c8771f03077d 1 parent 078dbcd
Paul J Stevens authored September 13, 2012
112  contrib/sql2sql/mysql2pgsql.sh
... ...
@@ -1,109 +1,109 @@
1 1
 #!/bin/bash
2 2
 
  3
+# this script can be used to migrate from MySQL to PostgreSQL
  4
+# 
  5
+# check postgresql.conf:
  6
+#
  7
+# bytea_output = 'escape'
  8
+# escape_string_warning = off
  9
+# standard_conforming_strings = off
  10
+#
  11
+# this script requires py-mysql2pgsql which utilizes the yaml script in
  12
+# this directory. Please edit the yaml file to match your setup.
  13
+#
  14
+
  15
+set -e
  16
+
3 17
 MYDB="dbmail"
4 18
 PGDB="dbmail"
5 19
 PGDD="../../sql/postgresql/create_tables.pgsql"
6 20
 
7  
-TMPDIR="/opt/tmp"
8 21
 
9 22
 export tables="
10 23
 dbmail_users
11 24
 dbmail_mailboxes
12 25
 dbmail_physmessage
13 26
 dbmail_messages
14  
-dbmail_messageblks
15 27
 dbmail_aliases
16 28
 dbmail_acl
  29
+dbmail_authlog
17 30
 dbmail_auto_notifications
18 31
 dbmail_auto_replies
19  
-dbmail_ccfield
20  
-dbmail_datefield
21  
-dbmail_envelope
22  
-dbmail_fromfield
23  
-dbmail_headername
24  
-dbmail_headervalue
25  
-dbmail_pbsp
26  
-dbmail_referencesfield
  32
+dbmail_filters
  33
+dbmail_keywords
27 34
 dbmail_replycache
28  
-dbmail_replytofield
29 35
 dbmail_sievescripts
30  
-dbmail_subjectfield
31 36
 dbmail_subscription
32  
-dbmail_tofield
33 37
 dbmail_usermap
34 38
 "
35 39
 
36  
-export_mysql()
37  
-{
38  
-	dumpfile=$TMPDIR/dbmail.mysqldata
39  
-	[ -e "$dumpfile" ] && return 1
40  
-	echo -n "export from mysql ..."
41  
-	mysqldump --skip-opt --single-transaction --hex-blob --compatible=postgresql -q -t -c $MYDB $tables > $dumpfile
42  
-	echo "done"
43  
-}
44  
-
45 40
 
46 41
 init_pgsql()
47 42
 {
48  
-	dropdb dbmail >/dev/null 2>&1
49  
-	createdb dbmail >/dev/null 2>&1
50  
-	psql dbmail < ../../sql/postgresql/create_tables.pgsql >/dev/null 2>&1 || { echo "create db failed. abort."; exit 1; }
51  
-	echo "delete from dbmail_users;"|psql dbmail
52  
-
  43
+	echo "clean out old DB ..."
  44
+	dropdb $PGDB >/dev/null 2>&1
  45
+	echo "create fresh DB ..."
  46
+	createdb -E UTF-8 -O dbmail $PGDB >/dev/null 2>&1
  47
+	echo "create schema ..."
  48
+	psql $PGDB < $PGDD >/dev/null 2>&1 || { echo "create db failed. abort."; exit 1; }
  49
+	echo "delete from dbmail_users;"|psql $PGDB >/dev/null 2>&1
  50
+	pgsql_owner
53 51
 }
  52
+
54 53
 import_pgsql()
55 54
 {
56  
-	dumpfile=$TMPDIR/dbmail.mysqldata
57  
-	echo -n "import into pgsql ..."
58  
-	cat $dumpfile | psql -q dbmail
59  
-	echo "done."
  55
+	for table in `echo $tables`; do
  56
+		echo -n "  migrate table: $table ..."
  57
+		mysqldump --compatible=postgresql -t --compact -c dbmail $table|psql dbmail >/dev/null 2>&1
  58
+		echo "done."
  59
+	done
  60
+	echo "  migrate tables: mimeparts, partlists ..."
  61
+	py-mysql2pgsql -v 2>/dev/null
  62
+	pgsql_sequences
60 63
 	return $?
61 64
 }
62 65
 
63 66
 pgsql_sequences()
64 67
 {
  68
+	echo -e "reset sequences ..."
65 69
 	qfile=`tempfile`
66 70
 	cat >> $qfile << EOQ
67 71
 BEGIN;
68  
-SELECT setval('dbmail_alias_idnr_seq', max(alias_idnr)) FROM dbmail_aliases;
69  
-SELECT setval('dbmail_user_idnr_seq', max(user_idnr)) FROM dbmail_users;
70  
-SELECT setval('dbmail_mailbox_idnr_seq', max(mailbox_idnr)) FROM dbmail_mailboxes;
71  
-SELECT setval('dbmail_physmessage_id_seq', max(id)) FROM dbmail_physmessage;
72  
-SELECT setval('dbmail_message_idnr_seq', max(message_idnr)) FROM dbmail_messages;
73  
-SELECT setval('dbmail_messageblk_idnr_seq', max(messageblk_idnr)) FROM dbmail_messageblks;
74  
-SELECT setval('dbmail_seq_pbsp_id', max(idnr)) FROM dbmail_pbsp;
75  
-SELECT setval('dbmail_headername_idnr_seq', max(id)) FROM dbmail_headername;
76  
-SELECT setval('dbmail_headervalue_idnr_seq', max(id)) FROM dbmail_headervalue;
77  
-SELECT setval('dbmail_subjectfield_idnr_seq', max(id)) FROM dbmail_subjectfield;
78  
-SELECT setval('dbmail_datefield_idnr_seq', max(id)) FROM dbmail_datefield;
79  
-SELECT setval('dbmail_referencesfield_idnr_seq', max(id)) FROM dbmail_referencesfield;;
80  
-SELECT setval('dbmail_fromfield_idnr_seq', max(id)) FROM dbmail_fromfield;
81  
-SELECT setval('dbmail_tofield_idnr_seq', max(id)) FROM dbmail_tofield;
82  
-SELECT setval('dbmail_replytofield_idnr_seq', max(id)) FROM dbmail_replytofield;
83  
-SELECT setval('dbmail_ccfield_idnr_seq', max(id)) FROM dbmail_ccfield;
84  
-SELECT setval('dbmail_sievescripts_idnr_seq', max(id)) FROM dbmail_sievescripts;
85  
-SELECT setval('dbmail_envelope_idnr_seq', max(id)) FROM dbmail_envelope;
  72
+SELECT setval('dbmail_alias_idnr_seq', max(alias_idnr)+1) FROM dbmail_aliases;
  73
+SELECT setval('dbmail_user_idnr_seq', max(user_idnr)+1) FROM dbmail_users;
  74
+SELECT setval('dbmail_mailbox_idnr_seq', max(mailbox_idnr)+1) FROM dbmail_mailboxes;
  75
+SELECT setval('dbmail_physmessage_id_seq', max(id)+1) FROM dbmail_physmessage;
  76
+SELECT setval('dbmail_message_idnr_seq', max(message_idnr)+1) FROM dbmail_messages;
  77
+SELECT setval('dbmail_seq_pbsp_id', max(idnr)+1) FROM dbmail_pbsp;
  78
+SELECT setval('dbmail_headername_id_seq', max(id)+1) FROM dbmail_headername;
  79
+SELECT setval('dbmail_headervalue_id_seq', max(id)+1) FROM dbmail_headervalue;
  80
+SELECT setval('dbmail_referencesfield_idnr_seq', max(id)+1) FROM dbmail_referencesfield;;
  81
+SELECT setval('dbmail_sievescripts_idnr_seq', max(id)+1) FROM dbmail_sievescripts;
  82
+SELECT setval('dbmail_envelope_idnr_seq', max(id)+1) FROM dbmail_envelope;
  83
+SELECT setval('dbmail_authlog_id_seq', max(id)+1) FROM dbmail_authlog;
  84
+SELECT setval('dbmail_filters_id_seq', max(id)+1) FROM dbmail_filters;
  85
+SELECT setval('dbmail_mimeparts_id_seq', max(id)+1) FROM dbmail_mimeparts;
86 86
 END;
87 87
 EOQ
88  
-	psql -q dbmail < $qfile
  88
+	psql -q $PGDB < $qfile >/dev/null 2>&1
  89
+	echo "done."
89 90
 	rm -f $qfile
90 91
 }
91 92
 
92 93
 pgsql_owner()
93 94
 {
94  
-	for t in `echo '\d'|psql dbmail|grep root|awk '{print $3}'`; do 
95  
-		echo "alter table $t owner to dbmail;"|psql -q dbmail
  95
+	echo -n "fix ownership ..."
  96
+	for t in `echo '\d'|psql $PGDB|grep public |awk '{print $3}'`; do 
  97
+		echo "alter table $t owner to dbmail;"|psql -q $PGDB >/dev/null 2>&1
96 98
 	done
  99
+	echo "done."
97 100
 }
98 101
 
99 102
 main()
100 103
 {
101  
-	install -d -m 7777 $TMPDIR || { echo "unable to access $TMPDIR"; exit 1; }
102  
-	#export_mysql || { echo "Export failed"; exit 1; }
103 104
 	init_pgsql
104 105
 	import_pgsql || { echo "Import failed"; exit 1; }
105  
-	pgsql_sequences
106  
-	pgsql_owner
  106
+	echo "Data migration complete. Now rebuild all caching tables using dbmail-util -by."
107 107
 }
108 108
 
109 109
 
34  contrib/sql2sql/mysql2pgsql.yml
... ...
@@ -0,0 +1,34 @@
  1
+
  2
+# if a socket is specified we will use that
  3
+# if tcp is chosen you can use compression
  4
+mysql:
  5
+ hostname: localhost
  6
+ port: 3306
  7
+ socket: /var/run/mysqld/mysqld.sock
  8
+ username: dbmail
  9
+ password: 
  10
+ database: dbmail
  11
+ compress: false
  12
+destination:
  13
+ # if file is given, output goes to file, else postgres
  14
+ file: 
  15
+ postgres:
  16
+  hostname: localhost
  17
+  port: 5432
  18
+  username: dbmail
  19
+  password: 
  20
+  database: dbmail
  21
+
  22
+# if tables is given, only the listed tables will be converted.  leave empty to convert all tables.
  23
+only_tables:
  24
+- dbmail_mimeparts
  25
+- dbmail_partlists
  26
+
  27
+# if supress_data is true, only the schema definition will be exported/migrated, and not the data
  28
+supress_data: false
  29
+
  30
+# if supress_ddl is true, only the data will be exported/imported, and not the schema
  31
+supress_ddl: true
  32
+
  33
+# if force_truncate is true, forces a table truncate before table loading
  34
+force_truncate: false

0 notes on commit 100396e

Please sign in to comment.
Something went wrong with that request. Please try again.