- Timestamp:
- Oct 18, 2019, 10:35:56 AM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ipp-259_genericise_backups/tools/backups/backup.py
r40945 r40949 10 10 import backups.utils.errors as errs 11 11 import backups.utils.subprocess_utils as sub_utils 12 from backups.utils.config_parse_helper import ConfigSchema Section, ConfigSchemaLine12 from backups.utils.config_parse_helper import ConfigSchema, ConfigSchemaSection, ConfigSchemaLine, ParsedConfig 13 13 14 14 DEFAULT_CONFIG_FILE = path.join(path.dirname(__file__), 'atlassian_backups.config') 15 16 DEFAULT_SCHEMA_SECTION = ConfigSchemaSection('DEFAULT', 17 [ ConfigSchemaLine('backup_paths', True, list, cfg_help.SpecialSchemaProcessing.commma_separated) 18 , ConfigSchemaLine('source_machine', True, str) 19 , ConfigSchemaLine('verbose', False, bool) 20 , ConfigSchemaLine('make_dirs', False, bool) 21 ] 22 ) 23 COPY_SCHEMA_SECTION = ConfigSchemaSection('COPY', 24 [ ConfigSchemaLine('sources', True, list, cfg_help.SpecialSchemaProcessing.commma_separated) 25 , ConfigSchemaLine('additional_args', False, list, cfg_help.SpecialSchemaProcessing.space_separated) 26 ] 27 ) 28 TAR_SCHEMA_SECTION = ConfigSchemaSection('TAR', 29 [ ConfigSchemaLine('sources', True, list, cfg_help.SpecialSchemaProcessing.commma_separated) 30 , ConfigSchemaLine('additional_args', False, list, cfg_help.SpecialSchemaProcessing.space_separated) 31 , ConfigSchemaLine('target_filename', True, str) 32 , ConfigSchemaLine('prefix_date', True, bool) 33 ] 34 ) 35 RSYNC_SCHEMA_SECTION = ConfigSchemaSection('RSYNC', 36 [ ConfigSchemaLine('sources', True, list, cfg_help.SpecialSchemaProcessing.commma_separated) 37 , ConfigSchemaLine('additional_args', False, list, cfg_help.SpecialSchemaProcessing.space_separated) 38 ] 39 ) 40 41 MYSQLDUMP_SCHEMA_SECTION = ConfigSchemaSection('MYSQLDUMP', 42 [ ConfigSchemaLine('user', True, str) 43 , ConfigSchemaLine('password', True, str) 44 , ConfigSchemaLine('db_name', True, str) 45 , ConfigSchemaLine('additional_args', False, list, cfg_help.SpecialSchemaProcessing.space_separated) 46 , ConfigSchemaLine('target_filename', True, str) 47 , ConfigSchemaLine('prefix_date', True, bool) 48 ] 49 ) 50 51 52 def get_backup_schema() -> ConfigSchema: 53 backup_schema = ConfigSchema(None) 54 backup_schema.add_section(DEFAULT_SCHEMA_SECTION) 55 backup_schema.add_section(COPY_SCHEMA_SECTION) 56 backup_schema.add_section(TAR_SCHEMA_SECTION) 57 backup_schema.add_section(RSYNC_SCHEMA_SECTION) 58 backup_schema.add_section(MYSQLDUMP_SCHEMA_SECTION) 59 return backup_schema 15 60 16 61 … … 20 65 """ 21 66 22 def __init__(self, config_file=None ):67 def __init__(self, config_file=None, schema: ConfigSchema=None): 23 68 if config_file is None: 24 69 raise errs.ValidationError("ValidationError: Backup class must be given a config") 25 self.load_config(config_file) 26 27 def load_config(self, config_class_or_filepath): 70 if schema is None: 71 schema = get_backup_schema() 72 print(schema) 73 self.load_config(config_file, schema) 74 75 def load_config(self, config_class_or_filepath, schema): 28 76 """Accepts a filepath to a config file, or """ 29 77 … … 38 86 config.read(config_class_or_filepath) 39 87 40 self.read_default_section(config) 41 if self._has_copy_section(config): 42 self.read_copy_config_section(config) 43 if self._has_tar_section(config): 44 self.read_tar_config_section(config) 45 if self._has_rsync_section(config): 46 self.read_rsync_config_section(config) 47 if self._has_mysqldump_section(config): 48 self.read_mysqldump_config_section(config) 49 50 def _has_copy_section(self, config_parser: ConfigParser) -> bool: 51 section = 'COPY' 52 self.run_copy = config_parser.has_section(section) 53 return self.run_copy 54 55 def _has_tar_section(self, config_parser: ConfigParser) -> bool: 56 section = 'TAR' 57 self.run_tar = config_parser.has_section(section) 58 return self.run_tar 59 60 def _has_rsync_section(self, config_parser: ConfigParser) -> bool: 61 section = 'RSYNC' 62 self.run_rsync = config_parser.has_section(section) 63 return self.run_rsync 64 65 def _has_mysqldump_section(self, config_parser: ConfigParser) -> bool: 66 section = 'MYSQLDUMP' 67 self.run_mysqldump = config_parser.has_section(section) 68 return self.run_mysqldump 69 70 DEFAULT_SCHEMA = ConfigSchemaSection('DEFAULT', 71 [ ConfigSchemaLine('backup_paths', True, list, cfg_help.SpecialSchemaProcessing.commma_separated) 72 , ConfigSchemaLine('source_machine', True, str) 73 , ConfigSchemaLine('verbose', False, bool) 74 , ConfigSchemaLine('make_dirs', False, bool) 75 ] 76 ) 77 78 def read_default_section(self, config: ConfigParser, schema: ConfigSchemaSection=DEFAULT_SCHEMA): 79 self.default_dict = cfg_help.parse_config(config, schema) 88 self.read_config_and_schema(config, schema) 89 # self.read_copy_config_section(config, schema) 90 # self.read_tar_config_section(config, schema) 91 # self.read_rsync_config_section(config, schema) 92 # self.read_mysqldump_config_section(config, schema) 93 94 def read_config_and_schema(self, config: ConfigParser, schema: ConfigSchema): 95 parsed_config = ParsedConfig(config, schema) 96 print(parsed_config) 97 self.default_dict = parsed_config.get_section('DEFAULT') 80 98 self.hostname_is_valid() 81 99 self.maybe_make_backup_dirs() 82 100 83 COPY_SCHEMA = ConfigSchemaSection('COPY', 84 [ ConfigSchemaLine('sources', True, list, cfg_help.SpecialSchemaProcessing.commma_separated) 85 , ConfigSchemaLine('additional_args', False, list, cfg_help.SpecialSchemaProcessing.space_separated) 86 ] 87 ) 88 89 def read_copy_config_section(self, config: ConfigParser, schema: ConfigSchemaSection=COPY_SCHEMA): 90 self.copy_dict = cfg_help.parse_config(config, schema) 91 92 TAR_SCHEMA = ConfigSchemaSection('TAR', 93 [ ConfigSchemaLine('sources', True, list, cfg_help.SpecialSchemaProcessing.commma_separated) 94 , ConfigSchemaLine('additional_args', False, list, cfg_help.SpecialSchemaProcessing.space_separated) 95 , ConfigSchemaLine('target_filename', True, str) 96 , ConfigSchemaLine('prefix_date', True, bool) 97 ] 98 ) 99 100 def read_tar_config_section(self, config: ConfigParser, schema: ConfigSchemaSection=TAR_SCHEMA): 101 self.tar_dict = cfg_help.parse_config(config, schema) 102 103 RSYNC_SCHEMA = ConfigSchemaSection('RSYNC', 104 [ ConfigSchemaLine('sources', True, list, cfg_help.SpecialSchemaProcessing.commma_separated) 105 , ConfigSchemaLine('additional_args', False, list, cfg_help.SpecialSchemaProcessing.space_separated) 106 ] 107 ) 108 109 def read_rsync_config_section(self, config: ConfigParser, schema: ConfigSchemaSection=RSYNC_SCHEMA): 110 self.rsync_dict = cfg_help.parse_config(config, schema) 111 112 MYSQLDUMP_SCHEMA = ConfigSchemaSection('MYSQLDUMP', 113 [ ConfigSchemaLine('user', True, str) 114 , ConfigSchemaLine('password', True, str) 115 , ConfigSchemaLine('db_name', True, str) 116 , ConfigSchemaLine('additional_args', False, list, cfg_help.SpecialSchemaProcessing.space_separated) 117 , ConfigSchemaLine('target_filename', True, str) 118 , ConfigSchemaLine('prefix_date', True, bool) 119 ] 120 ) 121 122 def read_mysqldump_config_section(self, config: ConfigParser, schema: ConfigSchemaSection=MYSQLDUMP_SCHEMA): 123 self.mysqldump_dict = cfg_help.parse_config(config, schema) 101 section = 'COPY' 102 self.should_run_copy = parsed_config.has_section(section) 103 if self.should_run_copy: 104 self.copy_dict = parsed_config.get_section(section) 105 106 section = 'TAR' 107 self.should_run_tar = parsed_config.has_section(section) 108 if self.should_run_tar: 109 self.tar_dict = parsed_config.get_section(section) 110 111 section = 'RSYNC' 112 self.should_run_rsync = parsed_config.has_section(section) 113 if self.should_run_rsync: 114 self.rsync_dict = parsed_config.get_section(section) 115 116 section = 'MYSQLDUMP' 117 self.should_run_mysqldump = parsed_config.has_section(section) 118 if self.should_run_mysqldump: 119 self.mysqldump_dict = parsed_config.get_section(section) 120 121 def run_backup_processes(self): 122 123 if self.should_run_copy: 124 self._run_copy() 125 if self.should_run_tar: 126 self._run_tar() 127 if self.should_run_rsync: 128 self._run_rsync() 129 if self.should_run_mysqldump: 130 self._run_mysqldump() 124 131 125 132 def hostname_is_valid(self) -> bool: … … 146 153 os.makedirs(backup_path) 147 154 148 def run_backup(self):149 self._run_copy()150 self._run_tar()151 self._run_rsync()152 self._run_mysqldump()153 154 155 def _run_copy(self): 155 if self. run_copy is None or False:156 if self.should_run_copy is None or False: 156 157 return 0 157 158 self.target_backup_paths_ok(raise_error=True) … … 177 178 178 179 def _run_tar(self): 179 if self. run_tar is None or False:180 if self.should_run_tar is None or False: 180 181 return 0 181 182 … … 203 204 204 205 def _run_rsync(self): 205 if self. run_rsync is None or False:206 if self.should_run_rsync is None or False: 206 207 return 0 207 208
Note:
See TracChangeset
for help on using the changeset viewer.
