code.fastix.org

Dateiansicht:

Datei:Projekte -> Ubuntu,btrfs,apt:Löschen alter Snapshots -> delete-apt-snaphots.sh
md5:e4c0151a2b1b18d2d740bed1307260e0
sha1:dfc45c57aa8209dcb26f4817639f104ef324df07
  1. #!/usr/bin/sudo /bin/bash
  2. # (Debian: Bitte ersetzen Sie die Zeile 1 (die "shebang") mit "#!/bin/bash" und starten Sie das Skript als root)
  3. # (Debian: Please replace the line 1 (the "shebang") with "#!/bin/bash" and start the script as root)
  4.  
  5. ###
  6. # LastUpdate:   2018-03-31
  7. # Version:      2.0
  8. # Author:       Jörg Reinholz, fastix WebDesign & Consult Kassel, http://www.fastix.org
  9. # Requirements: Ubuntu 16.04 (Debian, Mint e.t.c.) with btrfs on root-filesstem
  10. # Licence:      https://code.fastix.org/lizenz.php
  11. # Non-Warranty: https://code.fastix.org/haftung.php
  12. ###
  13.  
  14. ### Config: ###
  15. # Anzahl der Snapshots welche erhalten bleiben:
  16. # Number of snapshots which are preserved:
  17. noDel=5;
  18.  
  19. ### Hinweis: Starten Sie das Skript mit der Option "-a" um alle Snapshots zu löschen.
  20. ### Hint: Start the script with the option "-a" to delete all snapshots
  21.  
  22. ### Program. Do not edit until you know EXACT what you do! ###
  23.  
  24. if [ "$1" = "-a" ]; then
  25.     noDel=0;
  26. fi
  27.  
  28. rootdevice=$(/bin/df / | /usr/bin/tail -n1 | /usr/bin/cut -d " " -f1);
  29. workdir=$(/bin/mktemp -d -t "delete-apt-snapshots-XXXXX");
  30. olddir=$(/bin/pwd);
  31.  
  32. /bin/mount -t btrfs "${rootdevice}" "${workdir}";
  33. cd "${workdir}";
  34. echo;
  35.  
  36. lSnapshots=$(/bin/ls -d @apt-snapshot* 2> /dev/null);
  37. count=0;
  38. for snapshot in ${lSnapshots}; do
  39.         count=$((${count}+1));
  40. done
  41. cDelete=$((${count}-${noDel}));
  42. if [ ${cDelete} -lt 0 ]; then
  43.     cDelete=0;
  44. fi
  45.  
  46. echo "Snapshots: ${count}";
  47. echo "To Delete: ${cDelete}";
  48.  
  49.  
  50. for snapshot in ${lSnapshots}; do
  51.     if [ ${cDelete} -gt 0 ]; then
  52.         /bin/echo -n "delete ${snapshot} .. ";
  53.         /sbin/btrfs subvolume delete "${snapshot}/@" 1> /dev/null 2> /dev/null;
  54.         /sbin/btrfs subvolume delete "${snapshot}"   1> /dev/null 2> /dev/null;
  55.         /bin/echo " passed";
  56.     else
  57.        /bin/echo "skipping ${snapshot}";
  58.     fi
  59.     cDelete=$((${cDelete}-1));
  60. done
  61. /bin/echo;
  62. cd "${olddir}";
  63. /bin/umount "${workdir}";
  64. /bin/rmdir "${workdir}";
  65.