diff --git a/btrfs-snapshot/btrfs-snapshot b/btrfs-snapshot/btrfs-snapshot new file mode 100755 index 0000000..7ff0e87 --- /dev/null +++ b/btrfs-snapshot/btrfs-snapshot @@ -0,0 +1,151 @@ +#!/bin/bash +# By Thomas KUSCHEL +# MIT License 2015 +VERSION="v6.15" +set -o errexit # to make script exit when a command fails / a.k.a. set -e +# then add "|| true" to commands that you allow to fail. +set -o nounset # to exit when script tries to use undeclared variables / set -u +# set -o xtrace # to trace when gets executed for debugging / a.k.a. set -x +set -o pipefail +SCRIPTNAME=${0##*/} +SCRIPTPATH=$(dirname "$0") +# echo $SCRIPTNAME +# where the btrfs is found (use command: which btrfs) +WHICH="/usr/bin/which" +BTRFS_ALT="/sbin/btrfs" +BTRFS="/usr/local/bin/btrfs" +# Function to install this script: +installation() { +# you have to switch to administrator +#`${WHICH} ${PATHINSTALL}` +PATHINSTALL="/usr/local/bin" +SNAPSYSTEMDCFG="/etc/systemd/system/" +SCRIPTPATH=$(dirname "$0") +# echo "Installation of ${SCRIPTNAME} to ${PATHINSTALL} and create systemd timer/service examples in ${SNAPSYSTEMDCFG}" +read -n 1 -p "Install ${SCRIPTNAME} to ${PATHINSTALL}? [Y/n] " reply; + if [ "$reply" != "" ]; then echo; fi + if [ "$reply" = "${reply#[Nn]}" ]; then + sudo cp $0 ${PATHINSTALL}/${SCRIPTNAME} + fi + read -n 1 -p "Do you want to create systemd timer/service examples in ${SNAPSYSTEMDCFG}? [Y/n] " reply; + if [ "$reply" != "" ]; then echo; fi + if [ "$reply" = "${reply#[Nn]}" ]; then + sudo cp -a ${SCRIPTPATH}/system/. ${SNAPSYSTEMDCFG}. + path=`$WHICH $SCRIPTNAME` + fi +} + +deinstallation() { +PATHINSTALL="/usr/local/bin" +read -n 1 -p "Do you want to remove (deinstallation) of ${SCRIPTNAME} in ${PATHINSTALL}? [Y/n] " reply; + if [ "$reply" != "" ]; then echo; fi + if [ "$reply" = "${reply#[Nn]}" ]; then + sudo rm -f ${PATHINSTALL}/${SCRIPTNAME} + fi +} + +# Function to display usage: +usage() { +path=`${WHICH} ${SCRIPTNAME}` || installation +if [ -z "${path}" ] ; then + path="/usr/local/bin/" + echo "Please install $SCRIPTNAME to \"$path\"" + path="${path}${SCRIPTNAME}" +fi +cat </dev/null +done +# Create new snapshot: +cmd="$BTRFS subvolume snapshot -r $SOURCE $TARGET/`date "+%F-%H%M-@${SNAP}"`" +if [ -z $QUIET ]; then + echo $cmd +fi +$cmd >/dev/null diff --git a/btrfs-snapshot/system/snapshot_home_user_daily.service b/btrfs-snapshot/system/snapshot_home_user_daily.service new file mode 100644 index 0000000..9636e1e --- /dev/null +++ b/btrfs-snapshot/system/snapshot_home_user_daily.service @@ -0,0 +1,8 @@ +[Unit] +Description=rolling snapshot for /home/user +[Service] +Type=oneshot +Nice=19 +IOSchedulingClass=best-effort +IOSchedulingPriority=7 +ExecStart=/usr/local/bin/btrfs-snapshot /home/user /home/snap/user daily 4 diff --git a/btrfs-snapshot/system/snapshot_home_user_daily.timer b/btrfs-snapshot/system/snapshot_home_user_daily.timer new file mode 100644 index 0000000..c5602ec --- /dev/null +++ b/btrfs-snapshot/system/snapshot_home_user_daily.timer @@ -0,0 +1,9 @@ +[Unit] +Description=daily snapshot +[Timer] +# see systemd.time(7) manual page for other scheduling options +OnCalendar=01:00 +# run immediately if we missed a backup for some reason +Persistent=true +[Install] +WantedBy=timers.target