mirror of
https://github.com/bashclub/zfs-housekeeping.git
synced 2024-11-07 15:31:58 +01:00
Merge pull request #3 from sts23/patch-1
Update sts23.sh, neues Script, readme angepasst
This commit is contained in:
commit
1e26b8a7b4
180
README.md
180
README.md
@ -1 +1,181 @@
|
||||
# zfs-keep-and-clean
|
||||
|
||||
## Script sts23.sh
|
||||
### Aufruf:
|
||||
Aufruf: ./sts23.sh <test|echt> <Poolfilter> <Anz. Behalte> [Snapshotfilter]
|
||||
|
||||
|
||||
## Script snpgrpdl.sh (neu)
|
||||
Snapshots nach Datasets und Muster im Snapshotnamen filtern und ggf. löschen.
|
||||
Es können alle bis auf die letzten x Snapshots behalten werden (-k keep).
|
||||
Zu löschende Snapshots werden mit einem '-' gekennzeichnet.
|
||||
|
||||
### Aufruf:
|
||||
```
|
||||
root@zfsrasp~# ./snpgrpdl.sh -h
|
||||
|
||||
Aufruf: snpgrpdl.sh [-htf] [-k keep] <Datasetfilter> [Snapshotfilter]
|
||||
|
||||
-h Hilfe Anzeigen
|
||||
-t Testmodus. Simulation von löschen ohne Nachfragen (benötigt -k)
|
||||
-f Echtmodus. Snapshots löschen ohne Nachfragen (benötigt -k)
|
||||
-k <Anzahl> Anzahl der letzten Snapshots die behalten werden sollen.
|
||||
Rest wird gelöscht. Ohne -t oder -f kommt Abfrage ob gelöscht werden soll.
|
||||
```
|
||||
### Beispiele:
|
||||
Nur Filtern (ohne löschen):
|
||||
```
|
||||
root@zfsrasp:~# ./snpgrpdl.sh backup frequent
|
||||
# Gefilterte Datasets:
|
||||
NAME USED AVAIL REFER MOUNTPOINT
|
||||
backup 3.93M 3.62G 24K /backup
|
||||
backup/smbshr 182K 3.62G 50.5K /backup/smbshr
|
||||
|
||||
# Dataset: backup
|
||||
# Snapfilter: frequent
|
||||
# Anzahl Snaps: 4
|
||||
# Liste Snaps:
|
||||
+ backup@zfs-auto-snap_frequent-2021-01-01-2030
|
||||
+ backup@zfs-auto-snap_frequent-2021-01-01-2045
|
||||
+ backup@zfs-auto-snap_frequent-2021-01-01-2100
|
||||
+ backup@zfs-auto-snap_frequent-2021-01-01-2115
|
||||
|
||||
# Dataset: backup/smbshr
|
||||
# Snapfilter: frequent
|
||||
# Anzahl Snaps: 6
|
||||
# Liste Snaps:
|
||||
+ backup/smbshr@zfs-auto-snap_frequent-2020-12-28-1145
|
||||
+ backup/smbshr@zfs-auto-snap_frequent-2020-12-28-1200
|
||||
+ backup/smbshr@zfs-auto-snap_frequent-2021-01-01-2030
|
||||
+ backup/smbshr@zfs-auto-snap_frequent-2021-01-01-2045
|
||||
+ backup/smbshr@zfs-auto-snap_frequent-2021-01-01-2100
|
||||
+ backup/smbshr@zfs-auto-snap_frequent-2021-01-01-2115
|
||||
|
||||
root@zfsrasp:~#
|
||||
```
|
||||
Interaktiv - Filtern und alle außer die letzten 10 löschen:
|
||||
```
|
||||
root@zfsrasp:~# ./snpgrpdl.sh -k 10 backup$ daily
|
||||
# Gefilterte Datasets:
|
||||
NAME USED AVAIL REFER MOUNTPOINT
|
||||
backup 4.00M 3.62G 24K /backup
|
||||
|
||||
# Dataset: backup
|
||||
# Snapfilter: daily
|
||||
# Anzahl Snaps: 12
|
||||
# Behalte: 10
|
||||
# Anzahl Del. Snaps: 2
|
||||
# Liste Snaps:
|
||||
- backup@zfs-auto-snap_daily-2020-12-21-0525
|
||||
- backup@zfs-auto-snap_daily-2020-12-22-0525
|
||||
+ backup@zfs-auto-snap_daily-2020-12-23-0525
|
||||
+ backup@zfs-auto-snap_daily-2020-12-24-0525
|
||||
+ backup@zfs-auto-snap_daily-2020-12-25-0525
|
||||
+ backup@zfs-auto-snap_daily-2020-12-26-0525
|
||||
+ backup@zfs-auto-snap_daily-2020-12-27-0525
|
||||
+ backup@zfs-auto-snap_daily-2020-12-28-0525
|
||||
+ backup@zfs-auto-snap_daily-2020-12-29-0525
|
||||
+ backup@zfs-auto-snap_daily-2020-12-30-0525
|
||||
+ backup@zfs-auto-snap_daily-2020-12-31-0525
|
||||
+ backup@zfs-auto-snap_daily-2021-01-01-0525
|
||||
> Snapshots (2 von 12) mit J löschen (Return = weiter): J
|
||||
% zfs destroy wird ausgeführt:
|
||||
will destroy backup@zfs-auto-snap_daily-2020-12-21-0525
|
||||
will reclaim 0B
|
||||
will destroy backup@zfs-auto-snap_daily-2020-12-22-0525
|
||||
will reclaim 0B
|
||||
% Liste nach zfs destroy:
|
||||
+ backup@zfs-auto-snap_daily-2020-12-23-0525
|
||||
+ backup@zfs-auto-snap_daily-2020-12-24-0525
|
||||
+ backup@zfs-auto-snap_daily-2020-12-25-0525
|
||||
+ backup@zfs-auto-snap_daily-2020-12-26-0525
|
||||
+ backup@zfs-auto-snap_daily-2020-12-27-0525
|
||||
+ backup@zfs-auto-snap_daily-2020-12-28-0525
|
||||
+ backup@zfs-auto-snap_daily-2020-12-29-0525
|
||||
+ backup@zfs-auto-snap_daily-2020-12-30-0525
|
||||
+ backup@zfs-auto-snap_daily-2020-12-31-0525
|
||||
+ backup@zfs-auto-snap_daily-2021-01-01-0525
|
||||
|
||||
root@zfsrasp:~#
|
||||
```
|
||||
Testmodus - löschen nur simulieren (ohne Nachfragen):
|
||||
```
|
||||
root@zfsrasp:~# ./snpgrpdl.sh -t -k 5 backup$ daily
|
||||
# Gefilterte Datasets:
|
||||
NAME USED AVAIL REFER MOUNTPOINT
|
||||
backup 3.98M 3.62G 24K /backup
|
||||
|
||||
# Dataset: backup
|
||||
# Snapfilter: daily
|
||||
# Anzahl Snaps: 10
|
||||
# Behalte: 5
|
||||
# Anzahl Del. Snaps: 5
|
||||
# Liste Snaps:
|
||||
- backup@zfs-auto-snap_daily-2020-12-23-0525
|
||||
- backup@zfs-auto-snap_daily-2020-12-24-0525
|
||||
- backup@zfs-auto-snap_daily-2020-12-25-0525
|
||||
- backup@zfs-auto-snap_daily-2020-12-26-0525
|
||||
- backup@zfs-auto-snap_daily-2020-12-27-0525
|
||||
+ backup@zfs-auto-snap_daily-2020-12-28-0525
|
||||
+ backup@zfs-auto-snap_daily-2020-12-29-0525
|
||||
+ backup@zfs-auto-snap_daily-2020-12-30-0525
|
||||
+ backup@zfs-auto-snap_daily-2020-12-31-0525
|
||||
+ backup@zfs-auto-snap_daily-2021-01-01-0525
|
||||
# Testmodus - Für folgende Snapshots würde ein destroy ausgeführt:
|
||||
backup@zfs-auto-snap_daily-2020-12-23-0525 creation Wed Dec 23 6:25 2020 -
|
||||
backup@zfs-auto-snap_daily-2020-12-24-0525 creation Thu Dec 24 6:25 2020 -
|
||||
backup@zfs-auto-snap_daily-2020-12-25-0525 creation Fri Dec 25 6:25 2020 -
|
||||
backup@zfs-auto-snap_daily-2020-12-26-0525 creation Sat Dec 26 6:25 2020 -
|
||||
backup@zfs-auto-snap_daily-2020-12-27-0525 creation Sun Dec 27 6:25 2020 -
|
||||
root@zfsrasp:~#
|
||||
```
|
||||
|
||||
Forcemodus - löschen ohne Nachfragen:
|
||||
```
|
||||
root@zfsrasp:~# ./snpgrpdl.sh -f -k 6 backup frequent
|
||||
# Gefilterte Datasets:
|
||||
NAME USED AVAIL REFER MOUNTPOINT
|
||||
backup 3.98M 3.62G 24K /backup
|
||||
backup/smbshr 182K 3.62G 50.5K /backup/smbshr
|
||||
|
||||
# Dataset: backup
|
||||
# Snapfilter: frequent
|
||||
# Anzahl Snaps: 4
|
||||
# Behalte: 6
|
||||
# Anzahl Del. Snaps: 0
|
||||
# Liste Snaps:
|
||||
+ backup@zfs-auto-snap_frequent-2021-01-01-2000
|
||||
+ backup@zfs-auto-snap_frequent-2021-01-01-2015
|
||||
+ backup@zfs-auto-snap_frequent-2021-01-01-2030
|
||||
+ backup@zfs-auto-snap_frequent-2021-01-01-2045
|
||||
% Keine Snapshots zum löschen
|
||||
|
||||
# Dataset: backup/smbshr
|
||||
# Snapfilter: frequent
|
||||
# Anzahl Snaps: 8
|
||||
# Behalte: 6
|
||||
# Anzahl Del. Snaps: 2
|
||||
# Liste Snaps:
|
||||
- backup/smbshr@zfs-auto-snap_frequent-2020-12-21-2130
|
||||
- backup/smbshr@zfs-auto-snap_frequent-2020-12-21-2145
|
||||
+ backup/smbshr@zfs-auto-snap_frequent-2020-12-28-1145
|
||||
+ backup/smbshr@zfs-auto-snap_frequent-2020-12-28-1200
|
||||
+ backup/smbshr@zfs-auto-snap_frequent-2021-01-01-2000
|
||||
+ backup/smbshr@zfs-auto-snap_frequent-2021-01-01-2015
|
||||
+ backup/smbshr@zfs-auto-snap_frequent-2021-01-01-2030
|
||||
+ backup/smbshr@zfs-auto-snap_frequent-2021-01-01-2045
|
||||
% zfs destroy wird ausgeführt:
|
||||
will destroy backup/smbshr@zfs-auto-snap_frequent-2020-12-21-2130
|
||||
will reclaim 0B
|
||||
will destroy backup/smbshr@zfs-auto-snap_frequent-2020-12-21-2145
|
||||
will reclaim 0B
|
||||
% Liste nach zfs destroy:
|
||||
+ backup/smbshr@zfs-auto-snap_frequent-2020-12-28-1145
|
||||
+ backup/smbshr@zfs-auto-snap_frequent-2020-12-28-1200
|
||||
+ backup/smbshr@zfs-auto-snap_frequent-2021-01-01-2000
|
||||
+ backup/smbshr@zfs-auto-snap_frequent-2021-01-01-2015
|
||||
+ backup/smbshr@zfs-auto-snap_frequent-2021-01-01-2030
|
||||
+ backup/smbshr@zfs-auto-snap_frequent-2021-01-01-2045
|
||||
|
||||
root@zfsrasp:~#
|
||||
```
|
||||
|
163
snpgrpdl.sh
Normal file
163
snpgrpdl.sh
Normal file
@ -0,0 +1,163 @@
|
||||
#! /bin/sh
|
||||
|
||||
pr_aufruf() {
|
||||
cat <<ENDE
|
||||
|
||||
Aufruf: $(basename $0) [-htf] [-k keep] <Datasetfilter> [Snapshotfilter]
|
||||
|
||||
-h Hilfe Anzeigen
|
||||
-t Testmodus. Simulation von löschen ohne Nachfragen (benötigt -k)
|
||||
-f Echtmodus. Snapshots löschen ohne Nachfragen (benötigt -k)
|
||||
-k <Anzahl> Anzahl der letzten Snapshots die behalten werden sollen.
|
||||
Rest wird gelöscht. Ohne -t oder -f kommt Abfrage ob gelöscht werden soll.
|
||||
|
||||
ENDE
|
||||
}
|
||||
|
||||
# Defaults:
|
||||
# Modi: fragen (default), test (-t) oder echt (-f)
|
||||
MODUS="fragen"
|
||||
|
||||
while getopts ":htfk:" opt
|
||||
do
|
||||
case $opt in
|
||||
h) pr_aufruf
|
||||
exit 0
|
||||
;;
|
||||
t) MODUS="test"
|
||||
TFLAG="1"
|
||||
;;
|
||||
f) MODUS="echt"
|
||||
FFLAG="1"
|
||||
;;
|
||||
k) BEHALTE="$OPTARG"
|
||||
KFLAG="1"
|
||||
;;
|
||||
\?) echo "Unbekannte Option: -$OPTARG" 1>&2;;
|
||||
:) echo "Fehler Option: -$OPTARG benötigt ein Argument" 1>&2;;
|
||||
esac
|
||||
done
|
||||
|
||||
shift $((OPTIND -1))
|
||||
|
||||
# Mit der Filter Variable kannst du die Liste der datasets beschränken
|
||||
# Mit der Muster Variable kann man Snapshots nach Muster Filtern
|
||||
FILTER="$1"
|
||||
MUSTER="$2"
|
||||
|
||||
if [ "$1" = "" -o $# -gt 2 ]
|
||||
then
|
||||
pr_aufruf
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ "$FFLAG" = "1" -a "$TFLAG" = "1" ]
|
||||
then
|
||||
echo "Option -f oder -t können nicht zusammen verwendet werden."
|
||||
pr_aufruf
|
||||
exit 1
|
||||
elif [ "$FFLAG" -o "$TFLAG" ] && [ "$KFLAG" != "1" ]
|
||||
then
|
||||
echo "Option -t oder - f nur zusammen mit -k nutzbar"
|
||||
pr_aufruf
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Prüfen das Behalte eine Zahl >= 0 ist
|
||||
if [ "$KFLAG" = "1" ]
|
||||
then
|
||||
[ $BEHALTE -ge 0 ] 2>/dev/null
|
||||
if [ "$?" != "0" ]
|
||||
then
|
||||
echo "Option -k keine pos. Zahl"
|
||||
pr_aufruf
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
ZFSSETLIST=$(zfs list | grep "$FILTER" | awk '{print $1}')
|
||||
|
||||
echo "# Gefilterte Datasets:"
|
||||
zfs list | grep -e "$FILTER" -e NAME | sed 's/^/ /'
|
||||
echo ""
|
||||
|
||||
for DATASET in $ZFSSETLIST
|
||||
do
|
||||
echo "# Dataset: $DATASET"
|
||||
echo "# Snapfilter: $MUSTER"
|
||||
|
||||
if [ "$MUSTER" = "" ]
|
||||
then
|
||||
SNAPS=$(zfs list -r -t snap -H -o name -s creation | grep "${DATASET}@")
|
||||
else
|
||||
SNAPS=$(zfs list -r -t snap -H -o name -s creation | grep "${DATASET}@" | grep $MUSTER)
|
||||
fi
|
||||
ANZSNAPS=$(echo "$SNAPS"|wc -l|sed 's, ,,g')
|
||||
|
||||
echo "# Anzahl Snaps: $ANZSNAPS"
|
||||
if [ "$KFLAG" = "1" ]
|
||||
then
|
||||
DELSNAPS=$((ANZSNAPS - BEHALTE))
|
||||
echo "# Behalte: $BEHALTE"
|
||||
echo "# Anzahl Del. Snaps: $(if [ $DELSNAPS -lt 0 ];\
|
||||
then echo 0;else echo $DELSNAPS;fi)"
|
||||
else
|
||||
DELSNAPS="0"
|
||||
BEHALTE="$ANZSNAPS"
|
||||
fi
|
||||
|
||||
echo "# Liste Snaps:"
|
||||
echo "$SNAPS" |sed '/^$/d' | awk -v dellines=$DELSNAPS '{
|
||||
if (NR <= dellines){
|
||||
printf(" - %s\n",$0)
|
||||
}
|
||||
else {
|
||||
printf(" + %s\n",$0)
|
||||
}
|
||||
}'
|
||||
|
||||
[ $DELSNAPS -gt 0 -o "$KFLAG" != "1" ] 2>/dev/null
|
||||
if [ "$?" != "0" ]
|
||||
then
|
||||
echo "% Keine Snapshots zum löschen"
|
||||
echo ""
|
||||
continue
|
||||
fi
|
||||
|
||||
if [ $DELSNAPS -gt 0 ]
|
||||
then
|
||||
if [ "$MODUS" = "fragen" ]
|
||||
then
|
||||
printf "> Snapshots (%d von %d) mit J löschen (Return = weiter): " $DELSNAPS $ANZSNAPS
|
||||
read ZDESTROY
|
||||
elif [ "$MODUS" = "echt" ]
|
||||
then
|
||||
ZDESTROY="J"
|
||||
else
|
||||
ZDESTROY="N"
|
||||
echo "# Testmodus - Für folgende Snapshots würde ein destroy ausgeführt:"
|
||||
echo "$SNAPS" | head -n $DELSNAPS | xargs -n 1 zfs get -H creation | sed 's/^/ /'
|
||||
echo ""
|
||||
continue
|
||||
fi
|
||||
|
||||
if [ "$ZDESTROY" = "J" ]
|
||||
then
|
||||
echo "% zfs destroy wird ausgeführt:"
|
||||
echo "$SNAPS" | head -n $DELSNAPS | xargs -n 1 zfs destroy -v | sed 's/^/ /'
|
||||
|
||||
echo "% Liste nach zfs destroy:"
|
||||
if [ "$MUSTER" = "" ]
|
||||
then
|
||||
zfs list -r -t snap -o name -s creation | grep "${DATASET}@" | sed 's/^/ + /'
|
||||
else
|
||||
zfs list -r -t snap -o name -s creation | grep "${DATASET}@" | grep $MUSTER | sed 's/^/ + /'
|
||||
fi
|
||||
else
|
||||
echo "% Es wurden keine Snapshots gelöscht"
|
||||
fi
|
||||
fi
|
||||
echo ""
|
||||
done
|
||||
|
||||
exit 0
|
12
sts23.sh
12
sts23.sh
@ -16,9 +16,9 @@ FILTER="$2"
|
||||
# Wieviele behalten
|
||||
BEHALTE="$3"
|
||||
|
||||
[ $BEHALTE -gt 0 ] 2>/dev/null
|
||||
[ $BEHALTE -ge 0 ] 2>/dev/null
|
||||
if [ "$?" != "0" ]
|
||||
then
|
||||
then
|
||||
echo "Parameter 3 keine pos. Zahl"
|
||||
echo "$AUFRUF"
|
||||
exit 0
|
||||
@ -40,9 +40,9 @@ do
|
||||
|
||||
if [ "$MUSTER" = "" ]
|
||||
then
|
||||
SNAPS=$(zfs list -t snap -H -o name -s creation "${DATASET}")
|
||||
SNAPS=$(zfs list -r -t snap -H -o name -s creation|grep "${DATASET}@")
|
||||
else
|
||||
SNAPS=$(zfs list -t snap -H -o name -s creation "${DATASET}"|grep $MUSTER)
|
||||
SNAPS=$(zfs list -r -t snap -H -o name -s creation|grep "${DATASET}@"|grep $MUSTER)
|
||||
fi
|
||||
ANZSNAPS=$(echo $SNAPS|wc -w)
|
||||
|
||||
@ -77,9 +77,9 @@ do
|
||||
echo "# Liste nach zfs destroy"
|
||||
if [ "$MUSTER" = "" ]
|
||||
then
|
||||
zfs list -t snap -o name -s creation "${DATASET}"
|
||||
zfs list -r -t snap -o name -s creation|grep "${DATASET}@"
|
||||
else
|
||||
zfs list -t snap -o name -s creation "${DATASET}"|grep $MUSTER
|
||||
zfs list -r -t snap -o name -s creation|grep "${DATASET}@"|grep $MUSTER
|
||||
fi
|
||||
else
|
||||
echo "# Test - folgende zfs destroys würden ausgführt"
|
||||
|
Loading…
Reference in New Issue
Block a user