Permalink
Browse files

Now the restores are saved to the backup dir.

  • Loading branch information...
dpetzold committed Feb 25, 2012
1 parent 7d0fa64 commit 47a2ea3a33f3902d5e8615c9592d2b3d4a16e7a6
@@ -26,7 +26,7 @@ def __init__(self, router, container_name, verbose=False):
self.mysql = MysqlCommand(router, verbose)
self.rackspace = RackspaceCommand(container_name, verbose)
self.verbose = verbose
- self.start_time = datetime.datetime.now()
+ self.start_time = datetime.datetime.utcnow()
if not os.path.exists(settings.BACKUP_DIR):
os.mkdir(settings.BACKUP_DIR)
@@ -109,9 +109,12 @@ def restore(self, filename=None, remote=None):
self.mysql.drop()
self.mysql.create()
if remote is not None:
- filename = self.rackspace.get(remote)
+ filename = self.rackspace.get(remote, settings.BACKUP_DIR)
return self.mysql.source(filename)
+ def list(self):
+ for obj in self.rackspace.list():
+ print('%s %s' % (obj.name, obj.size))
class Command(BaseCommand):
@@ -129,12 +132,15 @@ class Command(BaseCommand):
make_option('-F', '--file', action='store',
dest='file', default=None,
help='Specify a file.'),
+ make_option('-L', '--list', action='store_true',
+ dest='list', default=False,
+ help='List the avalible backups.'),
make_option('-R', '--remote', action='store',
dest='remote', default=None,
help='Use the specified remote file.'),
make_option('--restore', action='store_true',
dest='restore', default=False,
- help='Restore the db from specified local or remote file.'),
+ help='Restore (drop, create, source) the db from specified local or remote file.'),
make_option('--router', action='store',
dest='router', default='default',
help='Use this router and not the default.'),
@@ -163,6 +169,8 @@ def handle(self, *args, **options):
backup = backup.cull()
elif options.get('db'):
backup = backup.backup_database()
+ elif options.get('list'):
+ backup = backup.list()
elif options.get('restore'):
if options.get('file'):
dbfile = backup.restore(filename=options.get('file'))
@@ -74,14 +74,13 @@ def source(self, dbfile):
os.system(cmd)
def dump(self, prefix='db'):
- appname = os.path.split(settings.SITE_ROOT)[1]
backup_dir = '%s/%s' % (settings.BACKUP_DIR, prefix)
if not os.path.exists(backup_dir):
os.mkdir(backup_dir)
dbfile = '{backup_dir}/{database}.{date}-{hostname}.gz'.format(
backup_dir=backup_dir,
database=self.db['NAME'],
- date=datetime.datetime.now().strftime('%Y%m%d_%H%M%S'),
+ date=datetime.datetime.utcnow().strftime('%Y%m%d_%H%M%S'),
hostname=socket.gethostname())
cmd = '{mysqldump} {database} | gzip > {db_file}'.format(
mysqldump=self.mysqldump,
@@ -69,13 +69,15 @@ def upload(self, filepath, prefix=None):
return obj
def list(self, pattern=None):
+ objs = []
for obj in self.container.get_objects():
if pattern is not None:
search = re.search(pattern, obj.name)
if search is not None:
- print('%s %s' % (obj.name, obj.size))
+ objs.append(obj)
else:
- print('%s %s' % (obj.name, obj.size))
+ objs.append(obj)
+ return objs
def delete(self, filename=None, pattern=None):
if pattern is not None:
@@ -89,10 +91,17 @@ def delete(self, filename=None, pattern=None):
else:
return self.container.delete_object(filename)
- def get(self, objname):
+ def get(self, objname, savedir=None):
root, filename = os.path.split(objname)
obj = self.container.get_object(objname)
- obj.save_to_filename(filename)
+ if savedir is not None:
+ dstdir = '%s/%s' % (savedir, root)
+ if not os.path.exists(dstdir):
+ os.system('mkdir -p "%s"' % (destdir))
+ filename = '%s/%s' % (dstdir, filename)
+ obj.save_to_filename(filename)
+ else:
+ obj.save_to_filename(filename)
return filename
class Command(BaseCommand):
@@ -157,7 +166,8 @@ def handle(self, *args, **options):
else:
print('Must specify a pattern or a file')
elif options.get('list'):
- rackspace.list(options.get('pattern'))
+ for obj in rackspace.list(options.get('pattern')):
+ print('%s %s' % (obj.name, obj.size))
elif options.get('get'):
filename = rackspace.get(options.get('get'))
print('Downloaded %s' % (filename))

0 comments on commit 47a2ea3

Please sign in to comment.