Commits

Colin Copeland  committed aa98aec

add destination directory command arg

  • Participants
  • Parent commits b40186a

Comments (0)

Files changed (1)

File postgresql_backup.py

 from optparse import OptionParser
 
 
-ROOT = '/Users/copelco/Desktop/backup/'
 DATABASES_TO_IGNORE = ('template0', )
 logging.basicConfig(stream=sys.stdout,
                     level=logging.DEBUG)
 parser = OptionParser(usage=usage)
 parser.add_option("-c", "--compression", default="gzip",
                   help="compression algorithm (gzip, bzip2, etc.)")
+parser.add_option("-d", "--dest", default=os.getcwd(),
+                  help="destination directory, defaults to cwd")
 
 
 class Backup(object):
     """ Base backup class to handle common remote commands """
 
-    def __init__(self, host, sudo='', compression='gzip'):
+    def __init__(self, host, dest, sudo='', compression='gzip'):
         self.host = host
         self.sudo_user = sudo
         self.compression = compression
+        self.dest = dest
 
     def execute(self, cmd, comm=True, **kwargs):
         if 'stderr' not in kwargs:
 
     def backup_postgres_globals(self):
         filename = 'globals.{0}'.format(self.compression)
-        path = os.path.join(ROOT, filename)
+        path = os.path.join(self.dest, filename)
         fh = open(path, 'w+')
         cmd = "pg_dumpall --globals-only | {0}".format(self.compression)
         self.remote(cmd, stdout=fh)
 
     def backup_postgres_database(self, database):
         filename = '{0}.{1}'.format(database, self.compression)
-        path = os.path.join(ROOT, filename)
+        path = os.path.join(self.dest, filename)
         cmd = "pg_dump -i {0} | {1}".format(database, self.compression)
         fh = open(path, 'w+')
         self.remote(cmd, stdout=fh)
     
     def backup_all_databases(self):
         filename = 'mysqldumpall.sql.{0}'.format(self.compression)
-        path = os.path.join(ROOT, filename)
+        path = os.path.join(self.dest, filename)
         cmd = "mysqldump -uroot --all-databases | {0}".format(self.compression)
         fh = open(path, 'w+')
         self.remote(cmd, stdout=fh)
         parser.print_usage()
         return -1
     for host in hosts:
-        pg = PostgreSQL(host=host, compression=options.compression)
+        pg = PostgreSQL(host=host, compression=options.compression,
+                        dest=options.dest)
         pg.run()