diff options
-rwxr-xr-x | mksparse | 78 | ||||
-rw-r--r-- | yarns/0030-basics.yarn | 25 | ||||
-rw-r--r-- | yarns/9000-implements.yarn | 28 |
3 files changed, 131 insertions, 0 deletions
diff --git a/mksparse b/mksparse new file mode 100755 index 00000000..6c1a2cff --- /dev/null +++ b/mksparse @@ -0,0 +1,78 @@ +#!/usr/bin/python +# Copyright 2013 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/>. +# +# =*= License: GPL-3+ =*= + + +description = '''Create a sparse file. + +The first command line argument is the name of the output file. The +second argument is a specification for how the file is to be made +sparse: a sequence of "data" and "hole" words, which may be +interspersed with spaces, commas, or the word "a", all of which are +ignored, except that the "data" and "hole" words must have something +in between them. + +''' + + +import cliapp +import os + + +class CreateSparseFile(cliapp.Application): + + data_size = 1024 + hole_size = 1024**2 + + def process_args(self, args): + if len(args) != 2: + raise cliapp.AppException('Usage!') + + output_filename = args[0] + spec = self.parse_spec(args[1]) + + with open(output_filename, 'w') as f: + for word in spec: + if word == 'hole': + self.append_hole(f) + else: + assert word == 'data' + self.append_data(f) + + def parse_spec(self, arg): + # Remove commas. + arg = ' '.join(arg.split(',')) + + # Split into words. + words = arg.split() + + # Remove any words that are not "data" or "hole". + spec = [x for x in words if x in ('data', 'hole')] + + return spec + + def append_data(self, f): + f.write('x' * self.data_size) + f.flush() + + def append_hole(self, f): + fd = f.fileno() + pos = os.lseek(fd, self.hole_size, os.SEEK_CUR) + os.ftruncate(fd, pos) + + +CreateSparseFile(description=description).run() diff --git a/yarns/0030-basics.yarn b/yarns/0030-basics.yarn index 7cdc3ba7..0106eb0d 100644 --- a/yarns/0030-basics.yarn +++ b/yarns/0030-basics.yarn @@ -22,3 +22,28 @@ program must be able to handle. WHEN user backs up live data THEN user can restore their data correctly AND user can fsck the repository + +Backup sparse files +------------------- + +Sparse files present an interesting challenge to backup programs. Most +people have none, but some people have lots, and theirs can have very +large holes. For example, at work I often generate disk images as +raw disk images in sparse files. The image may need to be, say 30 +gigabytes in size, even though it only contains one or two gigabyte of +data. The rest is a hole. + +A backup program should restore a sparse file as a sparse file. +Otherwise, the 30 gigabyte disk image file will, upon restore, use 30 +gigabytes of disk space, rather than one. That might make restoring +impossible. + +Unfortunately, it is not easy to (portably) check whether a file is +sparse. We'll settle for making sure the restored file does not use +more disk space than the one in live data. + + SCENARIO backup a sparse file + GIVEN a file S in live data, with a hole, data, a hole + WHEN user backs up live data + THEN user can restore their data into X + AND restored file S in X doesn't use more disk diff --git a/yarns/9000-implements.yarn b/yarns/9000-implements.yarn index 41b6db4d..2cffdd69 100644 --- a/yarns/9000-implements.yarn +++ b/yarns/9000-implements.yarn @@ -34,6 +34,15 @@ for single-client scenarios. IMPLEMENTS GIVEN (\S+) of live data genbackupdata --quiet --create "$MATCH_1" "$LIVEDATA" +We also need to generate a sparse file. A sparse file has at least one +hole in it, and it may matter where the hole is: at the beginning, +middle, or end of the file. Thus, we provide a way for scenarios to +specify that. + + IMPLEMENTS GIVEN a file (\S+) in live data, with (.+) + mkdir -p "$LIVEDATA" + "$SRCDIR/mksparse" "$LIVEDATA/$MATCH_1" "$MATCH_2" + Backing up and verifying a backup --------------------------------- @@ -59,3 +68,22 @@ We further verify that the repository itself is OK, by running IMPLEMENTS THEN user can fsck the repository run_obnam fsck -r "$REPO" + +Restoring data +-------------- + +We need a way to restore data from a test backup repository. + + IMPLEMENTS THEN user can restore their data into (\S+) + run_obnam restore -r "$REPO" --to "$DATADIR/$MATCH_1" + +Checks on files +--------------- + +Check that a restored file uses at most as much disk space as the +original one in live data. + + IMPLEMENTS THEN restored file (\S+) in (\S+) doesn't use more disk + old=$(stat -c %b "$LIVEDATA/$MATCH_1") + new=$(stat -c %b "$DATADIR/$MATCH_2/$LIVEDATA/$MATCH_1") + test "$old" -lt "$new" |