脚本backup.ps1
#
Set-ExecutionPolicy Bypass -Scope Process -Force
Write-Host "正在备份文件,请勿关闭...."
$SOURCE_DIR = "D:\data\app\images"
$BACKUP_DIR = "D:\data\backup"
$TIMESTAMP = (Get-Date).ToString('yyyyMMdd_HHmmss')
$BACKUP_FILE = "backup_$TIMESTAMP.zip"
$LOG_FILE = "D:\data\backup\backup_$TIMESTAMP.log"
$FILES_FOUND = $false
$TEMP_DIR = "$env:TEMP\backup_temp"
New-Item -ItemType Directory -Path $TEMP_DIR -Force | Out-Null
Start-Transcript -Path $LOG_FILE -Append
Write-Host "Backup started at $(Get-Date)"
Write-Host "Source directory: $SOURCE_DIR"
Write-Host "Backup directory: $BACKUP_DIR"
Write-Host "Temporary directory: $TEMP_DIR"
$startTime = (Get-Date).AddHours(-12)
$endTime = (Get-Date)
$formattedStartTime = $startTime.ToString("yyyy-MM-dd HH:mm:ss")
$formattedEndTime = $endTime.ToString("yyyy-MM-dd HH:mm:ss")
Write-Host "Finding and copying files for $formattedStartTime to $formattedEndTime to a temporary directory"
Get-ChildItem -Path $SOURCE_DIR -Recurse | Where-Object {$_.CreationTime -ge $startTime -and $_.CreationTime -le $endTime} | ForEach-Object {
$relativePath = $_.FullName.Substring($SOURCE_DIR.Length)
$targetPath = Join-Path $TEMP_DIR $relativePath
if (-not (Test-Path $targetPath)) {
New-Item -ItemType Directory -Path (Split-Path $targetPath -Parent) -Force | Out-Null
}
Copy-Item $_.FullName -Destination $targetPath -Recurse -Force
Write-Host "Copied: $_.FullName"
$FILES_FOUND = $true
}
if (-not (Test-Path $BACKUP_DIR)) {
New-Item -ItemType Directory -Path $BACKUP_DIR | Out-Null
}
if ($FILES_FOUND) {
Write-Host "Package and compress backup files..."
Compress-Archive -Path "$TEMP_DIR\*" -DestinationPath "$BACKUP_DIR\$BACKUP_FILE" -Update
if ($?) {
Write-Host "Backup file created: $BACKUP_DIR\$BACKUP_FILE"
Write-Host "Delete original files..."
Get-ChildItem -Path $SOURCE_DIR -Recurse | Where-Object {$_.CreationTime -ge $startTime -and $_.CreationTime -le $endTime} | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue
} else {
Write-Host "Error creating backup file"
}
} else {
Write-Host "The file does not exist and will not be backed up"
}
Remove-Item -Path $TEMP_DIR -Recurse -Force
if ($FILES_FOUND) {
Write-Host "Backup completed at $(Get-Date)"
Write-Host "Backup completed: $BACKUP_DIR\$BACKUP_FILE"
}
Stop-Transcript
windows定时执行#
- 进入任务计划程序

- 新增定时任务

- 定时任务操作

程序或脚本:%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
添加参数:-File D:\backup.ps1
- 触发器不做介绍
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!