Advertisement
Guest User

Untitled

a guest
May 23rd, 2023
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Verzeichnisse für die Sicherung
  4. source_dir1="/home/server"
  5. source_dir2="/home/server2"
  6.  
  7. # Temporäres Btrfs-Subvolume für die Snapshots
  8. tmp_subvolume="/tmp/snapshot_tmp"
  9.  
  10. # Zielverzeichnis für die Snapshots
  11. snapshot_dir="/home/asaj/snapshots"
  12.  
  13. # BTRFS-Subvolume für den vorherigen Snapshot
  14. previous_snapshot="snapshot_prev"
  15.  
  16. # BTRFS-Subvolume für den aktuellen Snapshot
  17. current_snapshot="snapshot_$(date +%Y%m%d%H%M%S)"
  18.  
  19. # Pfad zum Bandgerät
  20. tape_device="/dev/nst0"
  21.  
  22. # Schreibgeschwindigkeit des Bands in MB/sec
  23. tape_speed=350
  24.  
  25. # E-Mail-Einstellungen
  26. email_recipient="empfänger@example.com"
  27. email_sender="absender@example.com"
  28. email_subject="Sicherungsstatus"
  29.  
  30. # SMTP Relay-Einstellungen
  31. smtp_server="smtp.example.com"
  32. smtp_port="587"
  33. smtp_username="benutzername"
  34. smtp_password="passwort"
  35.  
  36. # Funktion zum Senden einer E-Mail
  37. send_email() {
  38. echo "$1" | mailx -s "$email_subject" -r "$email_sender" -S smtp="$smtp_server:$smtp_port" \
  39. -S smtp-use-starttls -S smtp-auth=login -S smtp-auth-user="$smtp_username" \
  40. -S smtp-auth-password="$smtp_password" "$email_recipient"
  41. }
  42.  
  43. # Funktion zur Schätzung der Sicherungsdauer
  44. estimate_backup_duration() {
  45. local source_size=$(du -sb "$tmp_subvolume/$previous_snapshot" | awk '{print $1}')
  46. local estimated_duration=$(bc <<< "scale=2; $source_size / $tape_speed")
  47. send_email "Die geschätzte Sicherungsdauer beträgt ca. $estimated_duration Sekunden."
  48. }
  49.  
  50. # Funktion zum Erstellen eines Snapshots
  51. create_snapshot() {
  52. # Vorherigen Snapshot umbenennen
  53. if btrfs subvolume list "$tmp_subvolume/$previous_snapshot" &> /dev/null; then
  54. btrfs subvolume delete "$tmp_subvolume/$previous_snapshot"
  55. fi
  56. mv "$tmp_subvolume/$current_snapshot" "$tmp_subvolume/$previous_snapshot" &> /dev/null || true
  57.  
  58. # Neues Snapshot-Subvolume erstellen
  59. btrfs subvolume snapshot "$source_dir1" "$tmp_subvolume/$current_snapshot"
  60. btrfs subvolume snapshot "$source_dir2" "$tmp_subvolume/$current_snapshot"
  61. }
  62.  
  63. # Funktion zum Schreiben des inkrementellen Backups auf Band
  64. write_backup_to_tape() {
  65. # Band vorspulen
  66. mt -f "$tape_device" rewind
  67. mt -f "$tape_device" fsf 1
  68.  
  69. # Sicherungsdauer schätzen
  70. estimate_backup_duration
  71.  
  72. # Sicherung auf Band schreiben
  73. tar -cvf "$tape_device" --listed-incremental="$tmp_subvolume/incremental.list" "$tmp_subvolume/$current_snapshot"
  74. }
  75.  
  76. # Funktion zur Auswertung der Sicherung
  77. finalize_backup() {
  78. local backup_exit_code=$?
  79.  
  80. # Band zurückspulen und auswerfen
  81. mt -f "$tape_device" rewind
  82. mt -f "$tape_device" offline
  83.  
  84. # Erfolgsmeldung oder Misserfolgsmeldung senden
  85. if [ $backup_exit_code -eq 0 ]; then
  86. send_email "Die Sicherung wurde erfolgreich abgeschlossen."
  87. else
  88. send_email "Die Sicherung ist fehlgeschlagen."
  89. fi
  90. }
  91.  
  92. # Sicherung abbrechen, wenn das Band voll ist
  93. trap "send_email 'Das Band ist voll. Die Sicherung wurde abgebrochen.'" SIGXFSZ
  94.  
  95. # Hauptprogramm
  96. btrfs subvolume create "$tmp_subvolume"
  97. create_snapshot
  98. write_backup_to_tape
  99. finalize_backup
  100.  
  101. # Temporäres Subvolume löschen
  102. btrfs subvolume delete "$tmp_subvolume"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement