Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for btrfs snapshotsSnapshots #829

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .raspiBackup.sh.swp
Binary file not shown.
3 changes: 3 additions & 0 deletions config/raspiBackup_de.conf
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,6 @@ DEFAULT_PARTITIONS_TO_BACKUP="1 2"

# Name der Backuppartition die dynamisch gemounted werden soll (z.B. /dev/sda1 oder /backup), muss sonst leer sein um keinen dynamischen Mount zu benutzen
DEFAULT_DYNAMIC_MOUNT=""

# Snapshots benutzen
USE_FS_SNAPSHOTS="0"
3 changes: 3 additions & 0 deletions config/raspiBackup_en.conf
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,6 @@ DEFAULT_PARTITIONS_TO_BACKUP="1 2"

# Name of backup partition to dynamically mount (e.g. /dev/sda1 or /backup), should be empty to disable dynamic mount
DEFAULT_DYNAMIC_MOUNT=""

# Use FS Snapshots
DEFAULT_USE_FS_SNAPSHOTS="0"
46 changes: 46 additions & 0 deletions extensions/raspiBackup_snapshot_pre.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash
#######################################################################################################################
#
# Sample plugin for raspiBackup.sh
# called before a backup is started
#
# Function: stop services, create snapshot, start services again, unset the variables for STOP- and STARTSERVICES.
#
# See http://www.linux-tips-and-tricks.de/raspiBackup for additional information
#
#######################################################################################################################
#
# Copyright (c) 2015-2021 framp at linux-tips-and-tricks dot de
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.#
#
#######################################################################################################################

# we have STARTSERVICES and STOPSERVICES

export SNAPDEST="/.snapshot-raspiBackup-$BACKUPFILE"

# create the snapshot
btrfs subvolume snapshot / $SNAPDEST

# start services again
eval $STARTSERVICES

# Now the funny part: since we already took the snapshot, we don't need to start/stop the services during the actual backup (assumption: boot partition is stable)
# However, we want to remove the snapshot afterwards
export STOPSERVICES=""
export STARTSERVICES="btrfs subvolume delete $SNAPDEST"



41 changes: 40 additions & 1 deletion raspiBackup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ export BACKUP_TARGETDIR
export BACKUP_TARGETFILE
export MSG_FILE
export LOG_FILE
export STOPSERVICES
export STARTSERVICES

# Telegram options

Expand Down Expand Up @@ -1931,6 +1933,9 @@ MSG_DE[$MSG_SYNC_CMDLINE_FSTAB]="RBK0295I: %s und %s werden synchronisiert."
OVERLAY_FILESYSTEM_NOT_SUPPORTED=296
MSG_EN[$OVERLAY_FILESYSTEM_NOT_SUPPORTED]="RBK0296E: Overlay filesystem is not supported."
MSG_DE[$OVERLAY_FILESYSTEM_NOT_SUPPORTED]="RBK0296E: Overlayfilesystem wird nicht unterstützt."
MSG_EXTRA_MOUNT_OPTS=297
MSG_EN[$MSG_EXTRA_MOUNT_OPTS]="RBK0341I: Mount options for %s: %s."
MSG_DE[$MSG_EXTRA_MOUNT_OPTS]="RBK0341I: Mount Optionen für %s: %s."

declare -A MSG_HEADER=( ['I']="---" ['W']="!!!" ['E']="???" )

Expand Down Expand Up @@ -2904,6 +2909,8 @@ function initializeDefaultConfigVariables() {
DEFAULT_DYNAMIC_MOUNT=""
# Define bootdevice (e.g. /dev/mmcblk0, /dev/nvme0n1 or /dev/sda) and turn off boot device autodiscovery
DEFAULT_BOOT_DEVICE=""
# Use native filesystem snapshots
DEFAULT_USE_FS_SNAPSHOTS="0"
############# End default config section #############
}

Expand Down Expand Up @@ -2987,6 +2994,7 @@ function copyDefaultConfigVariables() {
YES_NO_RESTORE_DEVICE="$DEFAULT_YES_NO_RESTORE_DEVICE"
ZIP_BACKUP="$DEFAULT_ZIP_BACKUP"
DYNAMIC_MOUNT="$DEFAULT_DYNAMIC_MOUNT"
USE_FS_SNAPSHOTS="$DEFAULT_USE_FS_SNAPSHOTS"

checkImportantParameters

Expand Down Expand Up @@ -8172,6 +8180,24 @@ function updateRestoreReminder() {

logExit

}
function extraMountOptions() { # devid
logEntry "$1"
FSTYPE=$(findmnt -n -o FSTYPE "$1" || echo "none" )
if [ ! $FSTYPE == "btrfs" ] ; then
echo ""
logExit "$1 is $FSTYPE, not btrfs - skipping setting mount options"
return
fi
logItem "FS type of $1: $FSTYPE"
local mntpoint
mntpoint=$(cat /proc/mounts|grep $1 | awk '{ print $2; }')
if [ -z "$(btrfs subvol list $mntpoint|grep ${SNAPDEST#/})" ] ; then
echo ""
logExit
fi
echo "-o ro,subvol=$SNAPDEST"
logExit
}

function mountAndCheck() { # device mountpoint
Expand All @@ -8185,7 +8211,14 @@ function mountAndCheck() { # device mountpoint
exitError $RC_MISC_ERROR
fi
fi
mount "$1" "$2" &>>"$LOG_FILE"
if [ -n "$USE_FS_SNAPSHOTS" -a "$USE_FS_SNAPSHOTS" == "1" -a -n "$SNAPDEST" ]; then
mountOpts=$(extraMountOptions "$1")
writeToConsole $MSG_LEVEL_MINIMAL $MSG_EXTRA_MOUNT_OPTS "$1" "$mountOpts"
else
mountOpts=""
fi
mount $mountOpts "$1" "$2" &>>"$LOG_FILE" || mount -o ro "$1" "$2" &>>"$LOG_FILE" # try with mountopts, fallback to regular mount on fail

local rc=$?
if (( $rc )); then
writeToConsole $MSG_LEVEL_MINIMAL $MSG_MOUNT_CHECK_ERROR "$1" "$2" "$rc"
Expand Down Expand Up @@ -8827,6 +8860,7 @@ function usageEN() {
echo "-P use partitionoriented backup mode"
echo "-t {backupType} ($ALLOWED_TYPES) (default: $DEFAULT_BACKUPTYPE)"
echo "-T \"{List of partitions to save}\" (Partition numbers, e.g. \"1 2 3\"). Only valid with parameter -P (default: ${DEFAULT_PARTITIONS_TO_BACKUP})"
echo "--snapshots Use filesystem snapshots"
echo ""
echo "-Restore options-"
echo "-0 SD card will not be formatted"
Expand Down Expand Up @@ -8879,6 +8913,7 @@ function usageDE() {
echo "-P Nutzung des partitionsorientierten Backupmode"
echo "-t {Backuptyp} ($ALLOWED_TYPES) (Standard: $DEFAULT_BACKUPTYPE)"
echo "-T \"Liste der Partitionen die zu Sichern sind}\" (Partitionsnummern, z.B. \"1 2 3\"). Nur gültig zusammen mit Parameter -P (Standard: ${DEFAULT_PARTITIONS_TO_BACKUP})"
echo "--snapshots Snapshots des Filesystems benutzen
echo ""
echo "-Restore Optionen-"
echo "-0 Keine Formatierung der SD Karte"
Expand Down Expand Up @@ -9373,6 +9408,10 @@ while (( "$#" )); do
SMART_RECYCLE_OPTIONS="$o"; shift 2
;;

--snapshots|--snapshot)
USE_FS_SNAPSHOTS=$(getEnableDisableOption "$1"); shift 1
;;

--systemstatus|--systemstatus[+-])
SYSTEMSTATUS=$(getEnableDisableOption "$1"); shift 1
;;
Expand Down