{"id":256,"date":"2012-02-10T12:35:20","date_gmt":"2012-02-10T11:35:20","guid":{"rendered":"http:\/\/elkano.org\/blog\/?p=256"},"modified":"2016-03-16T10:07:47","modified_gmt":"2016-03-16T09:07:47","slug":"bacula-migrate-database-from-mysql-to-postgresql","status":"publish","type":"post","link":"https:\/\/elkano.org\/blog\/bacula-migrate-database-from-mysql-to-postgresql\/","title":{"rendered":"Bacula: migrate database from mysql to postgresql"},"content":{"rendered":"<p>I&#8217;ve followed these steps to migrate bacula database from mysql 5.1.49 to postgresql 8.4.9. I am running bacula 5.2.3 compiled from sources on Debian Squeeze.<\/p>\n<p>1- Install postgresql database:<\/p>\n<p>\n<code><br \/>\n# apt-get install postgresql-8.4 postgresql-server-dev-8.4 postgresql-client-8.4<br \/>\n<\/code>\n<\/p>\n<p>2- Compile and install bacula with postgresql support:<\/p>\n<p>\n<code># cd \/usr\/src\/bacula-5.2.5<br \/>\n# .\/configure --with-postgresql --with-openssl --with-python<br \/>\n# make install<\/code>\n<\/p>\n<p>3- Create bacula user on postgresql:<\/p>\n<p>\n<code># su - postgres<br \/>\n# psql -d template1 -U postgres<br \/>\ntemplate1=# CREATE USER bacula WITH PASSWORD 'myPassword';<br \/>\n<\/code>\n<\/p>\n<p>4- Create bacula database<\/p>\n<p><code># sh \/etc\/bacula\/create_postgresql_database<\/code><\/p>\n<p>5- Create database tables<\/p>\n<p>#<code> sh \/etc\/bacula\/make_postgresql_tables<\/code><\/p>\n<p>6- Grant privilejes on bacula database to bacula user.<\/p>\n<p>#<code> sh \/etc\/bacula\/grant_postgresql_privileges<\/code><\/p>\n<p>7- take dump file from mysql. I&#8217;ve used this script to take the mysql dump and change some fields, like zero timestamp that does not fit with postgresql. <\/p>\n<pre class=\"brush: perl; gutter: true; first-line: 1; highlight: []; html-script: false\">\r\n\r\n#!\/bin\/perl -w\r\n\r\nmy $line=undef;\r\nmy $catalog=&quot;bacula&quot;;\r\n\r\n\r\nsystem (&quot;mysqldump --single-transaction --compatible=postgresql --compact --no-create-info $catalog &gt; $catalog.sql&quot;);\r\nprint &quot;$catalog dump completed!\\n&quot;;\r\n  \r\nopen FD_CAT, &quot;&lt;$catalog.sql&quot;;\r\nopen FD_OUT, &quot;&gt;out-$catalog.sql&quot;;\r\nwhile (&lt;FD_CAT&gt;)\r\n{\r\n if (m\/^(INSERT\\s+INTO\\s+\\&quot;)(\\w+)(\\&quot;\\s+.+)$\/){          \r\n   $line=$1.lc($2).$3;\r\n   $line=~s\/\\((\\d+)\\,\\&#039;\/\\($1\\,e\\&#039;\/g;    \r\n   $line=~ s\/\\\\\\&#039;\/\\&#039;\\&#039;\/g;\r\n   $line=~ s\/\\&#039;0000-00-00 00:00:00\\&#039;\/to_timestamp\\(0\\)\/g;\r\n   print FD_OUT &quot;$line\\n&quot;;\r\n }else{\r\n   print &quot;line not found: $_&quot;;\r\n } \r\n\r\n}\r\nclose (FD_CAT);\r\nclose (FD_OUT);\r\nprint &quot;Dump filtered commpleted for $catalog\\n&quot;;\r\n\r\n\r\n<\/pre>\n<p>8- Import dump file into postgresql<\/p>\n<p>\n<code># psql -d bacula -f out-bacula.sql<\/code>\n<\/p>\n<p>9- Change the catalog definition in bacula-dir.conf from mysql to postgresql<\/p>\n<p>Edit \/etc\/bacula-dir.conf and change driver and port<\/p>\n<p>\n<code>dbdriver = \"dbi:postgresql\"; dbaddress = 127.0.0.1; dbport =5432<\/code>\n<\/p>\n<p>10- Restart bacula director.<\/p>\n<p>\n<code># \/etc\/init.d\/bacula-dir restart<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve followed these steps to migrate bacula database from mysql 5.1.49 to postgresql 8.4.9. I am running bacula 5.2.3 compiled from sources on Debian Squeeze. 1- Install postgresql database: # apt-get install postgresql-8.4 postgresql-server-dev-8.4 postgresql-client-8.4 2- Compile and install bacula with postgresql support: # cd \/usr\/src\/bacula-5.2.5 # .\/configure &#8211;with-postgresql &#8211;with-openssl &#8211;with-python # make install 3- [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[146,38,10,37],"_links":{"self":[{"href":"https:\/\/elkano.org\/blog\/wp-json\/wp\/v2\/posts\/256"}],"collection":[{"href":"https:\/\/elkano.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/elkano.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/elkano.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/elkano.org\/blog\/wp-json\/wp\/v2\/comments?post=256"}],"version-history":[{"count":45,"href":"https:\/\/elkano.org\/blog\/wp-json\/wp\/v2\/posts\/256\/revisions"}],"predecessor-version":[{"id":1262,"href":"https:\/\/elkano.org\/blog\/wp-json\/wp\/v2\/posts\/256\/revisions\/1262"}],"wp:attachment":[{"href":"https:\/\/elkano.org\/blog\/wp-json\/wp\/v2\/media?parent=256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/elkano.org\/blog\/wp-json\/wp\/v2\/categories?post=256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/elkano.org\/blog\/wp-json\/wp\/v2\/tags?post=256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}