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
相关推荐
路由侠内网穿透2 天前
本地部署 GPS 跟踪系统 Traccar 并实现外部访问
运维·服务器·网络·windows·tcp/ip
研华嵌入式2 天前
如何在高通跃龙QCS6490 Arm架构上使用Windows 11 IoT企业版?
arm开发·windows·嵌入式硬件
带娃的IT创业者2 天前
Windows 平台上基于 MCP 构建“文心一言+彩云天气”服务实战
人工智能·windows·文心一言·mcp
csdn_aspnet2 天前
Windows Node.js 安装及环境配置详细教程
windows·node.js
摇滚侠2 天前
java语言中,list<String>转成字符串,逗号分割;List<Integer>转字符串,逗号分割
java·windows·list
Source.Liu2 天前
【Pywinauto库】12.2 pywinauto.element_info 后端内部实施模块
windows·python·自动化
Source.Liu2 天前
【Pywinauto库】12.1 pywinauto.backend 后端内部实施模块
开发语言·windows·python·自动化
私人珍藏库3 天前
[Windows] FileOptimizer v17.1.0_一款文件批量压缩工具
windows·批量压缩
掘根3 天前
【CMake】List
windows·microsoft·list
TToolss3 天前
删除文件夹里的网盘图标
windows