From f98a7e22699d39cc8752699d7ef8611aa850251c Mon Sep 17 00:00:00 2001 From: Marco Cetica Date: Tue, 14 Mar 2023 11:12:16 +0100 Subject: [PATCH] Added tests.sh script, Makefile and updated documentation --- Makefile | 15 ++++++++++ README.md | 16 ++++++++-- tests.sh | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 Makefile create mode 100755 tests.sh diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ea8f1b3 --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +all: + install + +install: + mkdir -p /usr/local/share/man/man1 + cp -R backup.sh /usr/local/bin/backup.sh + cp -R backup.source.bk /usr/local/etc/backup_sources.bk + cp -R backup.sh.1 /usr/local/share/man/man1/backup.sh.1 + chmod 755 /usr/local/bin/backup.sh + chmod 644 /usr/local/etc/backup_sources.bk + +uninstall: + rm -rf /usr/local/bin/backup.sh + rm -ff /usr/local/etc/backup_sources.bk + rm -rf /usr/local/share/man/man1/backup.sh.1 diff --git a/README.md b/README.md index 4222656..1215983 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,8 @@ Alternatively, you can install the script, the default sources file and the man $> sudo make install ``` This will copy `backup.sh` into `/usr/local/bin/backup.sh`, `backup_sources.bk` into `/usr/local/etc/backup_sources.bk` and -`backup.sh.1` into `/usr/local/share/man/man1`. +`backup.sh.1` into `/usr/local/share/man/man1`. To uninstall the program along with the sample _sources file_ and the manual page, +you can issue `sudo make uninstall`. At this point you still need to install the following dependencies: - `rsync` @@ -164,6 +165,17 @@ function; - `-salt`: **enable salting**: this option tells openssl to add a random salt to the key derivation process in order to avoid rainbow table based attacks. - ## Unit tests +`backup.sh` provides some unit tests inside the `tests.sh` script. This script generates some dummy files inside the following +directories: +- /var/log +- /var/www +- /etc/nginx +- /etc/ssh + +For this reason, this script should **NOT** be used in non-testing environments. To run all tests, issue the following command: +```sh +$> sudo ./tests.sh I_HAVE_READ_THE_HELPER +``` + ## License diff --git a/tests.sh b/tests.sh new file mode 100755 index 0000000..31cbe8c --- /dev/null +++ b/tests.sh @@ -0,0 +1,89 @@ +#!/bin/bash +# Unit tests for backup.sh +# This tool is NOT intended to be used outside +# of a testing environment, please use at your own risk. +# By Marco Cetica 2023 () +# + +set -e + +helper() { + cat < /etc/ssh/$file + done + + for file in index.html style.css logic.js ; do + head -c 1M /var/www/$file + done + + for file in nginx.conf fastcgi.conf mime.types ; do + head -c 1M /etc/nginx/$file + done + + for file in access.log error.log lastlog messages ; do + head -c 1M /var/log/$file + done +} + +execute_backup() { + ./backup.sh -b backup_sources.bk "$PWD" badpw +} + +extract_backup() { + ./backup.sh -e "$PWD"/backup-*-*.tar.gz.enc badpw +} + +test_backup() { + for dir in "$PWD/backup.sh.tmp/"backup-*-* ; do + if [ ! -d "$dir" ]; then + echo "Can't find '$dir' backup!" + exit 1 + else + echo "Found '$dir'" + fi + done +} + + +if [ $# -eq 0 ]; then + helper + exit 1 +fi + + +if [ "$1" = "I_HAVE_READ_THE_HELPER" ]; then + if [ "$(id -u)" -ne 0 ]; then + echo "Run this tool as root" + exit 1 + fi + + create_files + execute_backup + extract_backup + test_backup +fi