IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

PostgreSQL Discussion :

Synchroniser 2 DB.


Sujet :

PostgreSQL

  1. #1
    Membre éclairé Avatar de Empty_body
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    681
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2004
    Messages : 681
    Par défaut Synchroniser 2 DB.
    Salut,
    Je suis à la recherche d'un outil de synchronisation postgres qui soit un freeware... Quelqu'un aurait qqch à me proposer???
    Merci.

  2. #2
    Membre éclairé Avatar de Empty_body
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    681
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2004
    Messages : 681
    Par défaut
    En fait, je me suis sans doute mal exprimé dans ma demande... Ce que je cherche, c'est un freeware qui me permette de sélectionner une table de ma db ou plusieurs ou toutes et d'obtenir un fichier contenant toutes les lignes nécessaires pour dumper la ou les tables sélectionnées...
    Merci.

  3. #3
    Membre éclairé Avatar de Empty_body
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    681
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2004
    Messages : 681
    Par défaut
    J'ai trouvé mon bonheur en le créant en java...
    Voici la source pour les curieux... (elle est pas encore top niveau finition mais elle fonctionne nickel...)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
     
    import java.io.File;
    import java.io.FileOutputStream;
    import java.sql.Connection;
    import java.sql.DatabaseMetaData;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Types;
    import java.util.HashMap;
    import java.util.Iterator;
     
    public class GenerateExportScript {
     
    	private String myDB;
    	private String myHost;
    	private String myTable;
     
    	private Connection myConnexion;
     
    	private String driversPost = "org.postgresql.Driver";
    	private String urlPost = "jdbc:postgresql://";
    	static private String baseDir;
    	static private File outputDirect;
     
    	StringBuffer outputWithData = new StringBuffer();
    	StringBuffer outputWithoutData = new StringBuffer();
    	StringBuffer outputWithDataImport = new StringBuffer();
    	StringBuffer outputWithoutDataImport = new StringBuffer();
     
    	public static void main(String[] args){
    		String myHost = "127.0.0.1";
     
    		baseDir = "C:\\Dump"+myHost+"\\";
    		outputDirect = new File(baseDir);
     
    		//String[] listTableDB = { "ma_DB","ma_Table" };
    		String[] listTableDB = { "Ma_DB","#all" };
     
    		new GenerateExportScript(myHost, listTableDB[0], listTableDB[1]);
     
    }
     
    	public GenerateExportScript(String myHost, String myDB, String myTable) {
    		this.myDB = myDB;
    		this.myHost = myHost;
    		this.myTable = myTable;
     
    		try {
     
    			Class.forName(driversPost);
    			myConnexion = DriverManager.getConnection(urlPost + myHost + ":5432/" + myDB, "User", "Pass");
    			System.out.println("Batch file body");
    			String host;
    			if ("127.0.0.1".equals(myHost)){
    				host = "";
    			}else{
    				host = "-h "+myHost;
    			}
    			File directory = null;
    			File directoryData = null;
    			directory = new File(outputDirect, myDB);
    			directory.mkdirs();
    			directoryData = new File(directory, "Data");
    			directoryData.mkdirs();
     
    			if(myTable == "#all"){
    				HashMap tablesFirst = getTables(myConnexion);
    				Iterator listTBF = tablesFirst.keySet().iterator();
    				while (listTBF.hasNext()) {
    					String table = (String) listTBF.next();
    					dumpCommand(table);
    				}
     
    			}else{
    				dumpCommand(myTable);
    			}
    			File batFileData = new File(directoryData, "Dump.bat");
    			FileOutputStream batFileDataOS = new FileOutputStream(batFileData);
    			System.out.println(outputWithData.toString());
    			batFileDataOS.write(outputWithData.toString().getBytes());
    			batFileDataOS.flush();
    			batFileDataOS.close();
    			//------
    			File batFileDataImport = new File(directoryData, "Import.bat");
    			FileOutputStream batFileDataOSImport = new FileOutputStream(batFileDataImport);
    			System.out.println(outputWithDataImport.toString());
    			batFileDataOSImport.write(outputWithDataImport.toString().getBytes());
    			batFileDataOSImport.flush();
    			batFileDataOSImport.close();
     
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
     
    	public void dumpCommand(String myTable){
    		this.myTable = myTable;
    		outputWithData.append("pg_dump -D -U arthur "+myHost+" -c -t"+myTable+" "+myDB+" > "+myTable+".sql");
    		outputWithData.append('\r');
    		outputWithData.append('\n');
    			}
     
    	private HashMap getTables(Connection conn) throws SQLException{
    		HashMap tables = new HashMap();
    		DatabaseMetaData dbMD = conn.getMetaData();
    		String[] types = { "TABLE", "" };
    		ResultSet res = dbMD.getTables("public", "", null, types);
    		while (res.next()) {
    			String tableName = res.getString("table_name");
    			HashMap fields = new HashMap();
     
    			ResultSet res2 = dbMD.getColumns("public", "", tableName, null);
    			while (res2.next()) {
    				String column = res2.getString("COLUMN_NAME");
    				int type = res2.getInt("DATA_TYPE");
    				int size = res2.getInt("COLUMN_SIZE");
    				int decimal = res2.getInt("DECIMAL_DIGITS");
    				String deff = res2.getString("COLUMN_DEF");
    				String comment = res2.getString("REMARKS");
    				String typeName = res2.getString("TYPE_NAME");
    				fields.put(column, new Field(type, size, decimal, typeName, deff, comment));
    			}
    			tables.put(tableName, fields);
    		}
    		return tables;
    	}
     
    	private class Field {
     
    		private int type;
    		private int size;
    		private int decimal;
    		private String typeName;
    		private String deff;
    		private String remark;
     
    		public int getSize(){
    			return size;
    		}
     
    		public int getType(){
    			return type;
    		}
     
    		public Field(int type, int size, int decimal, String typeName, String deff, String remark) {
    			this.size = size;
    			this.decimal = decimal;
    			this.type = type;
    			this.typeName = typeName;
    			this.deff = deff;
    		}
    		public String toString(){
    			return "type=" + type + ", size=" + size + ", decimal=" + decimal + ", Type Name=" + typeName + ", Default Value=" + deff;
    		}
    		public boolean equals(Object obj){
    			if (obj instanceof Field) {
    				Field cp = (Field) obj;
    				return (cp.getType() == type && cp.getSize() == size);
    			} else {
    				return false;
    			}
    		}
     
    		public String getDeff(){
    			return deff;
    		}
     
    		public String getTypeName(){
    			return typeName;
    		}
     
    		public int getDecimal(){
    			return decimal;
    		}
     
    		public String getRemark(){
    			return remark;
    		}
    	}
    }
    Si on veut un dump de toutes les tables, il suffit de choisir #all sinon, on commente cette ligne et on note le nom de la DB et on décommente la ligne adéquate...

  4. #4
    Membre émérite Avatar de Spoutnik
    Homme Profil pro
    Inscrit en
    Octobre 2003
    Messages
    672
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 672
    Par défaut
    Hello

    il y a un petit outil qui s'appele pg_dump et qui fait exactement ce que tu cherchais à faire ^^.
    Et en plus il est fournit en standart avec le paquet client il me semble


    ++

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. synchronisation de deux DBLookUPComboBox
    Par frede dans le forum Bases de données
    Réponses: 2
    Dernier message: 20/02/2004, 08h32
  2. Synchronisation de thread
    Par declencher dans le forum Langage
    Réponses: 2
    Dernier message: 07/01/2004, 10h28
  3. Probleme Synchronisation modem Sagem Fast 800
    Par -PiKo- dans le forum Matériel
    Réponses: 4
    Dernier message: 03/01/2004, 15h36
  4. Synchronisation verticale
    Par hs_dino dans le forum OpenGL
    Réponses: 2
    Dernier message: 28/09/2003, 09h35
  5. Synchronisation de base de données locale/distante Internet
    Par StefC30 dans le forum Développement
    Réponses: 3
    Dernier message: 25/07/2003, 14h47

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo