From 2bde7ce22406e0232e513235c4317cb762a24c6e Mon Sep 17 00:00:00 2001
From: Jarno Rankinen <50285623+0ranki@users.noreply.github.com>
Date: Sat, 1 Aug 2020 09:52:21 +0300
Subject: [PATCH 1/7] Update README.md
Added requirements
---
README.md | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/README.md b/README.md
index fbb6230..2bcd3a5 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,7 @@
# snapsh
-Btrfs snapshot managing script
+Btrfs snapshot managing bash script
+
+###### Requirements:
+- `bash`
+- GNU `setopt` - part of GNU `coreutils`
+- `btrfs-progs`- Userspace programs for btrfs
--
2.40.1
From 4d4dcb311bf4edf0db0335d063a4c588cad3c80b Mon Sep 17 00:00:00 2001
From: Jarno Rankinen <50285623+0ranki@users.noreply.github.com>
Date: Sat, 1 Aug 2020 20:19:47 +0300
Subject: [PATCH 2/7] Update README.md
Added configuration and basic snapshotting instructions
---
README.md | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/README.md b/README.md
index 2bcd3a5..a2adbb4 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,13 @@
# snapsh
Btrfs snapshot managing bash script
-###### Requirements:
+### Requirements:
- `bash`
- GNU `setopt` - part of GNU `coreutils`
- `btrfs-progs`- Userspace programs for btrfs
+
+### Instructions:
+- Script needs the toplevel subvolume (id=5) mounted somewhere. Default location is `/root/btrfs-toplevel`, but you can mount it anywhere you like and define it with `TOPLEVEL` variable. (A separate config file will be implemented later).
+- Will create a subvolume named snapshots by default to the toplevel. This can also be changed with `SNAPSHOTS_LOCATION`.
+- Display usage instructions with `snapsh -h` or `snapsh --help`
+- Taking snapshots requires root priviledges. Take a snapshot with `snapsh -s SUBVOLUME` or `snapsh --snapshot SUBVOLUME`, where `SUBVOLUME` is the name of the source subvolume. Example with Fedora default btrfs layout with `root` and `home` subvolumes:
`snapsh -s root`
This will create a snapshot called `root_snapshot_YYYY.MM.DD-hh:mm:ss` to the `snapshots` subvolume (or the one you defined with `SNAPSHOTS_LOCATION`)
--
2.40.1
From 3a1a0c9800a265e1ac18e81f5111b0577659c43a Mon Sep 17 00:00:00 2001
From: Jarno Rankinen <50285623+0ranki@users.noreply.github.com>
Date: Sat, 1 Aug 2020 20:43:54 +0300
Subject: [PATCH 3/7] Update README.md
Instructions for adding description added
---
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/README.md b/README.md
index a2adbb4..cf65405 100644
--- a/README.md
+++ b/README.md
@@ -10,4 +10,4 @@ Btrfs snapshot managing bash script
- Script needs the toplevel subvolume (id=5) mounted somewhere. Default location is `/root/btrfs-toplevel`, but you can mount it anywhere you like and define it with `TOPLEVEL` variable. (A separate config file will be implemented later).
- Will create a subvolume named snapshots by default to the toplevel. This can also be changed with `SNAPSHOTS_LOCATION`.
- Display usage instructions with `snapsh -h` or `snapsh --help`
-- Taking snapshots requires root priviledges. Take a snapshot with `snapsh -s SUBVOLUME` or `snapsh --snapshot SUBVOLUME`, where `SUBVOLUME` is the name of the source subvolume. Example with Fedora default btrfs layout with `root` and `home` subvolumes:
`snapsh -s root`
This will create a snapshot called `root_snapshot_YYYY.MM.DD-hh:mm:ss` to the `snapshots` subvolume (or the one you defined with `SNAPSHOTS_LOCATION`)
+- Taking snapshots requires root priviledges. Take a snapshot with `snapsh -s SUBVOLUME` or `snapsh --snapshot SUBVOLUME`, where `SUBVOLUME` is the name of the source subvolume. You can add a description for the snapshot with the `-d | --description` option (must be used before the `-s` option)
Example with Fedora default btrfs layout with `root` and `home` subvolumes:
`snapsh -d "This is a snapshot" -s root`
This will create a snapshot called `root_snapshot_YYYY.MM.DD-hh:mm:ss` to the `snapshots` subvolume (or the one you defined with `SNAPSHOTS_LOCATION`), with a description "This is a snapshot"
--
2.40.1
From 8db86b51ec9245477d7c0d58223c2f62ca01563e Mon Sep 17 00:00:00 2001
From: Jarno Rankinen <50285623+0ranki@users.noreply.github.com>
Date: Sun, 2 Aug 2020 20:32:41 +0300
Subject: [PATCH 4/7] Update README.md
Added list option description
---
README.md | 1 +
1 file changed, 1 insertion(+)
diff --git a/README.md b/README.md
index cf65405..0c00b14 100644
--- a/README.md
+++ b/README.md
@@ -11,3 +11,4 @@ Btrfs snapshot managing bash script
- Will create a subvolume named snapshots by default to the toplevel. This can also be changed with `SNAPSHOTS_LOCATION`.
- Display usage instructions with `snapsh -h` or `snapsh --help`
- Taking snapshots requires root priviledges. Take a snapshot with `snapsh -s SUBVOLUME` or `snapsh --snapshot SUBVOLUME`, where `SUBVOLUME` is the name of the source subvolume. You can add a description for the snapshot with the `-d | --description` option (must be used before the `-s` option)
Example with Fedora default btrfs layout with `root` and `home` subvolumes:
`snapsh -d "This is a snapshot" -s root`
This will create a snapshot called `root_snapshot_YYYY.MM.DD-hh:mm:ss` to the `snapshots` subvolume (or the one you defined with `SNAPSHOTS_LOCATION`), with a description "This is a snapshot"
+- Snapshots can be listed with `snapsh -l` or `snapsh --list`
--
2.40.1
From 8e4de6a074d08195d8ebdc1ea3315fdcc72a1ee2 Mon Sep 17 00:00:00 2001
From: Jarno Rankinen <50285623+0ranki@users.noreply.github.com>
Date: Sun, 2 Aug 2020 21:47:27 +0300
Subject: [PATCH 5/7] Update README.md
Added information for snapshot deletion.
---
README.md | 1 +
1 file changed, 1 insertion(+)
diff --git a/README.md b/README.md
index 0c00b14..6c89ef2 100644
--- a/README.md
+++ b/README.md
@@ -12,3 +12,4 @@ Btrfs snapshot managing bash script
- Display usage instructions with `snapsh -h` or `snapsh --help`
- Taking snapshots requires root priviledges. Take a snapshot with `snapsh -s SUBVOLUME` or `snapsh --snapshot SUBVOLUME`, where `SUBVOLUME` is the name of the source subvolume. You can add a description for the snapshot with the `-d | --description` option (must be used before the `-s` option)
Example with Fedora default btrfs layout with `root` and `home` subvolumes:
`snapsh -d "This is a snapshot" -s root`
This will create a snapshot called `root_snapshot_YYYY.MM.DD-hh:mm:ss` to the `snapshots` subvolume (or the one you defined with `SNAPSHOTS_LOCATION`), with a description "This is a snapshot"
- Snapshots can be listed with `snapsh -l` or `snapsh --list`
+- Delete snapshots with the `-r` or `--remove` option. List snapshots first with `snapsh -l`, then delete snapshot with e.g. `snapsh -r 2`, where 2 is the number of the deletable snapshot in the `-l` listing. The list numbers always start from 1 and increment from there, so always check the number before deletion. Batch deletion might be implemented later.
--
2.40.1
From 5870065f0cefb0aa1c3436d6b190e98a923d5c51 Mon Sep 17 00:00:00 2001
From: Jarno Rankinen <50285623+0ranki@users.noreply.github.com>
Date: Sun, 2 Aug 2020 22:01:49 +0300
Subject: [PATCH 6/7] Update README.md
Added planned features and license
---
README.md | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/README.md b/README.md
index 6c89ef2..e8387f7 100644
--- a/README.md
+++ b/README.md
@@ -13,3 +13,16 @@ Btrfs snapshot managing bash script
- Taking snapshots requires root priviledges. Take a snapshot with `snapsh -s SUBVOLUME` or `snapsh --snapshot SUBVOLUME`, where `SUBVOLUME` is the name of the source subvolume. You can add a description for the snapshot with the `-d | --description` option (must be used before the `-s` option)
Example with Fedora default btrfs layout with `root` and `home` subvolumes:
`snapsh -d "This is a snapshot" -s root`
This will create a snapshot called `root_snapshot_YYYY.MM.DD-hh:mm:ss` to the `snapshots` subvolume (or the one you defined with `SNAPSHOTS_LOCATION`), with a description "This is a snapshot"
- Snapshots can be listed with `snapsh -l` or `snapsh --list`
- Delete snapshots with the `-r` or `--remove` option. List snapshots first with `snapsh -l`, then delete snapshot with e.g. `snapsh -r 2`, where 2 is the number of the deletable snapshot in the `-l` listing. The list numbers always start from 1 and increment from there, so always check the number before deletion. Batch deletion might be implemented later.
+
+### Planned features:
+- separate config file
+- `--install` option to integrate script into system
+- Batch deletion of snapshots
+- `systemd` timer for automating snapshots
+- `--auto` option (quiet) for automated snapshots
+- Option to snapshot multiple/all subvolumes
+
+Root access is required for `btrfs-progs`.
+
+### License:
+GPL v3.0
--
2.40.1
From 5910ad5f80214433fae94f433bce2cf81807dbaf Mon Sep 17 00:00:00 2001
From: Jarno Rankinen
Date: Wed, 5 Aug 2020 00:23:28 +0300
Subject: [PATCH 7/7] Rollbacks implemented
Rollback (--rollback) implemented. Requires tidying up printouts.
Fixed issue with using a larger number than the number of snapshots
for "--remove"
---
snapsh | 92 +++++++++++++++++++++++++++++++++++++++++++++++++---------
1 file changed, 79 insertions(+), 13 deletions(-)
diff --git a/snapsh b/snapsh
index e685614..97ddcbb 100755
--- a/snapsh
+++ b/snapsh
@@ -25,7 +25,6 @@ BTRFS_EXECUTABLE=$(which btrfs)
TIMESTAMP=$(date +%Y.%m.%d-%H:%M:%S)
SUBVOLUME="root"
DESCRIPTION=""
-REMOVE_TARGET=""
@@ -41,7 +40,10 @@ Options:
-s SUBVOL, --snapshot SUBVOL Take a snapshot of subvolume named SUBVOL.
-l, --list List snapshots
-r NUMBER, --remove NUMBER Remove snapshot NUMBER. See snapshot numbers with
- snapsh -l\n"
+ snapsh -l
+ --rollback NUMBER Roll back to snapshot NUMBER. See snapshot numbers
+ with snapsh -l. Target subvolume is detected from
+ snapshot automatically.\n"
}
@@ -109,20 +111,19 @@ list() {
remove() {
root_check
SNAPSHOTS=(${SNAPSHOTS_LOCATION}/*/)
+
+ if [[ "${REMOVE_TARGET}" -gt "${#SNAPSHOTS[@]}" ]]; then
+ printf "Snapshot number ${REMOVE_TARGET} does not exist.\n"
+ exit 1
+ elif [[ "${REMOVE_TARGET}" -lt 1 ]]; then
+ printf "Number must be greater than 0.\n"
+ exit 1
+ fi
+
let INDEX=${REMOVE_TARGET}-1
TARGET=${SNAPSHOTS[${INDEX}]}
. ${TARGET}/.snapsh
- if [[ "${INDEX}" -gt "${#SNAPSHOTS[@]}" ]]; then
- printf "Snapshot number ${REMOVE_TARGET} does not exist.\n"
- list
- exit 1
- elif [[ "${REMOVE_TARGET}" -lt 1 ]]; then
- printf "Number must be a positive integer.\n"
- list
- exit 1
- fi
-
printf "Delete snapshot ${REMOVE_TARGET}: ${DATE}, subvolume ${SOURCE_SUBVOLUME}, ${DESCRIPTION} (y/n)? "
read -n 1
if [[ "${REPLY}" == "y" ]]; then
@@ -138,6 +139,64 @@ remove() {
+rollback() {
+ root_check
+ SNAPSHOTS=(${SNAPSHOTS_LOCATION}/*/)
+
+ if [[ "${ROLLBACK_TARGET}" -gt "${#SNAPSHOTS[@]}" ]]; then
+ printf "Snapshot number ${ROLLBACK_TARGET} does not exist.\n"
+ exit 1
+ elif [[ "${ROLLBACK_TARGET}" -lt 1 ]]; then
+ printf "Number must be greater than 0.\n"
+ exit 1
+ fi
+
+ let INDEX=${ROLLBACK_TARGET}-1
+ TARGET=${SNAPSHOTS[${INDEX}]}
+ . ${TARGET}/.snapsh
+
+ printf "You are about to roll back to snapshot ${ROLLBACK_TARGET}: ${DATE}, subvolume ${SOURCE_SUBVOLUME}, type ${TYPE}, ${DESCRIPTION}.\nAre you sure (yes/no)? "
+ read
+
+ if [[ "${REPLY}" == "yes" ]]; then
+ unset ${REPLY}
+ printf "\nCreating a backup snapshot of ${SOURCE_SUBVOLUME}...\n"
+ # Create info file
+ printf "DATE=\"$(date)\"
+ SOURCE_SUBVOLUME=\"${SOURCE_SUBVOLUME}\"
+ DESCRIPTION=\"Rollback backup\"
+ TYPE=\"backup\"\n" > ${TOPLEVEL}/${SOURCE_SUBVOLUME}/.snapsh
+
+ # Create backup snapshot
+ ${BTRFS_EXECUTABLE} subvolume snapshot -r ${TOPLEVEL}/${SUBVOLUME} ${SNAPSHOTS_LOCATION}/${SUBVOLUME}_snapshot_${TIMESTAMP}
+ rm -f ${TOPLEVEL}/${SOURCE_SUBVOLUME}/.snapsh
+
+ # Rename current subvolume
+ printf "Renaming ${SOURCE_SUBVOLUME} to ${SOURCE_SUBVOLUME}.backup...\n"
+ mv ${TOPLEVEL}/${SOURCE_SUBVOLUME} ${TOPLEVEL}/${SOURCE_SUBVOLUME}.backup
+
+ printf "Copying ${TARGET} to ${TOPLEVEL}/${SOURCE_SUBVOLUME}...\n"
+ ${BTRFS_EXECUTABLE} subvolume snapshot ${TARGET} ${TOPLEVEL}/${SOURCE_SUBVOLUME}
+
+ printf "System needs to be restarted. Do you wish to do that now? (recommended!)? (y/n) "
+ read -n 1
+
+ if [[ "${REPLY}" == "y" ]]; then
+ systemctl restart & exit 0
+ else
+ printf "\nPlease restart system as soon as possible. Any changes to\nsubvolume ${SOURCE_SUBVOLUME} will not persist after rebooting.\n"
+ exit 1
+ fi
+
+ else
+ printf "\nAborted by user\n"
+ exit 1
+ fi
+
+}
+
+
+
# Check for root permissions
root_check() {
if [[ "$UID" -ne 0 ]]; then
@@ -156,7 +215,7 @@ fi
# Options parsing:
-OPTIONS=$(getopt -a -n snapsh -o hs:d:lr: --long help,snapshot:,description:,list,remove: -- "$@")
+OPTIONS=$(getopt -a -n snapsh -o hs:d:lr: --long help,snapshot:,description:,list,remove:,rollback: -- "$@")
# Invalid options (getopt returns nonzero)
if [[ "$?" -ne 0 ]]; then
@@ -197,6 +256,13 @@ while true; do
shift 2
;;
+ --rollback)
+ ROLLBACK_TARGET="$2"
+ rollback
+ shift 2
+ ;;
+
+
esac
done
--
2.40.1