]> arthur.barton.de Git - backup-script.git/commitdiff
Add support for generations on ZFS
authorAlexander Barton <alex@barton.de>
Wed, 29 Jul 2015 10:31:58 +0000 (12:31 +0200)
committerAlexander Barton <alex@barton.de>
Wed, 29 Jul 2015 10:31:58 +0000 (12:31 +0200)
README.md
bin/backup-script

index 81201368630d718253fe0d65b28e83720e4d95d0..fbeeab070fa9e0a52bce12a1be4f350bf9395116 100644 (file)
--- a/README.md
+++ b/README.md
@@ -113,11 +113,22 @@ Default: 0 (off; use ssh).
 
 Number of generations to keep. Default: 0 (none).
 
-On a *btrfs* target file systems (see `target` variable), this script can
-generate generations using *btrfs snapshots*: the script creates a new snapshot
+On a suitable target file systems (see `target` variable), this script can
+generate generations using snapshots: the script creates a new snapshot
 named with the timestamp for each generation inside of the system directory
 inside of the target directory.
 
+Supported file systems are:
+
+ * *btrfs*:
+   All generations are btrfs subvolumes and named after the date and time.
+ * *ZFS*:
+   All generations are ZFS filesystems. Latest generation is named `current`,
+   olders are links to the ZFS snapshot directories.
+
+The latest snapshot is always reachable using a symlink named `latest`
+inside the system directory.
+
 ### [default_]job_pre_exec
 
 Optional script to execute before `rsync` starts. Default: none.
index 37cbc53bd9d2da49d6b3ed9b01cf00b876665f91..5df6c6710bd58d1a803c31262785f6ca8690bad2 100755 (executable)
@@ -122,6 +122,9 @@ CreateSubvolume() {
          "btrfs")
                btrfs subvolume create "$volume"  >/dev/null || return 1
                ;;
+         "zfs")
+               zfs create "$(echo "$volume" | cut -c2-)" || return 1
+               ;;
          *)
                echo "CreateSubvolume: Incompatible FS type \"$fs\" on \"$dir\"!"
                return 9
@@ -135,6 +138,7 @@ CloneSubvolume() {
        local snapshot="$3"
        local dir
        local fs
+       local link_name
 
        dir=$(dirname "source")
        fs=$(GetFS "$source")
@@ -142,6 +146,13 @@ CloneSubvolume() {
          "btrfs")
                btrfs subvolume snapshot "$source" "$snapshot"  >/dev/null || return 1
                ;;
+         "zfs")
+               zfs snapshot "$snapshot" || return 1
+               link_name="$(echo "$snapshot" | cut -d@ -f2-)"
+               ln -s \
+                       "$volume/.zfs/snapshot/$link_name" \
+                       "$(dirname "$volume")/$link_name"
+               ;;
          *)
                echo "CloneSubvolume: Incompatible FS type \"$fs\" on \"$source\"!"
                return 9
@@ -159,6 +170,12 @@ RenameSubvolume() {
          "btrfs")
                mv "$source" "$target" || return 1
                ;;
+         "zfs")
+               zfs rename \
+                 "$(echo "$source" | cut -c2-)" \
+                 "$(echo "$target" | cut -c2-)" \
+                       || return 1
+               ;;
          *)
                echo "RenameSubvolume: Incompatible FS type \"$fs\" on \"$source\"!"
                return 9
@@ -169,12 +186,24 @@ RenameSubvolume() {
 DeleteSubvolume() {
        local volume="$1"
        local fs
+       local id
+       local snapshot
 
        fs=$(GetFS "$volume")
        case "$fs" in
          "btrfs")
                btrfs subvolume delete "$volume" >/dev/null || return 1
                ;;
+         "zfs")
+               id="$(basename "$volume")"
+               if [ -h "$volume" ]; then
+                       snapshot="$(dirname "$volume")/current@$id"
+               else
+                       snapshot="$volume"
+               fi
+               zfs destroy -r "$(echo "$snapshot" | cut -c2-)" >/dev/null || return 1
+               [ -h "$volume" ] && rm "$volume"
+               ;;
          *)
                echo "DeleteSubvolume: Incompatible FS type \"$fs\" on \"$volume\"!"
                return 9
@@ -202,6 +231,14 @@ Initialize_Last_SysTarget_Snapshot() {
                sys_target="$sys_target/$(date +%Y%m%d-%H%M%S)"
                snapshot="$sys_target"
                ;;
+         "zfs")
+               # On ZFS, the last generation is always named "current"
+               [ -e "$sys_target/current" ] \
+                       && last="$sys_target/current" \
+                       || last=""
+               snapshot="$(echo "$sys_target/current" | cut -c2-)@$(date +%Y%m%d-%H%M%S)"
+               sys_target="$sys_target/current"
+               ;;
          *)
                echo "Initialize_Last_SysTarget_Snapshot: Incompatible FS type \"$fs\" on \"$sys_target\"!"
                return 1