Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Verzeichnisse für die Sicherung
- source_dir1="/home/server"
- source_dir2="/home/server2"
- # Temporäres Btrfs-Subvolume für die Snapshots
- tmp_subvolume="/tmp/snapshot_tmp"
- # Zielverzeichnis für die Snapshots
- snapshot_dir="/home/asaj/snapshots"
- # BTRFS-Subvolume für den vorherigen Snapshot
- previous_snapshot="snapshot_prev"
- # BTRFS-Subvolume für den aktuellen Snapshot
- current_snapshot="snapshot_$(date +%Y%m%d%H%M%S)"
- # Pfad zum Bandgerät
- tape_device="/dev/nst0"
- # Schreibgeschwindigkeit des Bands in MB/sec
- tape_speed=350
- # E-Mail-Einstellungen
- email_recipient="empfänger@example.com"
- email_sender="absender@example.com"
- email_subject="Sicherungsstatus"
- # SMTP Relay-Einstellungen
- smtp_server="smtp.example.com"
- smtp_port="587"
- smtp_username="benutzername"
- smtp_password="passwort"
- # Funktion zum Senden einer E-Mail
- send_email() {
- echo "$1" | mailx -s "$email_subject" -r "$email_sender" -S smtp="$smtp_server:$smtp_port" \
- -S smtp-use-starttls -S smtp-auth=login -S smtp-auth-user="$smtp_username" \
- -S smtp-auth-password="$smtp_password" "$email_recipient"
- }
- # Funktion zur Schätzung der Sicherungsdauer
- estimate_backup_duration() {
- local source_size=$(du -sb "$tmp_subvolume/$previous_snapshot" | awk '{print $1}')
- local estimated_duration=$(bc <<< "scale=2; $source_size / $tape_speed")
- send_email "Die geschätzte Sicherungsdauer beträgt ca. $estimated_duration Sekunden."
- }
- # Funktion zum Erstellen eines Snapshots
- create_snapshot() {
- # Vorherigen Snapshot umbenennen
- if btrfs subvolume list "$tmp_subvolume/$previous_snapshot" &> /dev/null; then
- btrfs subvolume delete "$tmp_subvolume/$previous_snapshot"
- fi
- mv "$tmp_subvolume/$current_snapshot" "$tmp_subvolume/$previous_snapshot" &> /dev/null || true
- # Neues Snapshot-Subvolume erstellen
- btrfs subvolume snapshot "$source_dir1" "$tmp_subvolume/$current_snapshot"
- btrfs subvolume snapshot "$source_dir2" "$tmp_subvolume/$current_snapshot"
- }
- # Funktion zum Schreiben des inkrementellen Backups auf Band
- write_backup_to_tape() {
- # Band vorspulen
- mt -f "$tape_device" rewind
- mt -f "$tape_device" fsf 1
- # Sicherungsdauer schätzen
- estimate_backup_duration
- # Sicherung auf Band schreiben
- tar -cvf "$tape_device" --listed-incremental="$tmp_subvolume/incremental.list" "$tmp_subvolume/$current_snapshot"
- }
- # Funktion zur Auswertung der Sicherung
- finalize_backup() {
- local backup_exit_code=$?
- # Band zurückspulen und auswerfen
- mt -f "$tape_device" rewind
- mt -f "$tape_device" offline
- # Erfolgsmeldung oder Misserfolgsmeldung senden
- if [ $backup_exit_code -eq 0 ]; then
- send_email "Die Sicherung wurde erfolgreich abgeschlossen."
- else
- send_email "Die Sicherung ist fehlgeschlagen."
- fi
- }
- # Sicherung abbrechen, wenn das Band voll ist
- trap "send_email 'Das Band ist voll. Die Sicherung wurde abgebrochen.'" SIGXFSZ
- # Hauptprogramm
- btrfs subvolume create "$tmp_subvolume"
- create_snapshot
- write_backup_to_tape
- finalize_backup
- # Temporäres Subvolume löschen
- btrfs subvolume delete "$tmp_subvolume"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement