diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..06e0598 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +deb/ diff --git a/README.md b/README.md index c8cca39..27add21 100644 --- a/README.md +++ b/README.md @@ -6,17 +6,22 @@ workstations. `backup.sh` uses [rsync](https://linux.die.net/man/1/rsync), [tar] to copy, compress, encrypt the backup and verify the backup. ## Installation -`backup.sh` consists in a single source file, to install it you can copy the script wherever you want. -Alternatively, you can install the script, the default sources file and the man file using the following command: +`backup.sh` is a single source file, to install it you can copy the script wherever you want. Alternatively, if you +are running a DEB/RPM distribution, you can install it with the following command: +```sh +$> sudo apt install ./bin/backup.sh-1.0.0.x86_64.deb # Debian +$> sudo dnf install ./bin/backup.sh-1.0.0-2.x86_64.rpm # RHEL +``` + +For any other UNIX system, you can use the following command: ```sh $> sudo make install ``` This will copy `backup.sh` into `/usr/local/bin/backup.sh`, `sources.bk` into `/usr/local/etc/sources.bk` and -`backup.sh.1` into `/usr/share/man/man1/backup.sh.1`. To uninstall the program along with the sample _sources file_ and the manual page, -you can issue `sudo make uninstall`. +`backup.sh.1` into `/usr/share/man/man1/backup.sh.1`. 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: -- `Bash` +- `Bash(v>=4)` - `rsync` - `tar` - `gpg` @@ -24,13 +29,14 @@ At this point you still need to install the following dependencies: ## Usage To show the available options, you can run `backup.sh --help`, which will print out the following message: ```text -backup.sh - POSIX compliant, modular and lightweight backup utility. +backup.sh v1.0.0 - POSIX compliant, modular and lightweight backup utility. -Syntax: ./backup.sh [-b|-c|-e|-h] +Syntax: ./backup.sh [-b|-e|-c|-V|-h] options: -b|--backup SOURCES DEST PASS Backup folders from SOURCES file. --c|--checksum Generate/check SHA256 of a backup. -e|--extract ARCHIVE PASS Extract ARCHIVE using PASS. +-c|--checksum Generate/check SHA256 of a backup. +-V|--verbose Enable verbose mode. -h|--help Show this helper. General help with the software: https://github.com/ceticamarco/backup.sh @@ -121,6 +127,8 @@ This will automatically run `backup.sh` every Saturday morning at 03:30 AM. In t key is stored in a local file(with fixed permissions) to avoid password leaking in crontab logs. You can also adopt this practice while using the `--extract` option to avoid password leaking in shell history. +By default `backup.sh` is very quiet, to add some verbosity to the output, be sure to use the `-V`(`--verbose`) option. + ### Backup extraction `backup.sh` can also be used to extract and to verify the encrypted backup. To do so, use the following commands: diff --git a/backup.sh b/backup.sh index 55f1e44..ae7c3ad 100755 --- a/backup.sh +++ b/backup.sh @@ -37,6 +37,11 @@ set -e checkdeps() { + if [ "${BASH_VERSINFO[0]}" -lt 4 ]; then + echo "This version of Bash is not supported." + exit 1 + fi + # Check if dependencies are installed missing_dep=0 deps="rsync tar gpg" @@ -66,13 +71,20 @@ gethash() { # $2: output path # $3: password # $4: compute sha256(0,1) +# $5: verbosity flag(0,1) make_backup() { BACKUP_SH_SOURCES_PATH="$1" BACKUP_SH_OUTPATH="$2" BACKUP_SH_PASS="$3" BACKUP_SH_SHA256="$4" + BACKUP_SH_VERBOSE="$5" + + if [ "$BACKUP_SH_VERBOSE" -eq 1 ]; then + BACKUP_SH_COMMAND="rsync -aPhr --delete" + else + BACKUP_SH_COMMAND="rsync -aPhrq --delete" + fi - BACKUP_SH_COMMAND="rsync -aPhrq --delete" BACKUP_SH_DATE="$(date +'%Y%m%d')" BACKUP_SH_FOLDER="backup.sh.tmp" BACKUP_SH_OUTPUT="$BACKUP_SH_OUTPATH/$BACKUP_SH_FOLDER" @@ -119,8 +131,13 @@ make_backup() { # Compress backup directory echo "Compressing backup..." - tar -czf "$BACKUP_SH_OUTPATH/backup.sh.tar.gz" \ - -C "$BACKUP_SH_OUTPATH" "$BACKUP_SH_FOLDER" > /dev/null 2>&1 + if [ "$BACKUP_SH_VERBOSE" -eq 1 ]; then + tar -cvzf "$BACKUP_SH_OUTPATH/backup.sh.tar.gz" \ + -C "$BACKUP_SH_OUTPATH" "$BACKUP_SH_FOLDER" + else + tar -czf "$BACKUP_SH_OUTPATH/backup.sh.tar.gz" \ + -C "$BACKUP_SH_OUTPATH" "$BACKUP_SH_FOLDER" > /dev/null 2>&1 + fi # Encrypt backup directory echo "Encrypting backup..." @@ -151,10 +168,12 @@ make_backup() { # $1: archive file # $2: archive password # $3: sha256 file(optional) +# $4: verbosity flag(0,1) extract_backup() { BACKUP_SH_ARCHIVE_PATH="$1" BACKUP_SH_ARCHIVE_PW="$2" BACKUP_SH_SHA256_FILE="$3" + BACKUP_SH_VERBOSE="$4" # Decrypt the archive gpg -a \ @@ -167,7 +186,11 @@ extract_backup() { "$BACKUP_SH_ARCHIVE_PATH" # Extract archive - tar -xzf backup.sh.tar.gz 1> /dev/null 2>&1 + if [ "$BACKUP_SH_VERBOSE" -eq 1 ]; then + tar -xzvf backup.sh.tar.gz + else + tar -xzf backup.sh.tar.gz > /dev/null 2>&1 + fi # If specified, use SHA256 file to compute checksum of files if [ -n "$BACKUP_SH_SHA256_FILE" ]; then @@ -179,10 +202,11 @@ extract_backup() { SHA256="$(gethash "$file")" # Check if checksum file contains hash if ! grep -wq "$SHA256" "$BACKUP_SH_SHA256_FILE"; then - printf "[FATAL] - integrity error for '%s'.\n" "$file" rm -rf backup.sh.tar.gz backup.sh.tmp + printf "[FATAL] - integrity error for '%s'.\n" "$file" exit 1 fi + printf "[OK] - integrity check for '%s' passed.\n" "$file" done shopt -u globstar dotglob fi @@ -194,13 +218,14 @@ helper() { CLI_NAME="$1" cat <) +.EE .SH DESCRIPTION -.PP \f[B]backup.sh\f[R] is a POSIX compliant, modular and lightweight backup utility to save and encrypt your files. This tool is intended to be used on small scale UNIX environment such as @@ -42,7 +27,6 @@ VPS, small servers and workstations. \f[I]sha256sum\f[R] and \f[I]gpg\f[R] to copy, compress, verify and encrypt the backup. .SH OPTIONS -.PP \f[B]backup.sh\f[R] supports three options: \f[B]backup creation\f[R], \f[B]backup extraction\f[R] and \f[B]checksum\f[R] to verify the integrity of a backup. @@ -51,163 +35,142 @@ not. The checksum option must be used in combination of one of the previous options. .SS Backup creation -.PP -To specify the directories to back up, \f[V]backup.sh\f[R] uses an +To specify the directories to back up, \f[CR]backup.sh\f[R] uses an associative array defined in a text file(called \f[I]sources file\f[R]) with the following syntax: .IP -.nf -\f[C] +.EX