summaryrefslogtreecommitdiffstats
path: root/src/lib/Server/Admin/Backup.py
blob: f150b9cf028f2de44bcfa6d3102a8e19a87e098c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import glob
import os
import sys
import time
import tarfile
import Bcfg2.Server.Admin
import Bcfg2.Options

class Backup(Bcfg2.Server.Admin.MetadataCore):
    __shorthelp__ = "Make a backup of the Bcfg2 repository."
    __longhelp__ = (__shorthelp__ + "\n\nbcfg2-admin backup start"
                                    "\n\nbcfg2-admin backup restore")
    __usage__ = ("bcfg2-admin backup [start|restore]")

    def __init__(self, configfile):
        Bcfg2.Server.Admin.MetadataCore.__init__(self, configfile,
                                                 self.__usage__)

    def __call__(self, args):
        Bcfg2.Server.Admin.MetadataCore.__call__(self, args)
        # Get Bcfg2 repo directory
        opts = {'repo': Bcfg2.Options.SERVER_REPOSITORY}
        setup = Bcfg2.Options.OptionParser(opts)
        setup.parse(sys.argv[1:])
        self.datastore = setup['repo']
        
	if len(args) == 0:
            self.errExit("No argument specified.\n"
                         "Please see bcfg2-admin backup help for usage.")
        if args[0] == 'start':
            timestamp = time.strftime('%Y%m%d%H%M%S')
            format = 'gz'
            mode = 'w:' + format
            filename = timestamp + '.tar' + '.' + format
            out = tarfile.open(self.datastore + '/' + filename, mode=mode)
            content = os.listdir(self.datastore)    
            for item in content:
                out.add(item)
            out.close()
            print "Archive %s was stored.\nLocation: %s" % (filename, self.datastore)

        elif args[0] == 'restore':
            print 'Not implemented yet'
        
        else:
            print "No command specified"
            raise SystemExit(1)