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
相关推荐
liulilittle5 小时前
.NET ExpandoObject 技术原理解析
开发语言·网络·windows·c#·.net·net·动态编程
cpsvps11 小时前
VR协作海外云:跨国企业沉浸式办公解决方案
windows·架构·vr·etw事件
义薄云天us13 小时前
019_工具集成与外部API调用
数据库·人工智能·windows·microsoft·claude code
RainbowSea13 小时前
安装win10出现“计算机意外的重新启动或遇到错误。Windows安装无法继续。若要安装Windows,请单击“确定”重新启动计算机,然后安装系统。”
windows·后端
bug攻城狮13 小时前
理解Grafana中`X-Scope-OrgID`的作用与配置
windows·grafana
扛枪的书生15 小时前
AD 提权-NTLM 中继攻击(强制认证)
windows·渗透·kali·提权·域渗透
hqxstudying16 小时前
Java行为型模式---观察者模式
java·开发语言·windows·观察者模式
Damon小智17 小时前
玩转ClaudeCode:ClaudeCode安装教程(Windows+Linux+MacOS)
windows·ai·mac·wsl·claude code·vibe code
CHANG_THE_WORLD19 小时前
libimagequant windows 编译
windows·libimagequant
xchenhao1 天前
基于 Flutter 的开源文本 TTS 朗读器(支持 Windows/macOS/Android)
android·windows·flutter·macos·openai·tts·朗读器