diff --git a/README.md b/README.md index 9e30707..fbb6230 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,2 @@ # snapsh -Btrfs snapshot managing bash script - -### 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. 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. -- Rollback to a snapshot with `snapsh --rollback NUMBER`, where NUMBER is the number listed in `snapsh -l`. The subvolume will be detected from the snapshot. E.g. `snapsh --rollback 14`. Script will ask to reboot system once done. - -### 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 +Btrfs snapshot managing script diff --git a/snapsh b/snapsh index b2705f1..c389931 100755 --- a/snapsh +++ b/snapsh @@ -29,15 +29,16 @@ DESCRIPTION="" help() { - printf "Usage: - snapsh [OPTIONS] + printf "Usage:\n +snapsh [OPTIONS] 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 -s SUBVOL, --snapshot SUBVOL Take a snapshot of subvolume named SUBVOL. + -d STR, --description STR Add a description for the snapshot displayed in the + snapshots listing. + -t TYPE, --type TYPE Set the type of snapshot, where TYPE=manual|auto|boot|backup + Can be used with -l, --list to filter results -l, --list List snapshots -r NUMBER, --remove NUMBER Remove snapshot NUMBER. See snapshot numbers with snapsh -l @@ -65,6 +66,11 @@ snapshot() { EXIT_CODE=1 fi else + # TYPE defaults to manual + if [[ -z "${SET_TYPE}" ]]; then + SET_TYPE="manual" + fi + printf "Creating snapshot of subvolume ${SUBVOLUME} as ${SUBVOLUME}_snapshot_${TIMESTAMP}\n" # Create info file for listing snapshots @@ -72,7 +78,7 @@ snapshot() { printf "DATE=\"$(date)\" SOURCE_SUBVOLUME=\"${SUBVOLUME}\" DESCRIPTION=\"${DESCRIPTION}\" - TYPE=\"manual\"\n" > ${TOPLEVEL}/${SUBVOLUME}/.snapsh + TYPE=\"${SET_TYPE}\"\n" > ${TOPLEVEL}/${SUBVOLUME}/.snapsh # Create readonly subvolume ${BTRFS_EXECUTABLE} subvolume snapshot -r ${TOPLEVEL}/${SUBVOLUME} ${SNAPSHOTS_LOCATION}/${SUBVOLUME}_snapshot_${TIMESTAMP} @@ -85,7 +91,7 @@ snapshot() { DATE=$(date) SOURCE_SUBVOLUME=${SUBVOLUME} DESCRIPTION=${DESCRIPTION} - TYPE=\"manual\"\n" + TYPE=\"${SET_TYPE}\"\n" fi @@ -99,8 +105,14 @@ list() { NUM=1 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}" + if [[ -z "${SET_TYPE}" ]]; then + . ${snapshot}/.snapsh + printf "%8s %32s %8s %8s %s\n" "${NUM} |" "${DATE} |" "${SOURCE_SUBVOLUME} |" "${TYPE} |" "${DESCRIPTION}" + elif [[ -n "${SET_TYPE}" ]]; then + . ${snapshot}/.snapsh + [[ "${SET_TYPE}" == "${TYPE}" ]] && printf "%8s %32s %8s %8s %s\n" "${NUM} |" "${DATE} |" "${SOURCE_SUBVOLUME} |" "${TYPE} |" "${DESCRIPTION}" + fi + let NUM=NUM+1 done exit 0 @@ -124,7 +136,7 @@ remove() { TARGET=${SNAPSHOTS[${INDEX}]} . ${TARGET}/.snapsh - printf "Delete snapshot ${REMOVE_TARGET}: ${DATE}, subvolume ${SOURCE_SUBVOLUME}, ${DESCRIPTION} (y/n)? " + printf "Delete snapshot ${REMOVE_TARGET}: ${DATE}, source subvolume ${SOURCE_SUBVOLUME}, ${TYPE}, ${DESCRIPTION} (y/n)? " read -n 1 if [[ "${REPLY}" == "y" ]]; then printf "\n" @@ -215,11 +227,11 @@ fi # Options parsing: -OPTIONS=$(getopt -a -n snapsh -o hs:d:lr: --long help,snapshot:,description:,list,remove:,rollback: -- "$@") +OPTIONS=$(getopt -a -n snapsh -o hs:d:lr:t: --long help,snapshot:,description:,list,remove:,rollback:,type: -- "$@") # Invalid options (getopt returns nonzero) if [[ "$?" -ne 0 ]]; then - printf "Error Parsing options\n" + printf "Invalid options.\n" help exit 1 fi @@ -241,12 +253,12 @@ while true; do -s | --snapshot) SUBVOLUME="$2" - snapshot + SNAPSHOT=true shift 2 ;; -l | --list) - list + LIST=true shift ;; @@ -261,9 +273,34 @@ while true; do rollback shift 2 ;; + + -t | --type) + case "$2" in + manual) + SET_TYPE="manual";; + auto) + SET_TYPE="auto";; + boot) + SET_TYPE="boot";; + backup) + SET_TYPE="backup";; + *) + printf "\nIncorrect type value.\n" + exit 1;; + esac + shift 2 + ;; - + --) + shift + break + ;; esac done +if [[ -n "${SNAPSHOT}" ]]; then + snapshot +elif [[ -n "${LIST}" ]]; then + list +fi \ No newline at end of file