Advertisement
J2897

Scheduled n8n Backups

Aug 2nd, 2024 (edited)
671
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 3.68 KB | None | 0 0
  1. @echo off
  2. setlocal
  3.  
  4. REM Backup Script for n8n Configuration and Database Files
  5.  
  6. :: This will automate the process of backing up the 'config' and 'database.sqlite' files
  7. :: from n8n's main folder to a specified destination as a password-protected 'backup.zip' file.
  8. :: The backup is performed only if either of these files have been modified since the last backup.
  9. ::
  10. :: The script uses 7-Zip for compression and encryption, and PowerShell for file timestamp retrieval.
  11. ::
  12. :: Key Features:
  13. :: - Checks if 7-Zip and PowerShell are installed.
  14. :: - Retrieves and compares the last modification timestamps of the files.
  15. :: - Performs a backup only if the files have been modified since the last backup.
  16. :: - Writes human-readable timestamps to the backup timestamps file.
  17. ::
  18. :: Configuration:
  19. :: - SEVENZIP: Path to the 7-Zip executable.
  20. :: - SOURCE_DIR: Directory containing the files to back up.
  21. :: - DESTINATION: Path to the backup ZIP file.
  22. :: - TIMESTAMP_FILE: File to store the last backup timestamps.
  23. :: - PASSWORD: Password for the encrypted ZIP file.
  24.  
  25. :: Configuration
  26. set "SEVENZIP=C:\Program Files\7-Zip\7z.exe"
  27. set "SOURCE_DIR=%USERPROFILE%\.n8n"
  28. set "DESTINATION=X:\Storage\.n8n\backup.zip"
  29. set "TIMESTAMP_FILE=%USERPROFILE%\.n8n\backup_timestamps.txt"
  30. set "PASSWORD=password123" || REM Set the 7-Zip password to encrypt the backups
  31.  
  32. :: Check if 7-Zip exists
  33. if not exist "%SEVENZIP%" (
  34.     echo 7-Zip is not installed or the path is incorrect.
  35.     exit /b 1
  36. )
  37.  
  38. :: Check if PowerShell is installed
  39. powershell -Command "Write-Host 'PowerShell is working'" >nul 2>&1
  40. if errorlevel 1 (
  41.     echo PowerShell is not installed or not accessible.
  42.     exit /b 1
  43. )
  44.  
  45. :: Retrieve last backup timestamps
  46. if exist "%TIMESTAMP_FILE%" (
  47.     for /f "tokens=1,*" %%A in (%TIMESTAMP_FILE%) do (
  48.         if "%%A"=="config" set LAST_CONFIG_TIMESTAMP_HUMAN=%%B
  49.         if "%%A"=="database.sqlite" set LAST_DATABASE_TIMESTAMP_HUMAN=%%B
  50.     )
  51. )
  52.  
  53. :: Debug output to verify last timestamps
  54. echo Last config timestamp (human-readable): %LAST_CONFIG_TIMESTAMP_HUMAN%
  55. echo Last database.sqlite timestamp (human-readable): %LAST_DATABASE_TIMESTAMP_HUMAN%
  56.  
  57. :: Get current timestamps using PowerShell
  58. for %%F in (config database.sqlite) do (
  59.     echo Fetching timestamp for %%F
  60.     for /f "delims=" %%T in ('powershell -Command "Get-Item \"%SOURCE_DIR%\%%F\" | Select-Object -ExpandProperty LastWriteTime | ForEach-Object { $_.ToString('yyyy-MM-dd HH:mm:ss') }"') do (
  61.         set "CURRENT_%%F_TIMESTAMP_HUMAN=%%T"
  62.     )
  63. )
  64.  
  65. :: Debug output to verify current timestamps
  66. echo Current config timestamp (human-readable): %CURRENT_config_TIMESTAMP_HUMAN%
  67. echo Current database.sqlite timestamp (human-readable): %CURRENT_database.sqlite_TIMESTAMP_HUMAN%
  68.  
  69. :: Compare timestamps and decide if backup is needed
  70. set NEED_BACKUP=0
  71.  
  72. if not defined LAST_CONFIG_TIMESTAMP_HUMAN (
  73.     set NEED_BACKUP=1
  74. ) else (
  75.     if "%CURRENT_config_TIMESTAMP_HUMAN%" gtr "%LAST_CONFIG_TIMESTAMP_HUMAN%" (
  76.         set NEED_BACKUP=1
  77.     )
  78. )
  79.  
  80. if not defined LAST_DATABASE_TIMESTAMP_HUMAN (
  81.     set NEED_BACKUP=1
  82. ) else (
  83.     if "%CURRENT_database.sqlite_TIMESTAMP_HUMAN%" gtr "%LAST_DATABASE_TIMESTAMP_HUMAN%" (
  84.         set NEED_BACKUP=1
  85.     )
  86. )
  87.  
  88. :: Perform backup if needed
  89. if "%NEED_BACKUP%"=="1" (
  90.     echo Backing up files...
  91.     "%SEVENZIP%" a -tzip -p%PASSWORD% "%DESTINATION%" "%SOURCE_DIR%\database.sqlite" "%SOURCE_DIR%\config"
  92.  
  93.     :: Update timestamp file
  94.     > "%TIMESTAMP_FILE%" (
  95.         echo config %CURRENT_config_TIMESTAMP_HUMAN%
  96.         echo database.sqlite %CURRENT_database.sqlite_TIMESTAMP_HUMAN%
  97.     )
  98.     echo Backup completed.
  99. ) else (
  100.     echo No backup needed. Files are up-to-date.
  101. )
  102.  
  103. endlocal
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement