Started rewrite of btrfs-snapshots.sh
This commit is contained in:
parent
704340d796
commit
d268b96a4c
@ -18,15 +18,16 @@ More:
|
||||
|
||||
> 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.
|
||||
### Install
|
||||
|
||||
```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
|
||||
sudo su; cd
|
||||
git clone https://git.socialnerds.org/david/scripts.git
|
||||
ln -s /root/scripts/restic-backup.sh /usr/local/bin/restic-backup
|
||||
```
|
||||
|
||||
In the config section at the top of the script you can change various options to your liking.
|
||||
|
||||
|
||||
```bash
|
||||
# **** CONFIG ****
|
||||
|
@ -1,28 +1,102 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# this will take read-only btrfs snapshots and make it available in /srv/snapshots
|
||||
|
||||
# Information
|
||||
## ## ## ## ## ## ## ## ## ## ## ## ## ##
|
||||
## ##
|
||||
## btrfs-snapshots.sh ##
|
||||
## ##
|
||||
## Take read-only snapshots of BTRFS ##
|
||||
## subvolumes ##
|
||||
## ##
|
||||
## ## ## ## ## ## ## ## ## ## ## ## ## ##
|
||||
|
||||
|
||||
##
|
||||
## Information
|
||||
## (don't touch unless you are me)
|
||||
##
|
||||
|
||||
NAME="btrfs-snapshots.sh"
|
||||
VERSION="0.2.0"
|
||||
AUTHOR="david@socialnerds.org"
|
||||
LICENSE="MIT"
|
||||
DESCRIPTION="$NAME (v$VERSION) takes snapshots of BTRFS volumes. It is written by $AUTHOR and published unter the $LICENSE license."
|
||||
DESCRIPTION="Take read-only snapshots of BTRFS subvolumes."
|
||||
WEBSITE="https://git.socialnerds.org/david/scripts"
|
||||
CHANGELOG=("[2023-10-26][v0.2.0] Complete rewrite"
|
||||
"[2021-01-01][v0.1.0] Initial version")
|
||||
|
||||
|
||||
##
|
||||
## Configuration
|
||||
##
|
||||
|
||||
EXECUTABLE="$(basename $0)"
|
||||
LIBRARIES="lib.sh"
|
||||
DEPENDENCIES="basename dirname readlink ln mkdir rm ls date btrfs"
|
||||
REQUIRE_ROOT=1
|
||||
|
||||
# variables
|
||||
SUBVOLUMES="/"
|
||||
DESTINATION_ROOT=/srv/snapshots
|
||||
TIMESTAMP=$(date +%Y%m%d%H%M) #add %S if you want sub-minute snapshots
|
||||
PREFIX="snapshot-"
|
||||
NAME="$PREFIX$TIMESTAMP"
|
||||
MAX_SNAPSHOTS=28 #how many snapshots should be kept?
|
||||
|
||||
# preflight checks
|
||||
if [ $(whoami) != "root" ]; then
|
||||
echo "error: you need to be root"
|
||||
PREFIX="snapshot-"
|
||||
TIMESTAMP=$(date +%Y%m%d%H%M) #add %S if you want sub-minute snapshots
|
||||
NAME="$PREFIX$TIMESTAMP"
|
||||
|
||||
# How many snapshots should be kept?
|
||||
MAX_SNAPSHOTS=32
|
||||
|
||||
|
||||
##
|
||||
## Functions
|
||||
##
|
||||
|
||||
# Print help information
|
||||
function print_help() {
|
||||
printf "%s\n\n%s\n%b\n\n%s\n %-15s %s\n %-15s %s\n %-15s %s\n %-15s %s\n" \
|
||||
"$DESCRIPTION" "Usage:" "$LIB_BOLD$EXECUTABLE <options> <subvolume>$LIB_CLEAR" "Options:" \
|
||||
"-h, --help" "Print help screen and exit" \
|
||||
"-i, --info" "Print script information and exit" \
|
||||
"-v, --verbose" "More verbose output" \
|
||||
"-q, --quiet" "No output except errors (overrides -v)"
|
||||
}
|
||||
|
||||
|
||||
##
|
||||
## Preflight
|
||||
##
|
||||
|
||||
# Load BASH libraries
|
||||
SCRIPT_PATH=$(readlink -f "$0")
|
||||
SCRIPT_DIR=$(dirname "$SCRIPT_PATH")
|
||||
for LIBRARY in $LIBRARIES; do
|
||||
if [[ -r "$SCRIPT_DIR/$LIBRARY" ]]; then
|
||||
#echo "Loading library file [$SCRIPT_DIR/$LIBRARY]"
|
||||
source "$SCRIPT_DIR/$LIBRARY"
|
||||
else
|
||||
echo "Error: Cannot load library file [$SCRIPT_DIR/$LIBRARY]"
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
# Check for root privileges
|
||||
if ! lib_amiroot && [[ $REQUIRE_ROOT -eq 1 ]]; then
|
||||
lib_print "!You need to have root privileges"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check for dependencies
|
||||
MISSING_COMMANDS=$(lib_missing_commands $DEPENDENCIES)
|
||||
if [[ -n "$MISSING_COMMANDS" ]]; then
|
||||
lib_print "!One or more commands missing [$MISSING_COMMANDS]"
|
||||
lib_print "Try installing them with your package manager"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
||||
##
|
||||
## Liftoff
|
||||
##
|
||||
exit 0
|
||||
# take the snapshots
|
||||
for SUBVOLUME in $SUBVOLUMES; do
|
||||
if [ -z ${SUBVOLUME#/} ]; then
|
||||
@ -46,7 +120,7 @@ for SUBVOLUME in $SUBVOLUMES; do
|
||||
fi
|
||||
done
|
||||
|
||||
# prune old snapshots
|
||||
# Delete old snapshots
|
||||
SNAPSHOTS=$(ls -r $SNAPSHOT_PATH | grep $PREFIX)
|
||||
i=0
|
||||
for SNAPSHOT in $SNAPSHOTS; do
|
||||
@ -57,5 +131,9 @@ for SNAPSHOT in $SNAPSHOTS; do
|
||||
i=$((i+1))
|
||||
done
|
||||
|
||||
# end script successfully
|
||||
|
||||
##
|
||||
## Here be dragons
|
||||
##
|
||||
|
||||
exit 0
|
||||
|
Loading…
Reference in New Issue
Block a user