Advertisement
J2897

update_n8n_mega

Sep 10th, 2024 (edited)
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.80 KB | None | 0 0
  1. #!/usr/bin/bash
  2.  
  3. # Automatic Update and Backup Script for n8n
  4. # ==========================================
  5. # This script checks for updates to the n8n Docker image and updates it if a newer version is available.
  6. # It also performs a backup of critical n8n data files before updating and uploads the backup to a MEGA cloud storage.
  7. #
  8. # The script uses Docker to compare the latest image digest from Docker Hub with the currently running image.
  9. # If an update is available, it stops the running container, backs up the database and configuration files,
  10. # uploads these backups to a local directory and MEGA, and then starts a new container with the updated image.
  11. #
  12. # Dependencies:
  13. #   - Docker
  14. #   - MEGA Command-Line Interface (mega-put)
  15. #
  16. # Usage:
  17. #   Simply run the script to check for updates, back up data, and update the n8n container if a newer version is available.
  18. #
  19. # To automate this script to run weekly, you can add it as a cron job. Here's how:
  20. #   1. Open the crontab editor by running the command `crontab -e` in your terminal.
  21. #   2. Add the following line to the end of the file: `0 18 * * 1 /path/to/update_n8n.sh`
  22. #   3. Replace `/path/to/update_n8n.sh` with the actual path to this script.
  23. #   4. Save and exit the editor.
  24. #
  25. # This will schedule the script to run every Monday at 6pm. You can adjust the schedule as needed by modifying the cron expression.
  26. #
  27. # Here's a breakdown of the cron expression:
  28. #   - `0 18 * * 1`: This means "run at minute 0, hour 18 (6pm), every day of the month, every month, on Monday (day of the week 1)".
  29. #
  30. # Backup Details:
  31. #   - Backups are stored locally in the specified directory and also uploaded to MEGA cloud storage.
  32. #   - Ensure that the MEGA CLI is set up and authenticated on your system before running the script.
  33.  
  34. # Variables
  35. IMAGE='n8nio/n8n'  # The Docker image
  36. CONTAINER_NAME='n8n'
  37.  
  38. # Backup locations
  39. BACKUP_DIR="/home/J2897/Backups/n8n"   # Local backup directory (ensure this exists)
  40. MEGA_REMOTE_DIR="/Backups/n8n"  # Remote MEGA backup directory
  41.  
  42. # Volume name (ensure this matches your Docker volume name)
  43. VOLUME_NAME='n8n_data'
  44.  
  45. # Temporary mount point for the volume within the user's home directory
  46. TEMP_MOUNT="/home/J2897/n8n_data_temp"
  47.  
  48. # Function to check if the container is running
  49. is_container_running() {
  50.   docker ps -q --filter "name=$CONTAINER_NAME" | grep -q .
  51. }
  52.  
  53. # Function to check if the container exists
  54. is_container_exist() {
  55.   docker ps -a -q --filter "name=$CONTAINER_NAME" | grep -q .
  56. }
  57.  
  58. # Function to get the latest image digest from Docker Hub
  59. get_latest_image_digest() {
  60.   docker pull $IMAGE > /dev/null 2>&1
  61.   docker inspect --format='{{index .RepoDigests 0}}' $IMAGE | awk -F@ '{print $2}'
  62. }
  63.  
  64. # Function to get the current running image's digest
  65. get_current_image_digest() {
  66.   if is_container_running; then
  67.     docker inspect --format='{{.Image}}' $(docker ps -q --filter "name=$CONTAINER_NAME") | xargs docker inspect --format='{{index .RepoDigests 0}}' | awk -F@ '{print $2}'
  68.   else
  69.     echo ""
  70.   fi
  71. }
  72.  
  73. # Function to back up n8n's database and config files from the volume
  74. backup_n8n_data() {
  75.   echo "$(date) - Starting backup of n8n data..."
  76.  
  77.   # Create the mount point with proper permissions
  78.   mkdir -p $TEMP_MOUNT || { echo "$(date) - Error: Could not create temporary mount directory." >&2; exit 1; }
  79.  
  80.   # Stop the running n8n container if it's running
  81.   if is_container_running; then
  82.     echo "$(date) - Stopping n8n container..."
  83.     docker stop $CONTAINER_NAME || { echo "$(date) - Error: Failed to stop n8n container." >&2; exit 1; }
  84.   else
  85.     echo "$(date) - Container $CONTAINER_NAME is not running; proceeding without stopping."
  86.   fi
  87.  
  88.   # Mount the n8n volume to the temporary directory with user permissions
  89.   echo "$(date) - Mounting n8n volume and copying data..."
  90.   docker run --rm -v $VOLUME_NAME:/data -v $TEMP_MOUNT:/mnt --user $(id -u):$(id -g) busybox sh -c "cp -r /data/. /mnt/" || { echo "$(date) - Error: Failed to copy data from volume." >&2; docker start $CONTAINER_NAME; exit 1; }
  91.  
  92.   # Check for existence and try copying the specific files to the backup directory
  93.   if [ -f "$TEMP_MOUNT/database.sqlite" ] && [ -f "$TEMP_MOUNT/config" ]; then
  94.     echo "$(date) - Copying database.sqlite and config to backup directory..."
  95.     cp $TEMP_MOUNT/database.sqlite $BACKUP_DIR/database.sqlite || { echo "$(date) - Error: Failed to copy database.sqlite." >&2; }
  96.     cp $TEMP_MOUNT/config $BACKUP_DIR/config || { echo "$(date) - Error: Failed to copy config." >&2; }
  97.     echo "$(date) - Files copied successfully."
  98.  
  99.     # Backup the files to MEGA
  100.     echo "$(date) - Uploading files to MEGA..."
  101.     mega-put -c $BACKUP_DIR/database.sqlite $MEGA_REMOTE_DIR/ || { echo "$(date) - Error: Failed to upload database.sqlite to MEGA." >&2; }
  102.     mega-put -c $BACKUP_DIR/config $MEGA_REMOTE_DIR/ || { echo "$(date) - Error: Failed to upload config to MEGA." >&2; }
  103.     echo "$(date) - Files uploaded to MEGA successfully."
  104.   else
  105.     echo "$(date) - Error: Backup files not found in the mounted directory. Aborting backup." >&2
  106.     ls -l $TEMP_MOUNT # List contents to debug
  107.     rm -rf $TEMP_MOUNT
  108.     docker start $CONTAINER_NAME
  109.     exit 1
  110.   fi
  111.  
  112.   # Clean up the temporary mount point
  113.   echo "$(date) - Cleaning up temporary mount directory..."
  114.   rm -rf $TEMP_MOUNT || { echo "$(date) - Error: Could not remove temporary mount directory." >&2; }
  115.  
  116.   # # Check if a stopped container exists with the same name, remove it
  117.   if is_container_exist; then
  118.     echo "$(date) - Removing existing stopped n8n container..."
  119.     docker rm $CONTAINER_NAME || { echo "$(date) - Error: Failed to remove existing stopped container." >&2; exit 1; }
  120.   fi
  121. }
  122.  
  123. # Get the latest digest from the repository
  124. LATEST_DIGEST=$(get_latest_image_digest)
  125.  
  126. # Get the currently running image's digest
  127. CURRENT_DIGEST=$(get_current_image_digest)
  128.  
  129. # Check if the digests are different, indicating an update is needed
  130. if [ -n "$CURRENT_DIGEST" ] && [ "$LATEST_DIGEST" != "$CURRENT_DIGEST" ]; then
  131.   echo "$(date) - A newer version of n8n is available. Updating..."
  132.  
  133.   # Backup n8n data before updating
  134.   backup_n8n_data
  135.  
  136.   # Pull the latest image and start the updated container
  137.   docker pull $IMAGE || { echo "$(date) - Error: Failed to pull the latest n8n image." >&2; exit 1; }
  138.  
  139.   # Start a new instance of the n8n container
  140.   echo "$(date) - Starting new n8n container..."
  141.   docker run -d --name $CONTAINER_NAME -p 5678:5678 \
  142.     -e GENERIC_TIMEZONE="Europe/London" -e TZ="Europe/London" \
  143.     -v $VOLUME_NAME:/home/node/.n8n $IMAGE || { echo "$(date) - Error: Failed to start a new n8n container." >&2; exit 1; }
  144.  
  145.   echo "$(date) - Update complete."
  146. else
  147.   echo "$(date) - n8n is up to date."
  148. fi
  149.  
  150. # Optional: Clean up unused Docker resources
  151. # docker image prune -f
  152. # docker volume prune -f
  153. # docker container prune -f
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement