"""postgresql backup plugin for backup system. Data dumps are performed using 'pg_dump' The following settings are read from the 'options' heading in the backup.cfg file: pg_user : username to use for pg_dump pg_password : password for that user By default, the options -d -C -E utf8 are used. """ import plugins, os PIPE = plugins.subprocess.PIPE Popen = plugins.subprocess.Popen config = False databases = [] BACKUP_PATH = '/var/backup/db/pgsql/' def init(cfg, *args, **kwargs): global config, databases config = cfg databases = args print "config: %s, dbs: %s" % (config, databases) def pre(): """Prepare a copy of the postgres database using pg_dump.""" # pg_dump -d -C -E utf8 django > django.psql # -E : encoding # -C : CREATE commands # -d : INSERT instead of COPY for db in databases: mkdir = "mkdir -p /tmp%s" % (BACKUP_PATH) dumpfile = "/tmp%s%s.psql" % (BACKUP_PATH, db) cmd = ['pg_dump', '-d', '-C', '-E', 'utf8', db] plugins.print_exec(mkdir) dumpfileObj = open(dumpfile, 'w') env = None opts = config.get_section("options") if hasattr(opts, "pg_password"): env={"PGPASSWORD": opts.pg_password} if hasattr(opts, "pg_user"): cmd.insert(1, '-U') cmd.insert(2, opts.pg_user) print ' '.join(cmd) + '(%r)' % env process = Popen(cmd, stdout=dumpfileObj, env=env).wait() def run(): """Backup all of the trac copies in /tmp. Read the rsync manpage in the 'relative' section to see why, but we want to run rsync with the source /tmp/./path/to/trac""" rsync = config.rsync() dest = config.destination() # we want rsync + source + dest def source(db): return '/tmp/.%s%s.psql ' % (BACKUP_PATH, db) for db in databases: c = rsync + source(db) + dest print 'rs>', c plugins.print_exec(c) def post(): """Remove the copies we prepared in the pre() step.""" for db in databases: rm = "rm -f /tmp%s%s.psql" % (BACKUP_PATH, db) plugins.print_exec(rm) rmdir = "rm -rf /tmp/var" plugins.print_exec(rmdir)