How to dump and restore Postgresql databases

Only dump

pg_dump –host HOST_1  –port 5432 –username SOURCE_db_username –schema SCHEMA_DO_BE_BACKED_UP –password –format custom –no-owner -x –no-privileges –blobs –verbose –file SCHEMA_BACKUP_DUMP_FILE.backup SOURCE_DB_NAME

Only Restore

Notice:
Target database must exist.

pg_restore –host HOST_2 –port 5432 –username TARGET_db_username –dbname TARGET_DB_NAME –password –no-owner -x –no-privileges –jobs 2 –verbose SCHEMA_BACKUP_DUMP_FILE.backup

Dump and restore with data streaming

env PGPASSWORD=source_db_user_pass –host=HOST_1 –port=5432 –schema SCHEMA_DO_BE_BACKED_UP –username=”source_db_username” –format=custom –no-owner -x –no-privileges source_db_name | env PGPASSWORD=target_db_user_pass pg_restore –host=HOST_2 –no-privileges –no-owner –no-tablespaces –verbose –username=target_db_username –dbname=target_db_name –clean

sed power removing spaces and new lines from text

Imaginge input file:

– dev-env1-service-202.domain.com
– dev-env1-service-203.domain.com
– dev-env1-service-204.domain.com
– dev-env1-service-205.domain.com
– dev-env1-service-206.domain.com
– dev-env1-service-207.domain.com
– dev-env1-service-208.domain.com
– dev-env1-service-209.domain.com
– dev-env1-service-210.domain.com

and you would like to have it in form:

dev-env1-service-202.domain.com dev-env1-service-203.domain.com dev-env1-service-204.domain.com dev-env1-service-205.domain.com dev-env1-service-206.domain.com dev-env1-service-207.domain.com dev-env1-service-208.domain.com dev-env1-service-209.domain.com dev-env1-service-210.domain.com

Just run commands:

cat file.txt |sed -e s/’ – ‘/”/g|sed -e s/’ ‘/”/g| tr ‘\n’ ‘ ‘

 

 

Parsing XML to Map

package com.open.jacekkowalczyk;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
 * Xml2Map!
 */
public class Xml2Map {
    private static Logger LOGGER = Logger.getLogger(Xml2Map.class);
    public List<Map<String, String>> getListOfLevel1MapElelements(final File file, final String rootElemName,
        final String firstLevelElemName) {
        final List<Map<String, String>> elements = new ArrayList<Map<String, String>>();
        try {
            final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db;
            db = dbf.newDocumentBuilder();
            final Document doc = db.parse(file);
            doc.getDocumentElement().normalize();
            // System.out.println(“Root element ” + doc.getDocumentElement().getNodeName());
            final NodeList rootNodeList = doc.getElementsByTagName(rootElemName);
            LOGGER.debug(“all elements”);
            for (int i = 0; i < rootNodeList.getLength(); i++) {
                final Node rootXmlNode = rootNodeList.item(i);
                LOGGER.debug(“root: ” + rootXmlNode.getNodeName());
                if (rootXmlNode.getNodeType() == Node.ELEMENT_NODE) {
                    final Element rootXmlElem = (Element)rootXmlNode;
                    final NodeList level1Nodes = rootXmlElem.getElementsByTagName(firstLevelElemName);
                    for (int j = 0; j < level1Nodes.getLength(); j++) {
                        final Node level1Node = level1Nodes.item(j);
                        LOGGER.debug(“1stlevel:    ” + level1Node.getNodeName());
                        if (level1Node.getNodeType() == Node.ELEMENT_NODE) {
                            final Map<String, String> level1XmlElementsMap = new HashMap<String, String>();
                            final Element level1NodeElem = (Element)level1Node;
                            final NodeList level2Nodes = level1NodeElem.getChildNodes();
                            for (int k = 0; k < level2Nodes.getLength(); k++) {
                                final Node level2Node = level2Nodes.item(k);
                                // System.out.println(“2ndlevelNode:    ” + level2Node.getNodeName());
                                if (level2Node.getNodeType() == Node.ELEMENT_NODE) {
                                    final Element level2NodeElem = (Element)level2Node;
                                    LOGGER.debug(“2ndlevelElem:    ” + level2NodeElem.getNodeName() + “: “
                                        + level2NodeElem.getTextContent());
                                    level1XmlElementsMap.put(level2NodeElem.getNodeName(), level2NodeElem.getTextContent());
                                }
                            }
                            elements.add(level1XmlElementsMap);
                        }
                    }
                }
            }
            return elements;
        } catch (final ParserConfigurationException e) {
            e.printStackTrace();
            return elements;
        } catch (final SAXException e) {
            e.printStackTrace();
            return elements;
        } catch (final IOException e) {
            e.printStackTrace();
            return elements;
        }
    }
}

creating issues in bugzilla from java

final HttpClient httpClient = new HttpClient();
final XmlRpcClient rpcClient = new XmlRpcClient();
final XmlRpcCommonsTransportFactory factory = new XmlRpcCommonsTransportFactory(rpcClient);
final XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();

factory.setHttpClient(httpClient);
rpcClient.setTransportFactory(factory);
config.setServerURL(new URL(“http://192.168.12.13/xmlrpc.cgi&#8221;));
rpcClient.setConfig(config);

// map of the login data
final Map<String, String> loginMap = new HashMap<String, String>();

loginMap.put(“login”, userName);

loginMap.put(“password”, password);
loginMap.put(“rememberlogin”, “Bugzilla_remember”);

// login to bugzilla
final Object loginResult = rpcClient.execute(“User.login”, new Object[] {loginMap});
System.out.println(“loginResult=” + loginResult);

final Map issueParamsMap = new HashMap<String, String>();
issueParamsMap.put(“product”, “TestProduct”);
issueParamsMap.put(“component”, “testComponent”);
issueParamsMap.put(“summary”, “Bug created from java xmlrpc”);
issueParamsMap.put(“description”, “This is text including stacktrace”);
issueParamsMap.put(“version”, “unspecified”);
issueParamsMap.put(“op_sys”, “Linux”);
issueParamsMap.put(“platform”, “PC”);
issueParamsMap.put(“priority”, “Highest”);
issueParamsMap.put(“severity”, “Normal”);
issueParamsMap.put(“status”, “NEW”);
issueParamsMap.put(“estimated_time”, “2”);

final Object createResult = rpcClient.execute(“Bug.create”, new Object[] {issueParamsMap});

System.out.println(“createResult = ” + createResult);

pom dependencies and plugins:

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.apache.xmlrpc</groupId>
<artifactId>xmlrpc-client</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<!– com/open/jacekkowalczyk/xml-to-map/1.0-SNAPSHOT –>
<groupId>com.open.jacekkowalczyk</groupId>
<artifactId>xml-to-map</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.tomtom.cpu.coredb.xmlrpc.bugzilla.BugzillaIssuesCreator</mainClass>
</manifest>
<manifestEntries>
<buildVersion>${project.version}</buildVersion>
<SCM-Revision>${buildNumber}</SCM-Revision>
<SCM-Timestamp>${timestamp}</SCM-Timestamp>
</manifestEntries>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

</plugins>
</build>