scripts/pvlbackup-rsync-snapshot
changeset 22 a2bc5cc9de4d
parent 21 de69e9ba8f22
child 30 29b60df79122
equal deleted inserted replaced
21:de69e9ba8f22 22:a2bc5cc9de4d
   144         log.debug("section: %s", section_name)
   144         log.debug("section: %s", section_name)
   145 
   145 
   146         # subsections
   146         # subsections
   147         section_path = section_name.split(':')
   147         section_path = section_name.split(':')
   148 
   148 
   149         # lookup dict
   149         # lookup section dict from config
   150         lookup = config
   150         lookup = config
   151 
   151 
       
   152         # XXX: sections are not in order, so we can't rely on the parent section being created before we handle the sub-section
   152         for name in section_path :
   153         for name in section_path :
       
   154             # possibly create
   153             if name not in lookup :
   155             if name not in lookup :
   154                 lookup[name] = {}
   156                 lookup[name] = {}
   155             
   157 
   156             lookup = lookup[name]
   158             lookup = lookup[name]
   157 
   159  
   158         # found dict for this section
   160         # found dict for this section
   159         config_section = lookup
   161         config_section = lookup
   160 
   162 
   161         # values
   163         # values
   162         for name, value in config_file.items(section) :
   164         for name, value in config_file.items(section) :
   375         temp_path = os.path.join(self.snapshots_dir, 'tmp')
   377         temp_path = os.path.join(self.snapshots_dir, 'tmp')
   376 
   378 
   377         if os.path.exists(temp_path) :
   379         if os.path.exists(temp_path) :
   378             raise Exception("Old temp snapshot dir remains, please clean up: {path}".format(path=temp_path))
   380             raise Exception("Old temp snapshot dir remains, please clean up: {path}".format(path=temp_path))
   379 
   381 
   380         log.info("Perform main snapshot: %s", snapshot_path)
   382         log.info("Perform main snapshot: %s -> %s", self.source, snapshot_path)
   381 
   383 
   382         # build rsync options
   384         # build rsync options
   383         opts = dict(self.rsync_options)
   385         opts = dict(self.rsync_options)
   384 
   386 
   385         if os.path.exists(self.current_path) :
   387         if os.path.exists(self.current_path) :
   637         targets         = {},
   639         targets         = {},
   638     )
   640     )
   639 
   641 
   640     if options.config :
   642     if options.config :
   641         # load
   643         # load
   642         config = parse_config(options.config, config)
   644         try :
       
   645             config = parse_config(options.config, config)
       
   646         except ConfigError as e:
       
   647             log.error("Configuration error: %s: %s", options.config, e)
       
   648             return 2
   643  
   649  
   644     # manual?
   650     # manual?
   645     if options.target :
   651     if options.target :
   646         config['targets'][options.target] = dict(
   652         config['targets'][options.target] = dict(
   647             path        = options.target,
   653             path        = options.target,
   670         log.debug("rsync option: %s=%s", option, value)
   676         log.debug("rsync option: %s=%s", option, value)
   671 
   677 
   672         # store
   678         # store
   673         options.rsync_options[option] = value
   679         options.rsync_options[option] = value
   674 
   680 
   675     # all targets?
   681     # what targets?
   676     if not targets :
   682     if not targets :
       
   683         # default to all defined targets
   677         targets = list(config['targets'])
   684         targets = list(config['targets'])
       
   685     
       
   686     else :
       
   687         # given ones, but verify they exist
       
   688         for target in targets :
       
   689             if target not in config['targets'] :
       
   690                 log.error("Unknown target given: %s", target)
       
   691                 log.info("Defined targets: %s", ' '.join(config['targets']))
       
   692 
       
   693                 return 2
   678 
   694 
   679     # targets
   695     # targets
   680     for name in targets :
   696     for name in targets :
   681         target_config = config['targets'][name]
   697         target_config = config['targets'][name]
   682 
   698 
   685 
   701 
   686         log.info("Config target: %s", name)
   702         log.info("Config target: %s", name)
   687 
   703 
   688         # run
   704         # run
   689         target.run(options)
   705         target.run(options)
       
   706 
       
   707     # ok
       
   708     return 0
   690 
   709 
   691 def config_defaults () :
   710 def config_defaults () :
   692     return dict(
   711     return dict(
   693         # snapshots/ naming
   712         # snapshots/ naming
   694         snapshot_format = '%Y%m%d-%H%M%S',
   713         snapshot_format = '%Y%m%d-%H%M%S',