Exemple #1
0
def test_no_source(tmpdir):
    cfg_file = tmpdir.join('twindb-backup.cfg')
    with open(str(cfg_file), 'w') as fp:
        fp.write('')
    tbc = TwinDBBackupConfig(config_file=str(cfg_file))
    with pytest.raises(ConfigurationError):
        assert tbc.backup_dirs == []
def test_run_backup_job_gets_lock(mock_get_timeout, mock_backup_everything,
                                  tmpdir):
    config_content = """
[source]
backup_dirs=/etc /root /home

[intervals]
run_hourly=yes
run_daily=yes
run_weekly=yes
run_monthly=yes
run_yearly=yes
    """
    lock_file = str(tmpdir.join('foo.lock'))
    config_file = tmpdir.join('foo.cfg')
    config_file.write(config_content)

    cfg = TwinDBBackupConfig(config_file=str(config_file))

    mock_get_timeout.return_value = 1

    run_backup_job(cfg, 'hourly', lock_file=lock_file)
    mock_backup_everything.assert_called_once_with('hourly',
                                                   cfg,
                                                   binlogs_only=False)
Exemple #3
0
def test_ssh(config_file):
    tbc = TwinDBBackupConfig(config_file=str(config_file))
    assert tbc.ssh.user == 'root'
    assert tbc.ssh.key == '/root/.ssh/id_rsa'
    assert tbc.ssh.port == 123
    assert tbc.ssh.host == '127.0.0.1'
    assert tbc.ssh.path == '/tmp/backup'
Exemple #4
0
def test_no_backup_dirs(tmpdir):
    cfg_file = tmpdir.join('twindb-backup.cfg')
    with open(str(cfg_file), 'w') as fp:
        fp.write(dedent("""
                [source]
                """))
    tbc = TwinDBBackupConfig(config_file=str(cfg_file))
    assert tbc.backup_dirs == []
Exemple #5
0
def test_mysql(config_file):
    tbc = TwinDBBackupConfig(config_file=str(config_file))
    assert tbc.mysql.full_backup == 'daily'
    assert tbc.mysql.defaults_file == '/etc/twindb/my.cnf'
    assert tbc.mysql.expire_log_days == 8
    assert tbc.mysql.xtrabackup_binary == \
        '/opt/twindb-backup/embedded/bin/xtrabackup'
    assert tbc.mysql.xbstream_binary == \
        '/opt/twindb-backup/embedded/bin/xbstream'
Exemple #6
0
def test_retention(config_file):
    tbc = TwinDBBackupConfig(config_file=str(config_file))
    assert tbc.retention == RetentionPolicy(hourly=24,
                                            daily=7,
                                            weekly=4,
                                            monthly=12,
                                            yearly=3)

    assert tbc.retention_local == RetentionPolicy(hourly=24,
                                                  daily=7,
                                                  weekly=4,
                                                  monthly=12,
                                                  yearly=3)
Exemple #7
0
def test_custom_xtrabackup_binary(tmpdir):
    cfg_file = tmpdir.join('twindb-backup.cfg')
    with open(str(cfg_file), 'w') as fp:
        fp.write(
            dedent("""
                [mysql]
                mysql_defaults_file=/etc/twindb/my.cnf
                expire_log_days = 8
                xtrabackup_binary = foo
                xbstream_binary = bar
                """))
    tbc = TwinDBBackupConfig(config_file=str(cfg_file))
    assert tbc.mysql.xtrabackup_binary == 'foo'
    assert tbc.mysql.xbstream_binary == 'bar'
Exemple #8
0
def test_get_modifier(content, compressor, expected_kw, tmpdir):
    cfg_file = tmpdir.join('twindb-backup.cfg')
    with open(str(cfg_file), 'w') as fp:
        fp.write(content)

    tbc = TwinDBBackupConfig(config_file=str(cfg_file))

    with mock.patch.object(compressor, '__init__') as mock_compr:
        mock_compr.return_value = None
        mock_stream = mock.Mock()
        assert isinstance(
            tbc.compression.get_modifier(
                mock_stream
            ),
            compressor
        )
        mock_compr.assert_called_once_with(mock_stream, **expected_kw)
Exemple #9
0
def test_default_values(tmpdir):
    cfg_file = tmpdir.join('twindb-backup.cfg')
    with open(str(cfg_file), 'w') as fp:
        fp.write(
            dedent(
                """
                [ssh]
                ssh_user=root
                ssh_key=/etc/twindb/private_key

                [mysql]
                mysql_defaults_file=/etc/twindb/my.cnf
                """
            )
        )
    tbc = TwinDBBackupConfig(config_file=str(cfg_file))
    assert tbc.ssh.host == '127.0.0.1'
    assert tbc.ssh.path == '/var/backup'
Exemple #10
0
def main(
        ctx,
        debug,  # pylint: disable=too-many-arguments
        config,
        version,
        xtrabackup_binary,
        xbstream_binary):
    """
    Main entry point

    :param ctx: context (See Click docs (http://click.pocoo.org/6/)
    for explanation)
    :param debug: if True enabled debug logging
    :type debug: bool
    :param config: path to configuration file
    :type config: str
    :param version: If True print version string
    :type version: bool
    :param xtrabackup_binary: Path to xtrabackup binary.
    :type xtrabackup_binary: str
    :param xbstream_binary: Path to xbstream binary.
    :type xbstream_binary: str
    """
    if not ctx.invoked_subcommand:
        if version:
            print(__version__)
            exit(0)
        else:
            print(ctx.get_help())
            exit(-1)

    setup_logging(LOG, debug=debug)

    if os.path.exists(config):
        ctx.obj = {'twindb_config': TwinDBBackupConfig(config_file=config)}
        if xtrabackup_binary is not None:
            ctx.obj['twindb_config'].mysql.xtrabackup_binary = \
                xtrabackup_binary
        if xbstream_binary is not None:
            ctx.obj['twindb_config'].mysql.xbstream_binary = \
                xbstream_binary
    else:
        LOG.warning("Config file %s doesn't exist", config)
        exit(os.EX_CONFIG)
def test_keep_local_path(config_file):
    tbc = TwinDBBackupConfig(config_file=str(config_file))
    assert tbc.keep_local_path == '/var/backup/local'
def test_no_keep_local_path(content, tmpdir):
    cfg_file = tmpdir.join('twindb-backup.cfg')
    with open(str(cfg_file), 'w') as fp:
        fp.write(content)
    tbc = TwinDBBackupConfig(config_file=str(cfg_file))
    assert tbc.keep_local_path is None
Exemple #13
0
def test_s3(config_file):
    tbc = TwinDBBackupConfig(config_file=str(config_file))
    assert tbc.s3.aws_access_key_id == 'XXXXX'
    assert tbc.s3.aws_secret_access_key == 'YYYYY'
    assert tbc.s3.aws_default_region == 'us-east-1'
    assert tbc.s3.bucket == 'twindb-backups'
def test_run_intervals(config_file):
    tbc = TwinDBBackupConfig(config_file=str(config_file))
    assert tbc.run_intervals == RunIntervals()
Exemple #15
0
def test_no_mysql_section(tmpdir):
    cfg_file = tmpdir.join('twindb-backup.cfg')
    with open(str(cfg_file), 'w') as fp:
        fp.write('')
    tbc = TwinDBBackupConfig(config_file=str(cfg_file))
    assert tbc.mysql is None
Exemple #16
0
def test_mysql_set_xbstream_binary(config_file):
    tbc = TwinDBBackupConfig(config_file=str(config_file))
    tbc.mysql.xbstream_binary = 'foo'
    assert tbc.mysql.xbstream_binary == 'foo'
Exemple #17
0
def test_backup_dirs(config_file):
    tbc = TwinDBBackupConfig(config_file=str(config_file))
    assert tbc.backup_dirs == [
        '/', '/root/', '/etc', '/dir with space/', '/dir foo'
    ]
Exemple #18
0
def test_default(config_file):
    tbc = TwinDBBackupConfig(config_file=str(config_file))
    assert tbc.compression is not None
    assert tbc.compression.program == 'gzip'
Exemple #19
0
def test_gcs(config_file):
    tbc = TwinDBBackupConfig(config_file=str(config_file))
    assert tbc.gcs.gc_credentials_file == 'XXXXX'
    assert tbc.gcs.gc_encryption_key == ''
    assert tbc.gcs.bucket == 'twindb-backups'