Changeset 40893 for branches/ipp-132_automate_jira_conf_backups/backups/backup_atlassian_applications.py
- Timestamp:
- Sep 24, 2019, 9:51:49 AM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ipp-132_automate_jira_conf_backups/backups/backup_atlassian_applications.py
r40892 r40893 1 1 import argparse 2 import configparser 2 3 import datetime 3 4 import glob 4 import operator5 5 import os 6 6 import os.path as path … … 16 16 17 17 18 DEFAULT_ATLASSIAN_HOST = "ippops4" 19 DEFAULT_BACKUP_MACHINE_1 = "ippops3" 20 DEFAULT_BACKUP_MACHINE_2 = "ippops5" 21 DEFAULT_BACKUP_HOST = "/export/{0}.0/atlassian_backups".format(DEFAULT_ATLASSIAN_HOST) 22 DEFAULT_BACKUP_1_DIR = "/data/{0}.0/atlassian_backups".format(DEFAULT_BACKUP_MACHINE_1) 23 DEFAULT_BACKUP_2_DIR = "/data/{0}.0/atlassian_backups".format(DEFAULT_BACKUP_MACHINE_2) 24 25 DEFAULT_ATLASSIAN_HOME_DIR = "/var/atlassian/application-data/" 26 DEFAULT_JIRA_BACKUP_DIR = path.join(DEFAULT_ATLASSIAN_HOME_DIR, "jira/export") 27 DEFAULT_CONF_BACKUP_DIR = path.join(DEFAULT_ATLASSIAN_HOME_DIR, "confluence/backups") 28 DEFAULT_JIRA_ATTACHMENTS_DIR = path.join(DEFAULT_ATLASSIAN_HOME_DIR, "jira/data") 29 DEFAULT_CONF_ATTACHMENTS_DIR = path.join(DEFAULT_ATLASSIAN_HOME_DIR, "confluence/attachments") 30 31 DEFAULT_VERBOSITY = False 32 33 34 class SubprocessError(Exception): 35 """Errors in Popen stderr subprocess""" 36 37 38 def _move_dir_contents_to_dir(source_dirs: [str], destination_dirs: [str]): 18 DEFAULT_CONFIG_FILE = path.abspath('./atlassian_backups.config') 19 20 21 class ConfigError(Exception): 22 """Error in loading the config file""" 23 24 def __init__(self, config_item, message): 25 self.config_item = config_item 26 self.message = message 27 28 29 def _move_dir_contents_to_dir(source_dirs: [str], destination_dirs: [str], verbose: bool=False): 39 30 """ Moves all files from inside source paths to the destination dits. 40 31 It's important to note that it moves index to index … … 64 55 try: 65 56 for fp in filepaths: 66 print("Moving: {0} -> {1}".format(fp, destination_dirs[i])) 57 if verbose: 58 print("Moving: {0} -> {1}".format(fp, destination_dirs[i])) 67 59 command_and_args = ["mv", fp, destination_dirs[i]] 68 60 # shutil.move(fp, destination_dirs[i]) … … 77 69 class AtlassianBackups(): 78 70 79 def __init__(self, 80 host_backup_path=None, 81 backup_1_path=None, 82 backup_2_path=None, 83 jira_backup_path=None, 84 conf_backup_path=None, 85 jira_attachments_path=None, 86 conf_attachments_path=None, 87 verbose=None): 88 89 self.host_backup_path = DEFAULT_BACKUP_HOST if host_backup_path is None else host_backup_path 90 self.backup_1_path = DEFAULT_BACKUP_1_DIR if backup_1_path is None else backup_1_path 91 self.backup_2_path = DEFAULT_BACKUP_2_DIR if backup_2_path is None else backup_2_path 92 self.jira_backup_path = DEFAULT_JIRA_BACKUP_DIR if jira_backup_path is None else jira_backup_path 93 self.conf_backup_path = DEFAULT_CONF_BACKUP_DIR if conf_backup_path is None else conf_backup_path 94 self.jira_attachments_path = DEFAULT_JIRA_ATTACHMENTS_DIR if jira_attachments_path is None else jira_attachments_path 95 self.conf_attachments_path = DEFAULT_CONF_ATTACHMENTS_DIR if conf_attachments_path is None else conf_attachments_path 96 self.verbose = DEFAULT_VERBOSITY if verbose is None else verbose 97 98 host_backup_path = property(operator.attrgetter('_host_backup_path')) 99 100 @host_backup_path.setter 101 def host_backup_path(self, value): 102 self._host_backup_path = value 103 104 backup_1_path = property(operator.attrgetter('_backup_1_path')) 105 106 @backup_1_path.setter 107 def backup_1_path(self, value): 108 self._backup_1_path = value 109 110 backup_2_path = property(operator.attrgetter('_backup_2_path')) 111 112 @backup_2_path.setter 113 def backup_2_path(self, value): 114 self._backup_2_path = value 115 116 jira_backup_path = property(operator.attrgetter('_jira_backup_path')) 117 118 @jira_backup_path.setter 119 def jira_backup_path(self, value): 120 self._jira_backup_path = value 121 122 conf_backup_path = property(operator.attrgetter('_conf_backup_path')) 123 124 @conf_backup_path.setter 125 def conf_backup_path(self, value): 126 self._conf_backup_path = value 127 128 jira_attachments_path = property(operator.attrgetter('_jira_attachments_path')) 129 130 @jira_attachments_path.setter 131 def jira_attachments_path(self, value): 132 self._jira_attachments_path = value 133 134 conf_attachments_path = property(operator.attrgetter('_conf_attachments_path')) 135 136 @conf_attachments_path.setter 137 def conf_attachments_path(self, value): 138 self._conf_attachments_path = value 139 140 verbose = property(operator.attrgetter('_verbose')) 141 142 @verbose.setter 143 def verbose(self, home_dir): 144 self._verbose = home_dir 71 def __init__(self, config_file=None): 72 self.config_file = DEFAULT_CONFIG_FILE if config_file is None else config_file 73 self.load_config(self.config_file) 74 75 def load_config(self, config_class_or_filepath): 76 """Accepts a filepath to a config file, or """ 77 78 config = configparser.ConfigParser() 79 if config_class_or_filepath is None: 80 raise ConfigError(config_class_or_filepath, "Neither config settings or filepath of config given") 81 elif isinstance(config_class_or_filepath, configparser.ConfigParser): 82 config = config_class_or_filepath 83 elif path.exists(config_class_or_filepath): 84 config.read(config_class_or_filepath) 85 86 def _get_str_key_from_section(config, section, key): 87 if config.has_option(section, key): 88 return config[section][key] 89 raise ConfigError(config, "ConfigError: KeyError: no key found for {0}".format(key)) 90 91 section = 'atlassian_backups' 92 self.host_backup_path = _get_str_key_from_section(config, section, 'host_backup_path') 93 self.backup_1_path = _get_str_key_from_section(config, section, 'backup_1_path') 94 self.backup_2_path = _get_str_key_from_section(config, section, 'backup_2_path') 95 self.jira_backup_path = _get_str_key_from_section(config, section, 'jira_backup_path') 96 self.conf_backup_path = _get_str_key_from_section(config, section, 'conf_backup_path') 97 self.jira_attachments_path = _get_str_key_from_section(config, section, 'jira_attachments_path') 98 self.conf_attachments_path = _get_str_key_from_section(config, section, 'conf_attachments_path') 99 self.verbose = config.getboolean('atlassian_backups', 'verbose') 145 100 146 101 def target_backup_paths_ok(self) -> bool: … … 184 139 185 140 # Move latest to tmp (and delete if move ok) 186 _move_dir_contents_to_dir(latest_paths, tmp_paths )141 _move_dir_contents_to_dir(latest_paths, tmp_paths, self.verbose) 187 142 188 143 # copy jira 189 jira_return = self._copy_jira_backup( )144 jira_return = self._copy_jira_backup(self.verbose) 190 145 191 146 # copy confluence 192 conf_return = self._copy_confluence_backup( )147 conf_return = self._copy_confluence_backup(self.verbose) 193 148 194 149 # tarfiles: 195 150 tar_return = self._tar_attachments() 196 151 197 # make mysql dump198 199 152 # If successful: move tmp to old 200 153 if jira_return == 0 and conf_return == 0 and tar_return == 0: 201 _move_dir_contents_to_dir(tmp_paths, old_paths )154 _move_dir_contents_to_dir(tmp_paths, old_paths, self.verbose) 202 155 else: 203 156 # If any failed: delete latest, move tmp back 204 _move_dir_contents_to_dir(tmp_paths, latest_paths )157 _move_dir_contents_to_dir(tmp_paths, latest_paths, self.verbose) 205 158 return 5 206 159 … … 216 169 return datetime.date.today().strftime(jira_format) 217 170 218 def _copy_jira_backup(self ):171 def _copy_jira_backup(self, verbose=False): 219 172 if not self.target_backup_paths_ok(): 220 173 return 2 … … 222 175 jira_wildcard_search = "{0}/{1}*".format(self.jira_backup_path, self.get_jira_backup_date_format()) 223 176 jira_xml_filepaths = glob.glob(jira_wildcard_search) 224 print(len(jira_xml_filepaths))225 177 if len(jira_xml_filepaths) == 0: 226 178 print("Copy Failure: No jira backups found at: ", jira_wildcard_search) … … 233 185 return 2 234 186 try: 235 print("Copying: {0} to {1}".format(jfile, tpath)) 187 if verbose: 188 print("Copying: {0} to {1}".format(jfile, tpath)) 236 189 shutil.copy2(jfile, tpath) 190 sub_utils.chmod_wrapper(['774', os.path.join(tpath, os.path.basename(jfile))]) 237 191 except IOError as e: 238 192 print("Error: copying jira backup.\n copying: {0}\n to: {1}".format(e.filename, e.filename2)) … … 248 202 return datetime.date.today().strftime(conf_format) 249 203 250 def _copy_confluence_backup(self ):204 def _copy_confluence_backup(self, verbose=False): 251 205 if not self.target_backup_paths_ok(): 252 206 print("Copy Failure: target path failure") … … 255 209 conf_wildcard_search = "{0}/*{1}*.zip".format(self.conf_backup_path, self.get_confluence_backup_date_format()) 256 210 conf_xml_filepaths = glob.glob(conf_wildcard_search) 257 print(len(conf_xml_filepaths))258 211 if len(conf_xml_filepaths) == 0: 259 212 print("Copy Failure: No confluence backups found at: ", conf_wildcard_search) … … 263 216 for tpath in target_paths: 264 217 try: 265 print("using cp wrapper")266 218 sub_utils.cp_wrapper([cfile, tpath]) 219 sub_utils.chmod_wrapper(['774', os.path.join(tpath, os.path.basename(cfile))]) 267 220 except Exception as e: 268 221 print(e) … … 279 232 jira_tar_filepath = path.join(latest_paths[0], jira_tar_name) 280 233 jira_tar_args = ["-cf", jira_tar_filepath, self.jira_attachments_path] 281 print(jira_tar_args)282 234 sub_utils.tar_wrapper(jira_tar_args) 235 sub_utils.chmod_wrapper(['774', jira_tar_filepath]) 283 236 # copy to backups 284 237 sub_utils.cp_wrapper([ jira_tar_filepath, latest_paths[1]]) … … 289 242 confluence_tar_filepath = path.join(latest_paths[0], confluence_tar_name) 290 243 confluence_tar_args = ["-cf", confluence_tar_filepath, self.conf_attachments_path] 291 print(confluence_tar_args)292 244 sub_utils.tar_wrapper(confluence_tar_args) 245 sub_utils.chmod_wrapper(['774', confluence_tar_filepath]) 293 246 # copy to backups 294 247 sub_utils.cp_wrapper([ confluence_tar_filepath, latest_paths[1]]) … … 326 279 decoded_stderr = stde.decode() 327 280 if decoded_stderr.casefold().find('error'.casefold()) > -1: 328 raise SubprocessError()281 raise sub_utils.SubprocessError() 329 282 330 283 # move the dump file to the correct locations … … 343 296 344 297 def run(self): 345 self.perform_atlassian_backups() 346 return 0 298 return self.perform_atlassian_backups() 347 299 348 300 … … 351 303 args_dict = vars(args) 352 304 ab = AtlassianBackups(**args_dict) 353 ab.run()305 return ab.run() 354 306 355 307 … … 359 311 description='Performs backup of atlassian applications\nYou should run this on the machine hosting the atlassian applications') 360 312 361 parser.add_argument('-- host_backup_path',313 parser.add_argument('--config_file', 362 314 nargs='?', 363 help='absolute path of where the backups should be copied to on the host', 364 default=DEFAULT_BACKUP_HOST) 365 parser.add_argument('--backup_1_dir', 366 nargs='?', 367 help='directory of where backups should be copied to', 368 default=DEFAULT_BACKUP_1_DIR) 369 parser.add_argument('--backup_2_dir', 370 nargs='?', 371 help='second directory of where backups should be copied to', 372 default=DEFAULT_BACKUP_1_DIR) 373 parser.add_argument('--jira_backup_dir', 374 nargs='?', 375 help='directory where the zipped xml backups are located', 376 default=DEFAULT_JIRA_BACKUP_DIR) 377 parser.add_argument('--conf_backup_dir', 378 nargs='?', 379 help='directory where the confluence xml backups are located', 380 default=DEFAULT_CONF_BACKUP_DIR) 381 parser.add_argument('--jira_attachments_dir', 382 nargs='?', 383 help='direcory of where the jira attachments are located', 384 default=DEFAULT_JIRA_ATTACHMENTS_DIR) 385 parser.add_argument('--conf_attachments_dir', 386 nargs='?', 387 help='directory of where the confluence attachments are located', 388 default=DEFAULT_CONF_ATTACHMENTS_DIR) 389 parser.add_argument('--verbose', '-v', 390 action='store_true', 391 help='will print some verbose messages') 315 help='contains config for all paths and mysqldump details', 316 default=DEFAULT_CONFIG_FILE) 392 317 393 318 return parser.parse_args()
Note:
See TracChangeset
for help on using the changeset viewer.
