aboutsummaryrefslogtreecommitdiffstats
path: root/molecule/default/tests/test_manual_backup.py
blob: b4068bcc3e1049c2a05aac17b576d7320f9a91cc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import pytest
from datetime import datetime

testinfra_hosts = ['borg-client', 'borg-client-2']


def get_server_info(hostname):
    if hostname == 'borg-client-2':
        return ('backupserver', 'borg-server-2', '/var/backups')
    return ('borg', 'borg-server', '/opt/borg')


compression_types = [
    'none',
    'lz4',
    'zstd',
    'zstd,10',
    'zlib',
    'zlib,6',
]


"""Creates backups with all possible combinations of compression to the backup
host"""


@pytest.mark.parametrize('compression', compression_types)
def test_backup_push(host, compression):
    hostname = host.backend.get_hostname()
    server_user, server_host, server_path = get_server_info(hostname)

    c = host.run(
        f'borg create -C "{compression}" {server_user}@{server_host}:{server_path}/{hostname}::testinfra-{{now:%S.%f}} /etc'
    )
    assert c.rc == 0
    assert c.stdout == ''
    assert c.stderr == ''


@pytest.mark.parametrize('compression', compression_types)
def test_backup_restore(host, compression):
    hostname = host.backend.get_hostname()
    server_user, server_host, server_path = get_server_info(hostname)
    timestamp = datetime.now().strftime('%Y%m%d-%H%M%S-%f')
    archive_name = f"testinfra-backup-restore-{compression}-{timestamp}"

    # Create backup
    c = host.run(
        f'borg create -C "{compression}" {server_user}@{server_host}:{server_path}/{hostname}::{archive_name} /etc'
    )
    assert c.rc == 0
    assert c.stdout == ''
    assert c.stderr == ''

    # Restore Backup
    c = host.run(
        f"cd /mnt && borg extract {server_user}@{server_host}:{server_path}/{hostname}::{archive_name}"
    )
    assert c.rc == 0
    assert c.stdout == ''
    assert c.stderr == ''

    # Check if every file exists, content has, and permissions / metadata
    c1 = host.run(
        'cd /etc && find /etc -type f -printf "%P\\n" | sort | xargs -i sh -c "echo {}; sha512sum {} | cut -d \' \' -f 1; ls -l {}; echo"'
    )
    c2 = host.run(
        'cd /mnt/etc && find /etc -type f -printf "%P\\n" | sort | xargs -i sh -c "echo {}; sha512sum {} | cut -d \' \' -f 1; ls -l {}; echo"'
    )
    assert c1.rc == 0 and c2.rc == 0
    assert c1.stderr == '' and c2.stderr == ''
    assert c1.stdout == c2.stdout

    # Delete directory extract directory again for future tests
    c = host.run('rm -rf /mnt/etc')
    assert c.rc == 0
    assert c.stdout == ''
    assert c.stderr == ''

    # Delete backup
    c = host.run(f"borg delete {server_user}@{server_host}:{server_path}/{hostname}::{archive_name}")
    assert c.rc == 0
    assert c.stdout == ''
    assert c.stderr == ''