Skip to content

Instantly share code, notes, and snippets.

@mubeeniqbal
Last active August 29, 2015 14:10
Show Gist options
  • Save mubeeniqbal/7be17c3ae904e1b2c393 to your computer and use it in GitHub Desktop.
Save mubeeniqbal/7be17c3ae904e1b2c393 to your computer and use it in GitHub Desktop.
#
# Snap is a utility to create system snapshots for btrfs subvolumes.
#
# @author Mubeen Iqbal
# @date 2014-11-26
# @license GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
#
#!/bin/sh
# The total subvolumes for which a snapshot will be created.
totalSubvolumes=3
# Source subvolumes array (create snapshot of these).
sourceSubvolumes=(
"/run/btrfs-root/arch/__active/rootvol"
"/run/btrfs-root/data/__active/home"
"/run/btrfs-root/data/__active/var"
)
# Source subvolume mount points.
mountPoints=(
"/"
"/home"
"/var"
)
# Snapshots directory.
snapshotDir="/run/btrfs-root/data/__snapshot"
# Snapshot names array.
snapshotNames=(
"rootvol"
"home"
"var"
)
# Record date and time for the snapshot.
# The date must be recorded only once to avoid any time differences.
dt="$(date +%Y-%m-%d_%H-%M-%S)"
d="$(echo "${dt}" | cut -d '_' -f1)"
t="$(echo "${dt}" | cut -d '_' -f2)"
dateTime=""${d}"-"${t}""
# Readme file info.
readmeFileName="readme.txt"
readmeFilePath=""${snapshotDir}"/"${dateTime}"/"${readmeFileName}""
# Variable to hold a user provided description (if any) for the snapshot.
description="---"
printHelp() {
echo "Usage: snap [OPTION]"
echo "Create a snapshot of the whole system (snapshots of all subvolumes in the system)."
echo
echo "Mandatory arguments to long options are mandatory for short options too."
echo " -d, --description=DESC add a description DESC to the file created with"
echo " the snapshot explaining about the snapshot"
echo " --help display this help and exit"
echo " --version output version information and exit"
}
printVersion() {
echo "snap 0.1"
echo "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>."
echo "This is free software: You are free to change and redistribute it."
echo "There is NO WARRANTY, to the extent permitted by law."
echo
echo "Written by Mubeen Iqbal."
}
printInvalidOptionError() {
echo "snap: invalid option -- '$(echo "${1}" | tr -d -)'"
echo "Try 'snap --help' for more information."
}
printNoDescriptionProvidedError() {
echo "snap: No description provided"
}
validate() {
# If any of the options is one of the following then execute and exit.
for param; do
case "${param}" in
--help)
printHelp
exit 0
;;
--version)
printVersion
exit 0
;;
esac
done
# Handle other options.
while [ "${1}" != "" ]; do
case "${1}" in
-d | --description)
shift
if [ -z "${1}" ]; then
printNoDescriptionProvidedError
exit 2
else
description="${1}"
fi
;;
*)
printInvalidOptionError "${1}"
exit 2
;;
esac
shift
done
}
printSnapshotPaths() {
echo -e "<mount point>\t<source subvolume>\t\t\t<snapshot path>"
for (( i=0; i<"${totalSubvolumes}"; ++i )) {
printf "%s\t\t%s\t%s\n" "${mountPoints[i]}" "${sourceSubvolumes[i]}" "${snapshotDir}"/"${dateTime}"/"${snapshotNames[i]}"
}
}
createSnapshot() {
for (( i=0; i<"${totalSubvolumes}"; ++i )) {
sudo btrfs subvolume snapshot -r "${sourceSubvolumes[i]}" "${snapshotDir}"/"${dateTime}"/"${snapshotNames[i]}"
}
}
printSnapshotCommands() {
for (( i=0; i<"${totalSubvolumes}"; ++i )) {
printf "btrfs subvolume snapshot -r %s %s\n" "${sourceSubvolumes[i]}" "${snapshotDir}"/"${dateTime}"/"${snapshotNames[i]}"
}
}
createReadmeFile() {
sudo printf "%s\n" "$(
echo -e "This file has been created automatically by snap.\n"
echo -e "Snapshot created on: $(date)"
echo -e "Author (user): "$(whoami)""
echo -e "Readonly snapshot: yes\n"
echo -e "<snapshot commands>"
printSnapshotCommands
echo
printSnapshotPaths
echo -e "\nDescription"
echo -e "===========\n"
echo "${description}"
)" > "${readmeFilePath}"
echo "A readme file for the snapshot has been created at:"
echo "${readmeFilePath}"
}
#
# Main
#
# Validate if this utility is being used with the proper syntax.
validate "${@}"
sudo mkdir ""${snapshotDir}"/"${dateTime}""
echo -e "Creating a full system snapshot on "${d}" (yyyy-mm-dd) at "$(echo "${t}" | tr - :)" (hh:mm:ss).\n"
createSnapshot
echo -e "A full system snapshot has been created at:"
echo -e ""${snapshotDir}"/"${dateTime}"/\n"
printSnapshotPaths
echo
createReadmeFile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment