Skip to content

Commit

Permalink
Check in test scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbertchen committed Jul 2, 2017
1 parent 37d34c9 commit da7ea1c
Show file tree
Hide file tree
Showing 5 changed files with 604 additions and 0 deletions.
150 changes: 150 additions & 0 deletions linux-backup-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
#!/bin/bash

if [ "$#" -eq 0 ]; then
echo "Usage: $0 <test dir>"
exit 1
fi

if [ -z "$DUPLICACY_PATH" ]; then
echo "DUPLICACY_PATH must be set to the path of the Duplicacy executable"
exit 1
fi

if [ -z "$RESTIC_PATH" ]; then
echo "RESTIC_PATH must be set to the path of the restic executable"
exit 1
fi

if [ -z "$ATTIC_PATH" ]; then
echo "ATTIC_PATH must be set to the path of the attic executable"
exit 1
fi

if [ -z "$DUPLICITY_PATH" ]; then
echo "DUPLICITY_PATH must be set to the path of the duplicity executable"
exit 1
fi

if [ -z "$GPG_KEY" ]; then
echo "GPG_KEY must be set for duplicity to work properly"
exit 1
fi

if [ -z "$PASSPHRASE" ]; then
echo "PASSPHRASE must be set for duplicity to work properly"
exit 1
fi

# Set up directories
TEST_DIR=$1
BACKUP_DIR=${TEST_DIR}/linux
DUPLICACY_STORAGE=${TEST_DIR}/linux-duplicacy-storage
RESTIC_STORAGE=${TEST_DIR}/linux-restic-storage
ATTIC_STORAGE=${TEST_DIR}/linux-attic-storage
DUPLICITY_STORAGE=${TEST_DIR}/linux-duplicity-storage

# Used as the storage password throughout the tests
PASSWORD=12345678

# Clean up the storages
rm -rf ${DUPLICACY_STORAGE}
mkdir -p ${DUPLICACY_STORAGE}
rm -rf ${RESTIC_STORAGE}
mkdir -p ${RESTIC_STORAGE}
rm -rf ${ATTIC_STORAGE}
mkdir -p ${ATTIC_STORAGE}
rm -rf ${DUPLICITY_STORAGE}
mkdir -p ${DUPLICITY_STORAGE}

# Download the github repository if needed
if [ ! -d "${BACKUP_DIR}" ]; then
git clone https://github.com/torvalds/linux.git ${BACKUP_DIR}
fi

function duplicacy_backup()
{
time env DUPLICACY_PASSWORD=${PASSWORD} ${DUPLICACY_PATH} backup -stats | grep -v Uploaded
}

function restic_backup()
{
time env RESTIC_PASSWORD=${PASSWORD} ${RESTIC_PATH} -r ${RESTIC_STORAGE} --exclude-file=${BACKUP_DIR}/.duplicacy/restic-exclude backup ${BACKUP_DIR}
}

function attic_backup()
{
time env BORG_PASSPHRASE=${PASSWORD} ${ATTIC_PATH} create --compression lz4 ${ATTIC_STORAGE}::$1 ${BACKUP_DIR} --exclude-from ${BACKUP_DIR}/.duplicacy/attic-exclude
}

function duplicity_backup()
{
time ${DUPLICITY_PATH} -v0 --encrypt-key ${GPG_KEY} --sign-key ${GPG_KEY} --gpg-options "--compress-level=1" --exclude-filelist ${BACKUP_DIR}/.duplicacy/duplicity-exclude ${BACKUP_DIR} file://${DUPLICITY_STORAGE}
}

function all_backup()
{
echo ======================================== backup $1 ========================================
duplicacy_backup
restic_backup
attic_backup $1
duplicity_backup
du -sh ${TEST_DIR}/linux-*-storage
}

pushd ${BACKUP_DIR}

echo =========================================== init ========================================
rm -rf ${BACKUP_DIR}/.duplicacy
env DUPLICACY_PASSWORD=${PASSWORD} ${DUPLICACY_PATH} init test ${DUPLICACY_STORAGE} -e -c 1M
echo "-.git/" > ${BACKUP_DIR}/.duplicacy/filters

echo ".git/**" > ${BACKUP_DIR}/.duplicacy/restic-exclude
echo ".duplicacy/**" >> ${BACKUP_DIR}/.duplicacy/restic-exclude
env RESTIC_PASSWORD=${PASSWORD} ${RESTIC_PATH} -r ${RESTIC_STORAGE} init

echo "${BACKUP_DIR}/.git/*" > ${BACKUP_DIR}/.duplicacy/attic-exclude
echo "${BACKUP_DIR}/.duplicacy/*" >> ${BACKUP_DIR}/.duplicacy/attic-exclude
env BORG_PASSPHRASE=${PASSWORD} ${ATTIC_PATH} init -e repokey-blake2 ${ATTIC_STORAGE}

echo "- ${BACKUP_DIR}/.git" > ${BACKUP_DIR}/.duplicacy/duplicity-exclude
echo "- ${BACKUP_DIR}/.duplicacy" >> ${BACKUP_DIR}/.duplicacy/duplicity-exclude


du -sh ${TEST_DIR}/linux-*-storage

git checkout -f 4f302921c1458d790ae21147f7043f4e6b6a1085 # commit on 07/02/2016
all_backup 1

git checkout -f 3481b68285238054be519ad0c8cad5cc2425e26c # commit on 08/03/2016
all_backup 2

git checkout -f 46e36683f433528bfb7e5754ca5c5c86c204c40a # commit on 09/02/2016
all_backup 3

git checkout -f 566c56a493ea17fd321abb60d59bfb274489bb18 # commit on 10/05/2016
all_backup 4

git checkout -f 1be81ea5860744520e06d0dfb9e3490b45902dbb # commit on 11/01/2016
all_backup 5

git checkout -f ef3d232245ab7a1bf361c52449e612e4c8b7c5ab # commit on 12/02/2016
all_backup 6

git checkout -f 0e377f3b9ae936aefe5aaca4c2e2546d57b63df7 # commit on 01/05/2017
all_backup 7

git checkout -f cb23ebdfa6a491cf2173323059d846b4c5c9264e # commit on 02/04/2017
all_backup 8

git checkout -f 67db256ed1e09fa03551f90ab3562df34c802a0b # commit on 03/02/2017
all_backup 9

git checkout -f 1aed89640a899cd695bbfc976a4356affa474646 # commit on 04/05/2017
all_backup 10

git checkout -f a6128f47f7940d8388ca7c8623fbe24e52f8fae6 # commit on 05/05/2017
all_backup 11

git checkout -f 57caf4ec2b8bfbcb4f738ab5a12eedf3a8786045 # commit on 06/05/2017
all_backup 12

133 changes: 133 additions & 0 deletions linux-restore-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#!/bin/bash

#
# This script is to be run after linux-backup-test.sh. It will restore backups
# in TEST_DIR/linux-*-storage to TEST_DIR/linux-*-restore
#
# NOTE:
# Please make sure that this script doesn't run pass midnight, otherwise it
# would not be able to restore duplicity backups because it assumed backups were
# created on the same day.

if [ "$#" -eq 0 ]; then
echo "Usage: $0 <test dir>"
exit 1
fi

if [ -z "$DUPLICACY_PATH" ]; then
echo "DUPLICACY_PATH must be set to the path of the Duplicacy executable"
exit 1
fi

if [ -z "$RESTIC_PATH" ]; then
echo "RESTIC_PATH must be set to the path of the restic executable"
exit 1
fi

if [ -z "$ATTIC_PATH" ]; then
echo "ATTIC_PATH must be set to the path of the attic executable"
exit 1
fi

if [ -z "$DUPLICITY_PATH" ]; then
echo "DUPLICITY_PATH must be set to the path of the duplicity executable"
exit 1
fi

if [ -z "$GPG_KEY" ]; then
echo "GPG_KEY must be set for duplicity to work properly"
exit 1
fi

if [ -z "$PASSPHRASE" ]; then
echo "PASSPHRASE must be set for duplicity to work properly"
exit 1
fi

# Set up directories
TEST_DIR=$1
DUPLICACY_STORAGE=${TEST_DIR}/linux-duplicacy-storage
RESTIC_STORAGE=${TEST_DIR}/linux-restic-storage
ATTIC_STORAGE=${TEST_DIR}/linux-attic-storage
DUPLICITY_STORAGE=${TEST_DIR}/linux-duplicity-storage

DUPLICACY_RESTORE=${TEST_DIR}/linux-duplicacy-restore
RESTIC_RESTORE=${TEST_DIR}/linux-restic-restore
ATTIC_RESTORE=${TEST_DIR}/linux-attic-restore
DUPLICITY_RESTORE=${TEST_DIR}/linux-duplicity-restore

# Used as the storage password throughout the tests
PASSWORD=12345678

rm -rf ${DUPLICACY_RESTORE}
mkdir -p ${DUPLICACY_RESTORE}
rm -rf ${RESTIC_RESTORE}
mkdir -p ${RESTIC_RESTORE}
rm -rf ${ATTIC_RESTORE}
mkdir -p ${ATTIC_RESTORE}
rm -rf ${DUPLICITY_RESTORE}
mkdir -p ${DUPLICITY_RESTORE}

function duplicacy_restore()
{
rm -rf ${DUPLICACY_RESTORE}/*
pushd ${DUPLICACY_RESTORE}
time env DUPLICACY_PASSWORD=${PASSWORD} ${DUPLICACY_PATH} restore -r $1 -stats | grep -v Downloaded
popd
}


function restic_restore()
{
rm -rf ${RESTIC_RESTORE}/*
# We need to find the snapshot id to restore
TODAY=`date +"%Y-%m-%d"`
SNAPSHOT=`env RESTIC_PASSWORD=${PASSWORD} ${RESTIC_PATH} -r ${RESTIC_STORAGE} snapshots | grep $TODAY | head -n $1 | tail -n 1 | awk '{print $1;}'`
echo Restoring from $SNAPSHOT
time env RESTIC_PASSWORD=${PASSWORD} ${RESTIC_PATH} -r ${RESTIC_STORAGE} restore $SNAPSHOT --target ${RESTIC_RESTORE}
}

function attic_restore()
{
rm -rf ${ATTIC_RESTORE}/*
pushd ${ATTIC_RESTORE}
time env BORG_PASSPHRASE=${PASSWORD} ${ATTIC_PATH} extract ${ATTIC_STORAGE}::$1
popd
}

function duplicity_restore()
{
rm -rf ${DUPLICITY_RESTORE}/*
# duplicity is crazy -- the --restore-time option doesn't take the time format printed by its own colleciton-status command!
TODAY=`date +"%Y-%m-%d"`
RESTORE_TIME=`${DUPLICITY_PATH} -v0 --encrypt-key ${GPG_KEY} --sign-key ${GPG_KEY} collection-status file://${DUPLICITY_STORAGE} | grep 'Full\|Incremental' | head -n $1 | tail -n 1 | awk '{print $5;}'`
RESTORE_TIME=${TODAY}T${RESTORE_TIME}
echo Restoring from $RESTORE_TIME
time ${DUPLICITY_PATH} --force -v0 --encrypt-key ${GPG_KEY} restore -t $RESTORE_TIME file://${DUPLICITY_STORAGE} ${DUPLICITY_RESTORE}
}

function all_restore()
{

echo ======================================== restore $1 ========================================
duplicacy_restore $1
restic_restore $1
attic_restore $1
duplicity_restore $1
}

# Initialize the duplicacy directory to be restored
pushd ${DUPLICACY_RESTORE}
env DUPLICACY_PASSWORD=${PASSWORD} ${DUPLICACY_PATH} init test ${DUPLICACY_STORAGE} -e
popd

echo restic snapshots:
env RESTIC_PASSWORD=${PASSWORD} ${RESTIC_PATH} -r ${RESTIC_STORAGE} snapshots

echo duplicity archives:
${DUPLICITY_PATH} -v0 --encrypt-key ${GPG_KEY} --sign-key ${GPG_KEY} collection-status file://${DUPLICITY_STORAGE} | grep "Full\|Incremental"

for i in `seq 1 12`; do
all_restore $i
done

51 changes: 51 additions & 0 deletions tabulate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/python

import os
import sys
import re

#
# This script is written to extract elapsed times from linux-backup-test.sh or linux-restore-test.sh
#
# Usage:
#
# ./linux-backup-test.sh &> linux-backup-test.results
# python tabulate.py linux-backup-test.results

def getBackup(i):
l = ["Initial", "2nd", "3rd"]
if i < len(l):
return l[i] + " backup"
else:
return str(i + 1) + "th backup"

def getTime(minute, second):
t = int(minute) * 60 + float(second)
return "%.1f" % t

if len(sys.argv) <= 1:
print "usage:", sys.argv[0], "<test result file>"
sys.exit(1)

i = 0
for line in open(sys.argv[1]).readlines():
if line.startswith("====") and "init" not in line:
print "\n|", getBackup(i), "|",
i += 1
continue
m = re.match(r"real\s+(\d+)m([\d.]+)s", line)
if m:
print getTime(m.group(1), m.group(2)),
continue

m = re.match(r"user\s+(\d+)m([\d.]+)s", line)
if m:
print "(", getTime(m.group(1), m.group(2)), ",",
continue
m = re.match(r"sys\s+(\d+)m([\d.]+)s", line)
if m:
print getTime(m.group(1), m.group(2)), ") |",
continue

print ""

Loading

0 comments on commit da7ea1c

Please sign in to comment.