Advertisement
J2897

Scheduled n8n Backups

Aug 2nd, 2024 (edited)
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Adjust window size
  2. $host.UI.RawUI.WindowSize = New-Object Management.Automation.Host.Size(80, 40)
  3. $host.UI.RawUI.BufferSize = New-Object Management.Automation.Host.Size(80, 3000)
  4.  
  5. # Configuration
  6. $sevenZip = "C:\Program Files\7-Zip\7z.exe"
  7. $sourceDir = "$env:USERPROFILE\.n8n"
  8. $destination = "X:\Storage\.n8n\backup.zip"
  9. $timestampFile = "$env:USERPROFILE\.n8n\backup_timestamps.txt"
  10. $password = $env:SEVENZIP_PASSWORD
  11.  
  12. # Check if n8n is running
  13. $n8nProcesses = Get-WmiObject Win32_Process | Where-Object {
  14.     $_.CommandLine -like "*npx*" -and $_.CommandLine -like "*n8n*" -and $_.CommandLine -notlike "*powershell*"
  15. }
  16. if ($n8nProcesses) {
  17.     Write-Host "n8n is currently running. Backup will not proceed."
  18.     exit 0
  19. }
  20.  
  21. # Check if 7-Zip exists
  22. if (!(Test-Path -Path $sevenZip)) {
  23.     Write-Error "7-Zip is not installed or the path is incorrect."
  24.     exit 1
  25. }
  26.  
  27. # Check if the password is set
  28. if ([string]::IsNullOrEmpty($password)) {
  29.     Write-Error "Backup password is not set. Please set the SEVENZIP_PASSWORD environment variable."
  30.     exit 1
  31. }
  32.  
  33. # Retrieve last backup timestamps
  34. $lastTimestamps = @{}
  35. if (Test-Path -Path $timestampFile) {
  36.     Get-Content -Path $timestampFile | ForEach-Object {
  37.         $parts = $_.Split(" ", 2)
  38.         if ($parts.Count -eq 2) {
  39.             $lastTimestamps[$parts[0]] = [long]$parts[1]
  40.         } else {
  41.             Write-Error "Timestamp file format is incorrect."
  42.             exit 1
  43.         }
  44.     }
  45. }
  46.  
  47. # Initialize current timestamps hashtable
  48. $currentTimestamps = @{}
  49.  
  50. # Get current timestamps
  51. foreach ($file in "config", "database.sqlite") {
  52.     $filePath = Join-Path -Path $sourceDir -ChildPath $file
  53.     if (Test-Path -Path $filePath) {
  54.         try {
  55.             $currentTimestamps[$file] = (Get-Item -Path $filePath).LastWriteTimeUtc.ToFileTimeUtc()
  56.         } catch {
  57.             Write-Error "Failed to get LastWriteTimeUtc for file: $file"
  58.             exit 1
  59.         }
  60.     } else {
  61.         Write-Error "Source file '$file' does not exist in the directory '$sourceDir'."
  62.         exit 1
  63.     }
  64. }
  65.  
  66. # Compare timestamps and decide if backup is needed
  67. $needBackup = $false
  68. foreach ($file in $currentTimestamps.Keys) {
  69.     if (!($lastTimestamps.ContainsKey($file)) -or $currentTimestamps[$file] -gt $lastTimestamps[$file]) {
  70.         $needBackup = $true
  71.         break
  72.     }
  73. }
  74.  
  75. # Perform backup if needed
  76. if ($needBackup) {
  77.     Write-Host "Backing up files..."
  78.     try {
  79.         & $sevenZip a -tzip -p$password $destination (Join-Path -Path $sourceDir -ChildPath "database.sqlite") (Join-Path -Path $sourceDir -ChildPath "config")
  80.        
  81.         # Check if the backup was created successfully
  82.         if (!(Test-Path -Path $destination)) {
  83.             throw "Backup creation failed."
  84.         }
  85.  
  86.         # Update timestamp file
  87.         $output = @()
  88.         foreach ($file in $currentTimestamps.Keys) {
  89.             $output += "$file $($currentTimestamps[$file])"
  90.         }
  91.         Set-Content -Path $timestampFile -Value $output
  92.         Write-Host "Backup completed."
  93.     } catch {
  94.         Write-Error "An error occurred during the backup process: $_"
  95.         exit 1
  96.     }
  97. } else {
  98.     Write-Host "No backup needed. Files are up-to-date."
  99. }
  100.  
  101. Start-Sleep -Seconds 4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement