View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.argeo.maintenance.backup.vfs;
17  
18  import org.apache.commons.vfs2.FileObject;
19  
20  /** Backups a PostgreSQL database using pg_dump. */
21  public class PostgreSqlBackup extends OsCallBackup {
22  	/**
23  	 * PostgreSQL password environment variable (see
24  	 * http://stackoverflow.com/questions
25  	 * /2893954/how-to-pass-in-password-to-pg-dump)
26  	 */
27  	protected final static String PGPASSWORD = "PGPASSWORD";
28  
29  	private String pgDumpLocation = "/usr/bin/pg_dump";
30  
31  	private String dbUser;
32  	private String dbPassword;
33  	private String dbName;
34  
35  	public PostgreSqlBackup() {
36  		super();
37  	}
38  
39  	public PostgreSqlBackup(String dbUser, String dbPassword, String dbName) {
40  		this.dbUser = dbUser;
41  		this.dbPassword = dbPassword;
42  		this.dbName = dbName;
43  		init();
44  	}
45  
46  	@Override
47  	public void init() {
48  		// disable compression since pg_dump is used with -Fc option
49  		setCompression(null);
50  
51  		if (getName() == null)
52  			setName(dbName + ".pgdump");
53  		super.init();
54  	}
55  
56  	@Override
57  	public void writeBackup(FileObject targetFo) {
58  		if (getCommand() == null) {
59  			getEnvironment().put(PGPASSWORD, dbPassword);
60  			setCommand(pgDumpLocation + " -Fc" + " -U ${dbUser} ${dbName}");
61  		}
62  		getVariables().put("dbUser", dbUser);
63  		getVariables().put("dbPassword", dbPassword);
64  		getVariables().put("dbName", dbName);
65  
66  		super.writeBackup(targetFo);
67  	}
68  
69  	public void setDbUser(String dbUser) {
70  		this.dbUser = dbUser;
71  	}
72  
73  	public void setDbPassword(String dbPassword) {
74  		this.dbPassword = dbPassword;
75  	}
76  
77  	public void setDbName(String dbName) {
78  		this.dbName = dbName;
79  	}
80  
81  	public void setPgDumpLocation(String mysqldumpLocation) {
82  		this.pgDumpLocation = mysqldumpLocation;
83  	}
84  
85  }