×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Bash
Posted by: Adam Bedell
Added: Aug 13, 2013 4:46 AM
Views: 1766
Tags: backup btrfs
  1. #!/bin/sh
  2.  
  3. # example call: # bash /usr/local/bin/snapshot_current_system_state.sh '/run/btrfs-root' '__current/ROOT' '__snapshot/__state_at_last_successful_boot'
  4.  
  5. if [ $# -ne 3 ]
  6. then
  7.   /usr/bin/echo -e "This script requires three parameters:\n1st parameter: The path of the btrfs filesystem root. e. g. /run/btrfs-root\n2nd parameter: The name of the btrfs root volume as specified in /etc/fstab. E. g. __current/ROOT\n3rd parameter: The path where the newly created snapshots will reside without its 1st parameter portion. E. g. __snapshot/__state_at_last_successful_boot\nCAUTION: This script will delete all snapshots of the same name as the regular volume names in the path parameter 3 is pointing to."
  8.   exit 0
  9. fi
  10.  
  11. btrfs_root="${1}" # example: '/run/btrfs-root'
  12. path_to_root_volume="${2}" # example: '__current/ROOT'
  13. path_to_snapshots="${3}" # example: '__snapshot/__state_at_last_successful_boot'
  14.  
  15. # take no snapshots when booted into a snapshot
  16. if [ -e '/SNAPSHOT-TIMESTAMP' ]
  17. then
  18.   exit 0
  19. fi
  20.  
  21. # anti recursive snapshots
  22. for subvolume in $(/usr/bin/btrfs subvolume list '/' | /usr/bin/awk '{print $NF}') # scan
  23. do
  24.   path_to_snapshot="${btrfs_root}/${path_to_snapshots}/${subvolume}"
  25.  
  26.   if [ -d "${path_to_snapshot}" ]
  27.   then
  28.     /usr/bin/btrfs subvolume delete "${path_to_snapshot}"
  29.   fi  
  30. done
  31.  
  32. subvolumes="$(/usr/bin/btrfs subvolume list '/' | /usr/bin/awk '{print $NF}')" # rescan
  33. for subvolume in $subvolumes
  34. do
  35.   snapshot_directory="${btrfs_root}/${path_to_snapshots}/$(/usr/bin/dirname ${subvolume})"
  36.  
  37.   if [ ! -d "${snapshot_directory}" ]
  38.   then
  39.     /usr/bin/mkdir -p "${snapshot_directory}"
  40.   fi  
  41.  
  42.   /usr/bin/btrfs subvolume snapshot "${btrfs_root}/${subvolume}" "${btrfs_root}/${path_to_snapshots}/${subvolume}"
  43.  
  44.   if [ "${subvolume}" = "${path_to_root_volume}" ]
  45.   then
  46.     timestamp="$(/usr/bin/date +%d.%m.%Y-%H:%M:%S)"
  47.  
  48.     /usr/bin/echo -e "Arch Linux --- state at last successful boot (nonpersistent) [${timestamp}]\n" > "${btrfs_root}/${path_to_snapshots}/${path_to_root_volume}/etc/issue"
  49.  
  50.     /usr/bin/echo "${timestamp}" > "${btrfs_root}/${path_to_snapshots}/${path_to_root_volume}/SNAPSHOT-TIMESTAMP"
  51.  
  52.     sed_path_to_snapshots="$(/usr/bin/echo ${path_to_snapshots} | /usr/bin/sed --posix --regexp-extended 's/\//\\\//g')"
  53.  
  54.     for subvolumeX in $(echo $subvolumes | /usr/bin/sed --posix --regexp-extended 's/\//\\\//g')
  55.     do
  56.       /usr/bin/sed --posix --regexp-extended "s/subvol=${subvolumeX}/subvol=${sed_path_to_snapshots}\/${subvolumeX}/g" --in-place "${btrfs_root}/${path_to_snapshots}/${path_to_root_volume}/etc/fstab"
  57.     done
  58.   fi
  59. done
  60.  
  61. /usr/bin/sync