文章目录
PowerShell脚本
简介:
由于我这里需要使用软件才能够链接到服务器,所以加了处理
如果可以直连,注释测试网络链接函数中的restartVPN即可
文件名:startbackup.ps1
powershell
# 定义变量
$ip = "192.168.10.204"
$port = 873
$processName = "c44e38b067efcf9b7f5d02dcb3d4caae" #VPN进程名,用于结束程序
$exePath = "C:\Users\Administrator\AppData\Local\c44e38b067efcf9b7f5d02dcb3d4caae\$processName.exe" # VPN程序位置,用于启动
$date = Get-Date -Format "yyyy-MM-dd" # 格式化时间,用于重命名同步日志文件
$scriptLogFile = "script_log.txt" # 脚本日志
$rsyncLogFile = "$date-rsync_log.txt" # 同步程序日志
$startSearchString = "receiving incremental file list" # 开始备份标志
$endSearchString = "total size is" # 备份成功标志
# $scriptPath = "D:\backup\backup-script" # bat批处理程序
# 清除旧日志(如果需要)
#if (Test-Path $logFile) {
# Remove-Item $logFile
#}
# 重启VPN程序
function restartVPN{
try {
# 获取VPN状态
statusVPN
# 判断重启VPN or 启动VPN
if($processStatus -ne $null){
# 停止VPN
Stop-Process -Name $processName -Force -ErrorAction Stop
Start-Sleep -Seconds 3
Add-Content -Path $scriptLogFile -Value "$(Get-Date -Format G): VPN程序已停止"
# 启动VPN
Start-Process -FilePath $exePath
Start-Sleep -Seconds 3
statusVPN
# 判断是否重启成功
if($processStatus -ne $null){
Add-Content -Path $scriptLogFile -Value "$(Get-Date -Format G): VPN程序已启动"
}else {
restartVPN
}
}else {
Start-Process -FilePath $exePath
Start-Sleep -Seconds 3
Add-Content -Path $scriptLogFile -Value "$(Get-Date -Format G): VPN程序已启动"
}
} catch {
Add-Content -Path $scriptLogFile -Value "$(Get-Date -Format G): 程序启动失败 ${exePath}: $_"
}
}
# 启动VPN程序
function statusVPN {
# VPN程序获取进程状态
$processStatus = Get-Process | Where-Object { $_.Name -like "*$processName*" }
}
# 启动备份
function startBackup {
Add-Content -Path $scriptLogFile -Value "$(Get-Date -Format G): 开始备份"
rsync -avz --delete --progress --password-file=/cygdrive/d/sync/bin/rsyncd.password root@10.6.10.24::test /cygdrive/d/backup/backup >$rsyncLogFile
# 开始备份结果
$searchStartResult = Select-String -Path $rsyncLogFile -Pattern $startSearchString -Quiet
# 结束备份结果
$searchEndResult = Select-String -Path $rsyncLogFile -Pattern $endSearchString -Quiet
# 判断是否备份成功
if ($searchStartResult) {
Add-Content -Path $scriptLogFile -Value "$(Get-Date -Format G): 正在备份,请等待..."
} else {
Add-Content -Path $scriptLogFile -Value "$(Get-Date -Format G): 未开始备份,重启备份程序...."
startBackup
}
# 循环判断是否备份成功
for ($i = 1; $i -le 5; $i++) {
if ($searchEndResult) {
Add-Content -Path $scriptLogFile -Value "$(Get-Date -Format G): 备份成功"
break;
}else{
Start-Sleep -Seconds 60
}
}
}
# 测试网络链接,并将结果存储到networkResult中
function TestNetConnection {
$networkResult = Test-NetConnection -ComputerName $ip -Port $port -InformationLevel Quiet
if ($networkResult) {
Add-Content -Path $scriptLogFile -Value "$(Get-Date -Format G): 网络链接成功"
} else {
Add-Content -Path $scriptLogFile -Value "$(Get-Date -Format G): 网络链接失败,重启VPN"
restartVPN
TestNetConnection
}
}
# 主程序
Add-Content -Path $scriptLogFile -Value "$(Get-Date -Format G): 脚本开始执行"
# 检查网络
TestNetConnection
# 开始备份
startBackup
# 脚本结束
Add-Content -Path $scriptLogFile -Value "$(Get-Date -Format G): 脚本执行完毕"
Add-Content -Path $scriptLogFile -Value "$(Get-Date -Format G): -----------------------------------------------------------------------------"
bat批处理程序脚本
文件名:start-backup-manual.bat
注意:若出现乱码,则需要另存为,并将编码设置为ANSI
bat
@echo off
echo 开始执行 %DATE% %TIME% >> rsync_log.txt
set "rsync_command=rsync -avz --delete --progress --password-file=/cygdrive/d/sync/bin/rsyncd.password root@192.168.10.204::test /cygdrive/d/backup/backup"
:: 执行rsync命令并将输出追加到带有时间戳的日志文件中
%rsync_command% >> rsync_log.txt 2>&1
echo %datetime% rsync执行完毕 >> rsync_log.txt
echo. >> rsync_log.txt
echo ------------------------------------------------------- >> rsync_log.txt