From 2622223ee485c13d8b88ede6f36f47d10e7e0a70 Mon Sep 17 00:00:00 2001 From: david Date: Wed, 10 Feb 2021 19:07:59 +0100 Subject: [PATCH] Added v0.1 of restic_backups.sh --- README.md | 72 ++++++++++++++++++++++++++++-------------- restic_backup.sh | 81 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 23 deletions(-) create mode 100755 restic_backup.sh diff --git a/README.md b/README.md index 441ca6e..2a70025 100644 --- a/README.md +++ b/README.md @@ -2,36 +2,62 @@ My personal collection of handy scripts. mostly bash. -Content: - +- [restic_backup.sh](#restic_backup.sh) - [setup_zsh.sh](#setup_zsh.sh) -- [healthchecks](#healthchecks) + +## restic_backup.sh + +> This script is my very simple take on how to perform restic backups. + +First you need to download the script from this Git repository. I usually just put it in root's home. + +```bash +# Download +sudo curl -o /root/restic_backup.sh https://git.socialnerds.org/david/scripts/raw/branch/main/restic_backup.sh +sudo chmod +x /root/restic_backup.sh +``` + +In the config section at the top of the script you can change various options to your liking. + +```bash +# **** CONFIG **** +PASSWORD="/root/.restic-password" +REPO="sftp::/path/to/repository" +KEEP=30 +BIN="/usr/bin/restic" +OPTIONS="-p $PASSWORD -r $REPO -q" +``` + +You can run `restic_backup.sh` directly from the command line + +```bash +# Usage +/root/restic_backup.sh /local/path + +# Example +/root/restic_backup.sh /home -x -e ./.snapshots +``` + +or using cron. + +```bash +# Cronjob +47 0 * * * /root/restic_backup.sh /home + +# Cronjob (with healthchecks notification) +47 0 * * * /root/restic_backup.sh /home && curl -fsS -m 10 --retry 5 -o /dev/null https://checks.socialnerds.org/ping/fb721aec-5179-42c3-3455-4fd2ff39ii55 +``` + +Happy backuping! :-) + +--- ## setup_zsh.sh -One-line installer for zsh, oh-my-zsh and various zsh plugins. +> One-line installer for zsh, oh-my-zsh and various zsh plugins. ```bash curl -fsSL https://socialg.it/david/scripts/raw/master/setup_zsh.sh | bash ``` It is still highly experimental and will probably fail on your system. - ---- - -## healthchecks - -A collection of scripts for monitoring various things with [healthchecks.io](https://healthchecks.io). -My personal healthchecks instance runs at [checks.socialnerds.org](https://checks.socialnerds.org) though only the `/ping` endpoint is publicly available. The rest of the interface is limited to internal networks. -All healthchecks reside in their own repository at [socialg.it/david/healthchecks](https://socialg.it/david/healthchecks). - -| script | purpose | -| :------------ | :--------------------------------------------------------------- | -| system.sh | Fire if load, memory or diskusage hits the configured threshold | - ---- - -## setup_snmpd.sh - -`setup_snmpd.sh` installs and configures the snmp daemon to be used with [libreNMS](https://librenms.org). -Supported operating systems are Ubuntu, Debian, Raspbian and Archlinux. diff --git a/restic_backup.sh b/restic_backup.sh new file mode 100755 index 0000000..7e256a5 --- /dev/null +++ b/restic_backup.sh @@ -0,0 +1,81 @@ +#!/bin/bash + +# name: restic_backup.sh +# version: 0.1 +# author: david@socialnerds.org +# license: MIT +# description: a simple script to automate the restic backup utility. +# i created this for my personal use, though you are very +# welcome reuse and improve. + + +# **** CONFIG **** +PASSWORD="/root/.restic-password" +REPO="sftp:jupiter.socialner.ds:/srv/storage/restic-repositories/hood" +KEEP=30 +BIN="/usr/bin/restic" +OPTIONS="-p $PASSWORD -r $REPO -q" + + +# **** START **** +CMD="$BIN -p $PASSWORD -r $REPO $OPTIONS" + +#TODO: check for elevated privileges + +# Generate a new password file if it is missing or empty +if [ ! -s $PASSWORD ]; then + #echo "error: Password file is empty or does not exist. [$PASSWORD]" + #exit 1 + tr -cd '[:alnum:]' < /dev/urandom | fold -w32 | head -n1 > $PASSWORD + chmod 600 $PASSWORD + echo "warning: New password file generated since it does not exist or is empty. [$PASSWORD]" +fi + +# Initialize a new repo if the repo url has no config file +$CMD snapshots >/dev/null 2>&1 +if [ $? -ne 0 ]; then + $CMD init >/dev/null 2>&1 + if [ $? -ne 0 ]; then + echo "error: A problem occured while creating a new repository. [$REPO]" + exit 1 + fi + echo "warning: New repository initialized. [$REPO]" +fi + +# Run the restic repository check +$CMD check > /dev/null +if [ $? -ne 0 ]; then + echo "error: Repository check failed. [$REPO]" + exit 1 +fi + +# First option must be a readable folder/file +if [ $1 ]; then + if [ -r $1 ]; then + LOCAL=$1 + else + echo "error: The path given does not seem to be readable. [$1]" + exit 1 + fi +else + echo "error: Local path must be given as an option." + exit 1 +fi + +# Run the actual backup +$CMD backup ${@:2} $LOCAL > /dev/null +if [ $? -ne 0 ]; then + echo "error: Something went wrong while running backup. [$LOCAL]" + exit 1 +fi + +# Delete old snapshots +$CMD forget --keep-last $KEEP --path $LOCAL --prune > /dev/null +if [ $? -ne 0 ]; then + echo "error: Something went wrong while deleting old snapshots. [$LOCAL]" + exit 1 +fi + + +# **** END **** +exit 0