82 lines
2.0 KiB
Bash
Executable File
82 lines
2.0 KiB
Bash
Executable File
#!/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
|