Commits

Colin Copeland committed aa98aec

add destination directory command arg

Comments (0)

Files changed (1)

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()
 
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.