Made detecting if zpool is already mounted or unmounted better
[zfs-ubuntu/.git] / zfs-rm-all-snaps
1 #!/bin/bash
2
3 # Author: Alan Pippin
4 # Description: This script will remove all snaps containing a given snapshot pattern
5 #              across all filesystems on a given pool.
6 # Usage: zfs-rm-all-snaps <pool> <snapshot pattern>
7
8 # source our configuration
9 config="${0%/*}/zfs-scripts.conf"
10 [ -e "${config}.dist" ] && . ${config}.dist
11 [ -e "${config}" ] && . ${config}
12
13 # command line arg parsing
14 zfs_pool=$1
15 snap_pattern=$2
16
17 if [[ -z "$zfs_pool" ]] || [[ -z "$snap_pattern" ]]; then
18   echo "Usage: $0 <pool>/<filesystem> <snapshot pattern>"
19   exit 1
20 fi
21
22 echo "-> Deleting all snapshots on pool '$zfs_pool' with pattern '$snap_pattern'"
23 for snap in `zfs list -t snapshot 2>/dev/null | grep "^$zfs_pool" | grep "$snap_pattern" | awk '{print $1}'`; do
24   echo "   removing snapshot: $snap"
25 done
26
27 echo -e "\n<Press Enter to execute removal, CTL-C to abort>";
28 read
29
30 for snap in `zfs list -t snapshot 2>/dev/null | grep "^$zfs_pool" | grep "$snap_pattern" | awk '{print $1}'`; do
31   echo "   destroying snapshot: $snap"
32   zfs destroy "$snap" 2>/dev/null
33 done
34
35 echo "-> Snapshots removed"