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/4] 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/4] 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/4] 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 c2f4819a3859cc84fd0b3224e5a354b7832f4dea Mon Sep 17 00:00:00 2001 From: Jarno Rankinen Date: Sun, 2 Aug 2020 20:29:04 +0300 Subject: [PATCH 4/4] Snapshot listing implemented Snapshots listing (-l, --list) implemented. --- snapsh | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/snapsh b/snapsh index e3616d3..0d5739e 100755 --- a/snapsh +++ b/snapsh @@ -36,7 +36,7 @@ Options: -h, --help Display this help message -d STR, --description STR Add a description for the snapshot displayed in the snapshots listing. Must be used before -s, e.g. - snapsh -d "A snapshot" -s root + snapsh -d \"A snapshot\" -s root -s SUBVOL, --snapshot SUBVOL Take a snapshot of subvolume named SUBVOL. Exit codes: @@ -67,9 +67,9 @@ snapshot() { # Create info file for listing snapshots # Created first on the source subvolume, then deleted - printf "DATE=$(date) - SOURCE_SUBVOLUME=${SUBVOLUME} - DESCRIPTION=${DESCRIPTION} + printf "DATE=\"$(date)\" + SOURCE_SUBVOLUME=\"${SUBVOLUME}\" + DESCRIPTION=\"${DESCRIPTION}\" TYPE=\"manual\"\n" > ${TOPLEVEL}/${SUBVOLUME}/.snapsh # Create readonly subvolume @@ -92,6 +92,20 @@ snapshot() { +list() { + root_check + NUM=0 + printf "%6s %s %s %26s %s %s %6s %s %s\n" "Number" "|" "Time:" "|" "Source" "|" "Type" "|" "Description" + for snapshot in ${SNAPSHOTS_LOCATION}/*/; do + . ${snapshot}/.snapsh + printf "%8s %32s %8s %8s %s\n" "${NUM} |" "${DATE} |" "${SOURCE_SUBVOLUME} |" "${TYPE} |" "${DESCRIPTION}" + let NUM=NUM+1 + done + exit 0 +} + + + # Check for root permissions root_check() { if [[ "$UID" -ne 0 ]]; then @@ -110,7 +124,7 @@ fi # Options parsing: -OPTIONS=$(getopt -a -n snapsh -o hs:d: --long help,snapshot:,description: -- "$@") +OPTIONS=$(getopt -a -n snapsh -o hs:d:l --long help,snapshot:,description:,list -- "$@") # Invalid options (getopt returns nonzero) if [[ "$?" -ne 0 ]]; then @@ -140,6 +154,11 @@ while true; do shift 2 ;; + -l | --list) + list + shift + ;; + esac done -- 2.40.1