Add automated daily backup script
This commit is contained in:
1 parent
f164832dcc
commit
417cbacd6b
1 file changed
+90
@@ -0,0 +1,90 @@
|
||||
# Daily Backup Script for niggers workspace
|
||||
$ErrorActionPreference = "Continue"
|
||||
|
||||
$Workspace = "V:\smalldata\niggers"
|
||||
$LogFile = "$Workspace\daily_backup.log"
|
||||
|
||||
Function Log-Message($msg) {
|
||||
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
||||
"$timestamp - $msg" | Out-File -FilePath $LogFile -Append -Encoding UTF8
|
||||
Write-Output "$timestamp - $msg"
|
||||
}
|
||||
|
||||
Log-Message "=== Starting Daily Backup ==="
|
||||
|
||||
if (-not (Test-Path $Workspace)) {
|
||||
Log-Message "ERROR: Workspace directory $Workspace is not accessible."
|
||||
exit 1
|
||||
}
|
||||
|
||||
Set-Location $Workspace
|
||||
|
||||
# 1. Regenerate structure manifest & tree summary
|
||||
Log-Message "Updating folder structure manifest and tree summary..."
|
||||
try {
|
||||
python "$Workspace\generate_manifest.py" 2>&1 | Out-File -FilePath $LogFile -Append -Encoding UTF8
|
||||
} catch {
|
||||
Log-Message "WARNING: generate_manifest.py failed: $_"
|
||||
}
|
||||
|
||||
# 2. Add safe directory
|
||||
git config --global --add safe.directory * 2>$null
|
||||
|
||||
# 3. Stage changes
|
||||
Log-Message "Staging modified/new files..."
|
||||
$stagePy = @"
|
||||
import os, subprocess, tempfile
|
||||
|
||||
root = r'V:\smalldata\niggers'
|
||||
target_exts = {'.py', '.txt', '.json', '.md', '.sh', '.bat', '.ps1', '.csv', '.log'}
|
||||
excluded_dirs = {'.git', '__pycache__', '.omo'}
|
||||
|
||||
all_target = []
|
||||
for dirpath, dirnames, filenames in os.walk(root):
|
||||
dirnames[:] = [d for d in dirnames if d not in excluded_dirs]
|
||||
for f in filenames:
|
||||
if f == 'daily_backup.log':
|
||||
continue
|
||||
ext = os.path.splitext(f)[1].lower()
|
||||
if ext in target_exts:
|
||||
full = os.path.join(dirpath, f)
|
||||
rel = os.path.relpath(full, root).replace('\\', '/')
|
||||
all_target.append(rel)
|
||||
|
||||
with tempfile.NamedTemporaryFile('w', delete=False, encoding='utf-8') as tf:
|
||||
tf.write('\n'.join(all_target) + '\n')
|
||||
tmp_path = tf.name
|
||||
|
||||
try:
|
||||
subprocess.run(['git', 'add', f'--pathspec-from-file={tmp_path}'], cwd=root)
|
||||
finally:
|
||||
if os.path.exists(tmp_path):
|
||||
os.remove(tmp_path)
|
||||
"@
|
||||
|
||||
python -c $stagePy 2>&1 | Out-File -FilePath $LogFile -Append -Encoding UTF8
|
||||
|
||||
# 4. Check if there are changes staged
|
||||
$status = (git status --porcelain=v1)
|
||||
if (-not $status) {
|
||||
Log-Message "No changes to commit. Backup is up-to-date."
|
||||
Log-Message "=== Daily Backup Completed ==="
|
||||
exit 0
|
||||
}
|
||||
|
||||
# 5. Commit changes
|
||||
$commitDate = Get-Date -Format "yyyy-MM-dd HH:mm"
|
||||
Log-Message "Committing changes..."
|
||||
git commit -m "Automated daily backup: $commitDate" 2>&1 | Out-File -FilePath $LogFile -Append -Encoding UTF8
|
||||
|
||||
# 6. Push to Gitea
|
||||
Log-Message "Pushing to Gitea..."
|
||||
git push origin main 2>&1 | Out-File -FilePath $LogFile -Append -Encoding UTF8
|
||||
|
||||
if ($LASTEXITCODE -eq 0) {
|
||||
Log-Message "SUCCESS: Pushed daily backup to Gitea."
|
||||
} else {
|
||||
Log-Message "ERROR: Failed to push to Gitea (Exit code: $LASTEXITCODE)."
|
||||
}
|
||||
|
||||
Log-Message "=== Daily Backup Completed ==="
|
||||
Reference in new issue
Block a user