Advertisement
ighormaia

ZFS Create Snapshot

Mar 5th, 2025
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.48 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Load environment variables from the .env file
  4. export $(grep -v '^#' "$(dirname "$0")/../.env" | xargs)
  5.  
  6. ###########################################################################################################################################
  7.  
  8. # Variables
  9. SOURCE_POOL="nas"
  10. DATASET="immich"
  11. SNAPSHOT_NAME="daily-$(date +%Y-%m-%d)"
  12. TELEGRAM_MESSAGE="📢 *Immich Snapshot*%0A%0ASnapshot: $SNAPSHOT_NAME%0A%0A"
  13.  
  14. ###########################################################################################################################################
  15.  
  16. # Function to accumulate messages
  17. append_message() {
  18.     local MESSAGE="$1"
  19.     TELEGRAM_MESSAGE+="$MESSAGE%0A"
  20. }
  21.  
  22. # Function to send telegram message
  23. send_telegram_message() {
  24.     local MESSAGE="$1"
  25.     $TELEGRAM_MESSAGE_SCRIPT "$TELEGRAM_MESSAGE" "$TELEGRAM_ZFS_TOKEN" "$TELEGRAM_ZFS_CHAT_ID"
  26. }
  27.  
  28. ###########################################################################################################################################
  29.  
  30. # Record the start time
  31. START_TIME=$(date +%s)
  32.  
  33. ###########################################################################################################################################
  34.  
  35. # Create the snapshot and capture output
  36. SNAPSHOT_OUTPUT=$(zfs snapshot -r "$SOURCE_POOL/$DATASET@$SNAPSHOT_NAME" 2>&1)
  37. SNAPSHOT_STATUS=$?
  38.  
  39. # Check if the command was successful
  40. if [[ $SNAPSHOT_STATUS -eq 0 ]]; then
  41.     append_message "✅ Snapshot created"
  42. else
  43.     append_message "❌ Snapshot creation failed"
  44. fi
  45.  
  46. ###########################################################################################################################################
  47.  
  48. # Record the end time
  49. END_TIME=$(date +%s)
  50.  
  51. # Calculate total execution time in seconds
  52. ELAPSED_TIME=$((END_TIME - START_TIME))
  53.  
  54. # Convert to minutes and seconds format
  55. MINUTES=$((ELAPSED_TIME / 60))
  56. SECONDS=$((ELAPSED_TIME % 60))
  57.  
  58. # Append execution time to the message log
  59. append_message "%0A⏳ Total time: ${MINUTES}m ${SECONDS}s"
  60.  
  61. ###########################################################################################################################################
  62.  
  63. # Create snapshot output logs to the message log
  64. if [[ $SNAPSHOT_STATUS -ne 0 ]]; then
  65.     append_message "%0A\`\`\`Error $SNAPSHOT_OUTPUT\`\`\`"
  66. fi
  67.  
  68. ###########################################################################################################################################
  69.  
  70. # Send the telegram message
  71. send_telegram_message
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement