Action: Add config accessor functions.
[matthijs/projects/backupninja.git] / src / backupninja
index b3c26f4343f5e80daf437e41ff0157329dfe5e69..019dfbdb48cd0489afb793a672d232b75bba1dd0 100755 (executable)
 """ Check for scheduled backupninja actions and run them when needed """
 
 import optparse
 """ Check for scheduled backupninja actions and run them when needed """
 
 import optparse
+import logging
 import sys
 
 import sys
 
+from backupninja.log import setup_logging
+from backupninja import config
+from backupninja import action
+
+log = logging.getLogger()
+
 def make_option_parser():
     description = """%prog checks for scheduled actions and runs them when needed."""
     parser = optparse.OptionParser(description=description)
 def make_option_parser():
     description = """%prog checks for scheduled actions and runs them when needed."""
     parser = optparse.OptionParser(description=description)
-
+    parser.add_option( "-c", "--confdir"
+                     , help="Load configuration from CONFIG_DIR "
+                            "instead of '%default'. When "
+                            "the --conffile or --actionsdir options "
+                            "specify relative paths, they are taken "
+                            "relative to this directory."
+                     , default="/etc/backupninja"
+                     , dest="config_dir"
+                     , metavar="CONFIG_DIR")
+    parser.add_option( "-f", "--conffile"
+                     , help="Load the main configuration from FILE instead "
+                            "of '%default'. If FILE is a relative path, it "
+                            "is interpreted relative to CONFIG_DIR."
+                     , default='backupninja.conf'
+                     , dest="global_config"
+                     , metavar="FILE")
+    parser.add_option( "-a", "--actionsdir"
+                     , help="Load action configurations from ACTIONS_DIR "
+                            "instead of '%default'. If ACTIONS_DIR is a "
+                            "relative path, it is interpreted relative to "
+                            "CONFIG_DIR."
+                     , default="actions"
+                     , dest="actions_dir"
+                     , metavar="ACTIONS_DIR")
+    parser.add_option( "-d", "--debug"
+                     , help="Run in debug mode. This only means logging is "
+                            "more verbose."
+                     , action="store_true"
+                     , dest="debug")
+    parser.add_option( "-t", "--test"
+                     , help="Run in testing mode. This means no actions "
+                            "are taken, only show what would be done "
+                            "(as far as possible without taking "
+                            "actions, of course)."
+                     , action="store_true"
+                     , dest="test")
     return parser
  
 def main(argv):
     return parser
  
 def main(argv):
@@ -37,9 +79,19 @@ def main(argv):
     parser = make_option_parser()
     (options, args) = parser.parse_args(argv)
 
     parser = make_option_parser()
     (options, args) = parser.parse_args(argv)
 
+    # Setup logging
+    setup_logging(options)
+
     # Load config file
     # Load config file
+    global_conf = config.get_global_config(options)
+    if global_conf is None:
+        # Error was already logged
+        return 1
+
     # Process command
     # Process command
-    parser.print_help()
+    action.run_all_actions(options, global_conf)
+
+    return 0
 
 if __name__ == '__main__':
     sys.exit(main(sys.argv))
 
 if __name__ == '__main__':
     sys.exit(main(sys.argv))