summaryrefslogtreecommitdiff
path: root/verification-test
blob: 04a475d6ab4db5bc23252cdb4f1e34336a8b051a (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/bin/sh
#
# Obnam verification test.
#
# This script runs and verifies backups with Obnam. There are two stages.
# In the first stage, the user makes backups frequently for some period
# of time (e.g., daily for a week). In the second stage, every backup
# generation is restored and the restored data compared with the original.
# The test succeeds if all generations can be restored and verified
# successfully.
#
# The verification is done using the summain(1) checksumming and manifest
# generation tool. Obnam has an internal verification command, but it is
# better to use an independent tool. summain(1) is a better choice than,
# say, md5sum, since it includes much of the inode metadata in the manifest,
# so that restoring file permissions, etc, are also verified.
#
# To use this script, run it one the following ways:
#
#   ./verification-test backup REPO DIR
#   ./verification-test verify REPO DIR
#
# You can optionally add the name of a configuration file to use as the
# fourth argument.
#
# You must run this script from the Obnam source directory.
# The repository must be a local directory.
# You can choose any DIR you like, but it should be something that changes
# frequently and is small enough that you don't get impatient about while
# it is getting backed up.
#
# The filesystem (or the parts inside the specified directory) MUST be
# idle from when the backup start until it ends. Otherwise the test may
# fail even though Obnam was working ine: it's just that some file changed
# between Obnam backing it up and summain including it in the manifest.
#
# Copyright 2011-2014  Lars Wirzenius
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

set -eux

die()
{
    echo "$@" 1>&2
    exit 1
}

backup()
{
    local repo="$1"
    local dir="$2"
    local conf="$3"
    local client=$(awk '/client-name/ { print $3 }' "$conf")
    local genids=$(mktemp)

    ./obnam --no-default-configs --config="$conf" backup || exit 1

    while ! ./obnam --no-default-configs --config="$conf" genids > "$genids"
    do
        :
    done
    local gen=$(tail -n1 "$genids")
    if [ "x$gen" = "x" ]
    then
        die "gen is empty"
    fi
    summain "$dir" -r --output="$repo/$client.$gen.summain"
}

abspath()
{
    case "$1" in
        /*) echo -n "$1" ;;
        *) echo -n "$(pwd)/$1" ;;
    esac
}

verify()
{
    local repo="$1"
    local dir=$(abspath "$2")
    local conf="$3"
    local tempdir="$(mktemp -d)"
    local client=$(awk '/client-name/ { print $3 }' "$conf")

    ./obnam --no-default-configs --config="$conf" genids \
        > "$tempdir"/gens || exit 42
    while read gen
    do
        rm -rf "$tempdir/$gen"
        ./obnam --no-default-configs --config="$conf" \
            restore --to="$tempdir/$gen" --generation="$gen" || \
            exit 42
        summain "$tempdir/$gen/$dir" -r --output="$tempdir/summain.$gen"
        if ! diff -u "$repo/$client.$gen.summain" "$tempdir/summain.$gen"
        then
            die "generation $gen failed to restore properly, see $tempdir"
        fi
        rm -rf "$tempdir/$gen" "$tempdir/summain.$gen"
    done < "$tempdir/gens"
    rm -rf "$tempdir"
}

[ "$#" = 4 ] || die "Bad usage, read source!"

repo="$2"
root="$3"
conf="$4"

case "$1" in
     backup)
        backup "$repo" "$root" "$conf"
        ;;
     verify)
        verify "$repo" "$root" "$conf"
        ;;
     *)
        die "Unknown subcommand $1"
        ;;
esac