IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Sep 20, 2019, 4:01:54 PM (7 years ago)
Author:
fairlamb
Message:

Added the mysql dump part and refactored some testing

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ipp-132_automate_jira_conf_backups/backups/backup_atlassian_applications.py

    r40885 r40892  
    66import os.path as path
    77import shutil
     8import subprocess
    89import sys
    910
     
    2930
    3031DEFAULT_VERBOSITY = False
     32
     33
     34class SubprocessError(Exception):
     35    """Errors in Popen stderr subprocess"""
    3136
    3237
     
    198203            # If any failed: delete latest, move tmp back
    199204            _move_dir_contents_to_dir(tmp_paths, latest_paths)
     205            return 5
    200206
    201207        # TODO:clean-up old files (to save space)
     
    290296        return 0
    291297
    292     def make_mysql_dump(self):
     298    def make_mysql_dump(self, dump_password, dump_name=None):
    293299        # do this as the dumper user
    294300
     301        latest_paths = self.target_paths("latest")
     302        if not self.target_backup_paths_ok():
     303            return 1
     304
    295305        # make sure target file does not exist (or it will fail)
    296 
    297         # verify mysql checksum?
     306        if dump_name is None:
     307            date = datetime.date.today().strftime("%Y_%m_%d")
     308            dump_filepath = path.join(latest_paths[0], '{0}_mysql_dump.sql'.format(date))
     309        else:
     310            dump_filepath = path.join(latest_paths[0], dump_name)
     311        if path.exists(dump_filepath):
     312            print("Warning: SQL dump already exists: ", dump_filepath)
     313            return 2
     314
     315        # Setup the commands:
     316        mysqldump_args = [ 'mysqldump', '-u', 'dumper', '-p{0}'.format(dump_password)
     317                         , '--databases', 'jiradb', 'confluencedb'
     318                         ]
     319        with open(dump_filepath, "w+") as df:
     320            mysqldump_process = subprocess.Popen(mysqldump_args, stdout=df, stderr=subprocess.PIPE)
     321            dump_result = mysqldump_process.communicate()
     322
     323        # Get the piped std err. See it contains the word error
     324        stdo, stde = dump_result
     325        if stde is not None:
     326            decoded_stderr = stde.decode()
     327            if decoded_stderr.casefold().find('error'.casefold()) > -1:
     328                raise SubprocessError()
    298329
    299330        # move the dump file to the correct locations
     331        if not path.exists(dump_filepath):
     332            return 2
     333        cp_1 = sub_utils.cp_wrapper([dump_filepath, latest_paths[1]])
     334        cp_2 = sub_utils.cp_wrapper([dump_filepath, latest_paths[2]])
     335        if cp_1 != 0:
     336            return cp_1
     337        if cp_2 != 0:
     338            return cp_2
    300339        return 0
    301340
Note: See TracChangeset for help on using the changeset viewer.