rsync 客户端实现自动备份 Windows端powershell脚本及bat批处理文件程序

文章目录

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
相关推荐
宝桥南山5 小时前
Visual Studio - 记录一个Visual Studio 2026(Insiders)的更新问题
windows·microsoft·微软·.net·.netcore·visual studio
JosieBook10 小时前
【鸿蒙开源】把 Windows 任务栏上的流量表搬到鸿蒙 PC:TrafficMonitor 1.86 的 C++ 内核移植实践
c++·windows·harmonyos
Vex2une20 小时前
把 Windows 装进 U 盘随身携带:Windows To Go 现状与 WinToUSB 制作完全指南
windows·vhdx·windows to go·wintousb·便携windows·系统带进u盘·随身系统
无奈笑天下2 天前
Wine适配Windows程序常见故障标准处理流程
linux·运维·服务器·windows
广东程序猿小展2 天前
PotPlayer 播放 4K 视频卡顿怎么办(硬解与渲染设置)
windows·工具·软件
xx z3 天前
Radmin LAN 怎么联机(虚拟局域网建房与加入教程)
windows·工具·软件
广东程序猿小展3 天前
谷歌相机配置文件怎么导入(GCam 配置导入与不生效排查)
windows·工具·软件
李小白杂货铺4 天前
Windows 桌面 GUI 原型快速开发技术选型指南(Mac 开发 + Windows 交付场景)
windows·原型·gui·原型开发·技术选型·快速开发·技术路线
承渊政道4 天前
magic-resume简历工具教程:Windows部署、在线编辑与远程访问
windows·远程访问·在线编辑·简历制作·工具教程·magic-resume