def __init__(self, name, config, target_directory, dry_run=False): """Create a new PgDump instance :param name: unique name of this backup (e.g. pg_dump/20100101_000000) :param target_directory: where backup files should be stored :param dry_run: boolean flag indicating whether we should only go through the motions of a backup without actually performing the heavy weight steps. """ self.name = name self.config = config self.target_directory = target_directory self.dry_run = dry_run self.config.validate_config(CONFIGSPEC) self.connection = get_connection(self.config) self.databases = pg_databases(self.config, self.connection) LOG.info("Found databases: %s", ','.join(self.databases))
def backup(self): """ Start a backup. """ if self.dry_run: # Very simply dry run information # enough to know that: # 1) We can connect to Postgres using pgpass data # 2) The exact databases we would dump for name in pg_databases(self.config): LOG.info('pg_dump -Fc %s', name) return # First run a pg_dumpall -g and save the globals # Then run a pg_dump for each database we find backup_dir = os.path.join(self.target_directory, 'data') # put everything in data/ try: os.mkdir(backup_dir) except OSError, exc: raise PgError("Failed to create backup directory %s" % backup_dir)