示例#1
0
def backup_mode_sql_server(backup_opt_dict, storage):
    """
    Execute a SQL Server DB backup. Currently only backups with shadow
    copy are supported. This mean, as soon as the shadow copy is created
    the db writes will be blocked and a checkpoint will be created, as soon
    as the backup finish the db will be unlocked and the backup will be
    uploaded. A sql_server.conf_file is required for this operation.
    """
    with open(backup_opt_dict.sql_server_conf, 'r') as sql_conf_file_fd:
        for line in sql_conf_file_fd:
            line = line.strip().split('#', 1)[0]
            if not line:
                continue

            key, value = line.split('=')
            # remove white spaces
            key = key.strip()
            value = value.strip()

            if key == 'instance':
                db_instance = utils.dequote(value)
                backup_opt_dict.sql_server_instance = db_instance
                continue
            else:
                raise Exception('Please indicate a valid SQL Server instance')

    try:
        stop_sql_server(backup_opt_dict.sql_server_instance)
        backup(backup_opt_dict, storage, backup_opt_dict.engine)
    finally:
        if not backup_opt_dict.vssadmin:
            # if vssadmin is false, wait until the backup is complete
            # to start sql server again
            start_sql_server(backup_opt_dict.sql_server_instance)
示例#2
0
    def test_stop_sql_server(self, monkeypatch):
        fake_disable_redirection = FakeDisableFileSystemRedirection()
        backup_opt = BackupOpt1()
        fakelogging = FakeLogging()
        fakesubprocess = FakeSubProcess()
        fakesubprocesspopen = fakesubprocess.Popen()

        monkeypatch.setattr(
            subprocess.Popen, 'communicate',
            fakesubprocesspopen.communicate)
        monkeypatch.setattr(
            subprocess, 'Popen', fakesubprocesspopen)
        monkeypatch.setattr(
            winutils.DisableFileSystemRedirection, '__enter__',
            fake_disable_redirection.__enter__)
        monkeypatch.setattr(
            winutils.DisableFileSystemRedirection, '__exit__',
            fake_disable_redirection.__exit__)

        monkeypatch.setattr(logging, 'info', fakelogging.info)

        assert winutils.start_sql_server(backup_opt) is not False

        fakesubprocess = FakeSubProcess3()
        fakesubprocesspopen = fakesubprocess.Popen()

        monkeypatch.setattr(
            subprocess.Popen, 'communicate',
            fakesubprocesspopen.communicate)
        monkeypatch.setattr(
            subprocess, 'Popen', fakesubprocesspopen)

        pytest.raises(Exception, winutils.stop_sql_server(backup_opt))