Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Load environment variables from the .env file
- export $(grep -v '^#' "$(dirname "$0")/../.env" | xargs)
- ###########################################################################################################################################
- # Variables
- SOURCE_POOL="nas"
- BACKUP_POOL="backup-ssd"
- DATASET="immich"
- SNAPSHOT_NAME="daily-$(date +%Y-%m-%d)"
- SNAPSHOT_PATTERN="@daily-"
- TELEGRAM_MESSAGE="📢 *Immich Backup*%0A%0ASnapshot: $SNAPSHOT_NAME%0A%0A"
- ###########################################################################################################################################
- # Function to accumulate messages
- append_message() {
- local MESSAGE="$1"
- TELEGRAM_MESSAGE+="$MESSAGE%0A"
- }
- # Function to send telegram message
- send_telegram_message() {
- local MESSAGE="$1"
- $TELEGRAM_MESSAGE_SCRIPT "$TELEGRAM_MESSAGE" "$TELEGRAM_ZFS_TOKEN" "$TELEGRAM_ZFS_CHAT_ID"
- }
- ###########################################################################################################################################
- # Record the start time
- START_TIME=$(date +%s)
- ###########################################################################################################################################
- # Check if the snapshot exists
- SNAPSHOT_OUTPUT=$(zfs list -t snapshot | grep "$SOURCE_POOL/$DATASET@$SNAPSHOT_NAME" 2>&1)
- SNAPSHOT_STATUS=$?
- # Check if the command was successful
- if [ $SNAPSHOT_STATUS -eq 0 ]; then
- append_message "✅ Snapshot found"
- else
- append_message "❌ Snapshot not found"
- fi
- ###########################################################################################################################################
- # Try to unmount before sending snapshot
- if [ "$(zfs get -H -o value mounted "$BACKUP_POOL/$DATASET")" = "yes" ]; then
- UNMOUNT_OUTPUT=$(zfs unmount "$BACKUP_POOL/$DATASET" 2>&1)
- UNMOUNT_STATUS=$?
- # Check if the command was successful
- if [ $UNMOUNT_STATUS -eq 0 ]; then
- append_message "✅ Unmounted filesystem"
- else
- append_message "❌ Failed to unmount"
- fi
- else
- append_message "🔹 Unmount not executed"
- fi
- ###########################################################################################################################################
- if [ $UNMOUNT_STATUS -eq 0 ]; then
- # Get the latest snapshot on the source pool
- LATEST_SOURCE_SNAPSHOT=$(zfs list -H -t snapshot -o name -S creation "$SOURCE_POOL/$DATASET" | head -n 1)
- # Get all snapshots on source pool sorted by creation time
- ALL_SOURCE_SNAPSHOTS=($(zfs list -t snapshot -o name -s creation | grep "$SOURCE_DATASET@"))
- # Determine the oldest snapshot from the source
- OLDEST_SOURCE_SNAPSHOT="${ALL_SOURCE_SNAPSHOTS[0]}"
- # Get the latest snapshot on the destination pool
- LATEST_BACKUP_SNAPSHOT=$(zfs list -H -t snapshot -o name -S creation "$BACKUP_POOL/$DATASET" 2>/dev/null | grep $SNAPSHOT_PATTERN | head -n 1)
- # Ensure the source has at least one snapshot
- if [[ -z "$LATEST_SOURCE_SNAPSHOT" ]]; then
- append_message "❌ No snapshots found"
- fi
- # If the destination has no snapshots, a full send is required
- if [[ -z "$LATEST_BACKUP_SNAPSHOT" ]]; then
- append_message "⚠️ No previous snapshot"
- append_message "🔹 Manually equalize"
- fi
- # Extract the snapshot names
- LATEST_SOURCE_NAME="${LATEST_SOURCE_SNAPSHOT#*@}"
- LATEST_BACKUP_NAME="${LATEST_BACKUP_SNAPSHOT#*@}"
- # If the latest snapshots are the same, no transfer is needed
- if [[ "$LATEST_SOURCE_NAME" == "$LATEST_BACKUP_NAME" ]]; then
- append_message "✅ Snapshots are already synced"
- else
- # Send incremental backup
- SEND_OUTPUT=$( { zfs send -i "$SOURCE_POOL/$DATASET@$LATEST_BACKUP_NAME" "$SOURCE_POOL/$DATASET@$LATEST_SOURCE_NAME" | zfs receive -F "$BACKUP_POOL/$DATASET"; } 2>&1 )
- SEND_STATUS=$?
- # Check if the command was successful
- if [[ $SEND_STATUS -eq 0 ]]; then
- append_message "✅ Transferred snapshot"
- else
- append_message "❌ Error transferring partial backup"
- fi
- fi
- # Get the logicalreferenced values
- SOURCE_LOGICAL=$(zfs list -o logicalreferenced "$SOURCE_POOL/$DATASET" | awk 'NR==2 {print $1}')
- BACKUP_LOGICAL=$(zfs list -o logicalreferenced "$BACKUP_POOL/$DATASET" | awk 'NR==2 {print $1}')
- # Compare values
- if [[ "$SOURCE_LOGICAL" == "$BACKUP_LOGICAL" ]]; then
- append_message "✅ Backup is identical"
- else
- append_message "❌ Backup is not identical"
- fi
- else
- append_message "🔹 Transferring not executed"
- fi
- ###########################################################################################################################################
- # Remount the dataset
- if [ "$(zfs get -H -o value mounted "$BACKUP_POOL/$DATASET")" != "yes" ]; then
- MOUNT_OUTPUT=$(zfs mount "$BACKUP_POOL/$DATASET" 2>&1)
- MOUNT_STATUS=$?
- if [ "$MOUNT_STATUS" -eq 0 ]; then
- append_message "✅ Remounted filesystem"
- else
- append_message "❌ Remounted failed"
- fi
- else
- append_message "🔹 Remounted not executed"
- fi
- ###########################################################################################################################################
- # Record the end time
- END_TIME=$(date +%s)
- # Calculate total execution time in seconds
- ELAPSED_TIME=$((END_TIME - START_TIME))
- # Convert to minutes and seconds format
- MINUTES=$((ELAPSED_TIME / 60))
- SECONDS=$((ELAPSED_TIME % 60))
- # Append execution time to the message log
- append_message "%0A⏳ Total time: ${MINUTES}m ${SECONDS}s"
- ###########################################################################################################################################
- # Append backup size
- TOTAL_SIZE=$(zfs list -o logicalreferenced "$SOURCE_POOL/$DATASET" | awk 'NR==2 {print $1}')
- append_message "📂 Total size: $TOTAL_SIZE"
- ###########################################################################################################################################
- # Append snapshots total size
- SNAPSHOTS_TOTAL_SIZE=$(zfs get -Hp usedbysnapshots | awk '{s+=$3} END {printf "%.2fG\n", s/(1024*1024*1024)}')
- append_message "📸 Snapshots size: $SNAPSHOTS_TOTAL_SIZE"
- ###########################################################################################################################################
- # Append unmount output logs to the message log
- if [[ $UNMOUNT_STATUS -ne 0 ]]; then
- append_message "%0A\`\`\`Error $UNMOUNT_OUTPUT\`\`\`"
- fi
- ###########################################################################################################################################
- # Append send output logs to the message log
- if [[ $SEND_STATUS -ne 0 ]]; then
- append_message "%0A\`\`\`Error $SEND_OUTPUT\`\`\`"
- fi
- ###########################################################################################################################################
- # Append mount output logs to the message log
- if [[ $MOUNT_STATUS -ne 0 ]]; then
- append_message "%0A\`\`\`Error $MOUNT_OUTPUT\`\`\`"
- fi
- ###########################################################################################################################################
- # Send the telegram message
- send_telegram_message
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement