szaydel
7/6/2016 - 10:24 PM

Bechtel maintenance examples

Bechtel maintenance examples

#!/bin/sh
# Config file is nothing more than a list of datasets, one per line.
# This all assumes ZFS paths, not filesystem mountpoints.
# This is a simple example:
# poolA/nfs/a
# poolA/nfs/b
# poolA/nfs/c
# poolA/nfs/d
config=conf # Maybe this file should be in /etc?
SNAPSHOT_NAME=pre-maintenance
CLONE_NAME=clone
ZFS_CMD="/usr/sbin/zfs"

# set -o xtrace # enable this for debugging

function print_err {
  printf "[ERROR] $@: " >&2
  exit 1
}

function print_warn {
  printf "[WARN] $@: " >&2
}

if [ ! -f "${config}" ]; then
  print_err \
    "Unable to locate config file. Please create ${config} before running."
else
  datasets=(`cat "${config}"`)
  if [ -z "${datasets[0]}" ]; then
    print_err \
    "Config file appears empty. At least one dataset path is required."
  fi




function iterate_create_clones {
  for ds in "${datasets[@]}"; do
    "${ZFS_CMD}" clone "${ds}@${SNAPSHOT_NAME}" "${ds}-${CLONE_NAME}" ||
    print_warn "Unable to create clone ${ds}-${CLONE_NAME}"
  done
  return
}

function iterate_create_snaps {
  for ds in "${datasets[@]}"; do
    "${ZFS_CMD}" snapshot "${ds}@${SNAPSHOT_NAME}" ||
    print_warn "Unable to create snapshot ${ds}-${SNAPSHOT_NAME}"
  done
  return
}

function iterate_remove_clones {
  for ds in "${datasets[@]}"; do
    "${ZFS_CMD}" destroy "${ds}-${CLONE_NAME}" ||
    print_warn "Unable to destroy clone ${ds}-${CLONE_NAME}"
  done
  return
}

function iterate_remove_snaps {
  for ds in "${datasets[@]}"; do
    "${ZFS_CMD}" destroy "${ds}@${SNAPSHOT_NAME}" ||
    print_warn "Unable to destroy snapshot ${ds}-${SNAPSHOT_NAME}"
  done
  return
}

case $1 in
  "start")
    iterate_create_snaps && iterate_create_clones
    ;;
  "stop")
    iterate_remove_clones && iterate_remove_snaps
    ;;
  *)
  ;;
esac