J2897 icon

Scheduled n8n Backups

J2897 | PRO | 08/21/24 10:27:20 PM UTC (Edited) | 0 ⭐ | 8025 👁️ | Never ⏰ | []
PowerShell |

3.23 KB

|

None

|

0 👍

/

0 👎

# Adjust window size
$host.UI.RawUI.WindowSize = New-Object Management.Automation.Host.Size(80, 40)
$host.UI.RawUI.BufferSize = New-Object Management.Automation.Host.Size(80, 3000)
 
# Configuration
$sevenZip = "C:\Program Files\7-Zip\7z.exe"
$sourceDir = "$env:USERPROFILE\.n8n"
$destination = "X:\Storage\.n8n\backup.zip"
$timestampFile = "$env:USERPROFILE\.n8n\backup_timestamps.txt"
$password = $env:SEVENZIP_PASSWORD
 
# Check if n8n is running
$n8nProcesses = Get-WmiObject Win32_Process | Where-Object { 
    $_.CommandLine -like "*npx*" -and $_.CommandLine -like "*n8n*" -and $_.CommandLine -notlike "*powershell*"
}
if ($n8nProcesses) {
    Write-Host "n8n is currently running. Backup will not proceed."
    exit 0
}
 
# Check if 7-Zip exists
if (!(Test-Path -Path $sevenZip)) {
    Write-Error "7-Zip is not installed or the path is incorrect."
    exit 1
}
 
# Check if the password is set
if ([string]::IsNullOrEmpty($password)) {
    Write-Error "Backup password is not set. Please set the SEVENZIP_PASSWORD environment variable."
    exit 1
}
 
# Retrieve last backup timestamps
$lastTimestamps = @{}
if (Test-Path -Path $timestampFile) {
    Get-Content -Path $timestampFile | ForEach-Object {
        $parts = $_.Split(" ", 2)
        if ($parts.Count -eq 2) {
            $lastTimestamps[$parts[0]] = [long]$parts[1]
        } else {
            Write-Error "Timestamp file format is incorrect."
            exit 1
        }
    }
}
 
# Initialize current timestamps hashtable
$currentTimestamps = @{}
 
# Get current timestamps
foreach ($file in "config", "database.sqlite") {
    $filePath = Join-Path -Path $sourceDir -ChildPath $file
    if (Test-Path -Path $filePath) {
        try {
            $currentTimestamps[$file] = (Get-Item -Path $filePath).LastWriteTimeUtc.ToFileTimeUtc()
        } catch {
            Write-Error "Failed to get LastWriteTimeUtc for file: $file"
            exit 1
        }
    } else {
        Write-Error "Source file '$file' does not exist in the directory '$sourceDir'."
        exit 1
    }
}
 
# Compare timestamps and decide if backup is needed
$needBackup = $false
foreach ($file in $currentTimestamps.Keys) {
    if (!($lastTimestamps.ContainsKey($file)) -or $currentTimestamps[$file] -gt $lastTimestamps[$file]) {
        $needBackup = $true
        break
    }
}
 
# Perform backup if needed
if ($needBackup) {
    Write-Host "Backing up files..."
    try {
        & $sevenZip a -tzip -p$password $destination (Join-Path -Path $sourceDir -ChildPath "database.sqlite") (Join-Path -Path $sourceDir -ChildPath "config")
        
        # Check if the backup was created successfully
        if (!(Test-Path -Path $destination)) {
            throw "Backup creation failed."
        }
 
        # Update timestamp file
        $output = @()
        foreach ($file in $currentTimestamps.Keys) {
            $output += "$file $($currentTimestamps[$file])"
        }
        Set-Content -Path $timestampFile -Value $output
        Write-Host "Backup completed."
    } catch {
        Write-Error "An error occurred during the backup process: $_"
        exit 1
    }
} else {
    Write-Host "No backup needed. Files are up-to-date."
}
 
Start-Sleep -Seconds 4

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎