#!/usr/bin/env python # Copyright 2013-2014,2017 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 . # # =*= License: GPL-3+ =*= description = '''\ Create a reference repository for an Obnam version. Create a tar file containing an Obnam backup repository and some summain(1) manifests. The repository contains two backup generations, of test live data generated by ./mkfunnyfarm. The backups are made using encryption and compression, using the test PGP keys in ./test-gpghome. The point of this is to archive the tar file and use it to test that future versions of Obnam can restore the data and otherwise use the repository. Only one command line argument: path to the Obnam executable. ''' import cliapp import logging import os import shutil import tarfile import tempfile class CreateReferenceRepository(cliapp.Application): def add_settings(self): self.settings.string( ['obnam'], 'which obnam to execute', metavar='CMD', default='./obnam') self.settings.string_list( ['obnam-argument', 'a'], 'add ARG to obnam command line', metavar='ARG') def process_args(self, args): if args: raise cliapp.AppException('Wrong command line args') tempdir = tempfile.mkdtemp() live_dir = os.path.join(tempdir, 'data') repo_dir = os.path.join(tempdir, 'repo') logging.info('Live data: %s', live_dir) logging.info('Repository: %s', repo_dir) # First generation. self.create_live_data(live_dir) self.create_manifest(live_dir, os.path.join(tempdir, 'manifest-1')) self.backup(live_dir, repo_dir) # Second generation. self.modify_live_data(live_dir) self.create_manifest(live_dir, os.path.join(tempdir, 'manifest-2')) self.backup(live_dir, repo_dir) # Remove the live data. Everything will be tarred up. shutil.rmtree(live_dir) # Tar up. self.create_tar_file(tempdir) def create_live_data(self, dirname): logging.info('Creating live data in %s with funnyfarm', dirname) cliapp.runcmd(['./mkfunnyfarm', dirname]) def modify_live_data(self, dirname): logging.info('Modifying live data in %s', dirname) filename = os.path.join(dirname, 'new-file.txt') with open(filename, 'w') as f: f.write('This file created for 2nd generation.\n') def create_manifest(self, dirname, manifest_name): logging.info('Creating manifest of %s in %s', dirname, manifest_name) cliapp.runcmd( ['summain', '--exclude=Ino', '--exclude=Dev', '--exclude=Uid', '--exclude=Username', '--exclude=Gid', '--exclude=Group', '--checksum=SHA1', '-r', '--output', manifest_name, dirname]) def backup(self, live_dir, repo_dir): logging.info('Backing up %s to %s', live_dir, repo_dir) env = dict(os.environ) env['GNUPGHOME'] = 'test-gpghome' argv = [ self.settings['obnam'], '--no-default-config', '--quiet', '--weak-random', '--compress-with=deflate', '-r', repo_dir, ] # This encryption key is the one in test-gpghome. argv += ['--encrypt-with=3B1802F81B321347'] argv += self.settings['obnam-argument'] argv += ['backup', live_dir] logging.info('Command line: %s', repr(argv)) cliapp.runcmd(argv, env=env) def create_tar_file(self, tempdir): logging.info('Creating tar file of %s to stdout', tempdir) with tarfile.TarFile(fileobj=self.output, mode='w') as f: for basename in os.listdir(tempdir): pathname = os.path.join(tempdir, basename) f.add(pathname, arcname=basename) CreateReferenceRepository(description=description).run()