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
相关推荐
搏博7 小时前
基于Python3.10.6与jieba库的中文分词模型接口在Windows Server 2022上的实现与部署教程
windows·python·自然语言处理·flask·中文分词
有梦想的攻城狮15 小时前
Java 11中的Collections类详解
java·windows·python·java11·collections
忒可君15 小时前
C# winform FTP功能
开发语言·windows·c#
十五年专注C++开发16 小时前
CMake进阶: CMake Modules---简化CMake配置的利器
linux·c++·windows·cmake·自动化构建
degree52016 小时前
全平台轻量浏览器推荐|支持Win/macOS/Linux,极速加载+隐私保护+扩展插件,告别广告与数据追踪!
windows·macos·电脑
许泽宇的技术分享2 天前
Windows桌面自动化的革命性突破:深度解析Windows-MCP.Net Desktop模块的技术奥秘
windows·自动化·.net
七仔的博客2 天前
【摸鱼办公神器】七仔的桌面工具超进化 -> 灵卡面板 v1.1.9
windows·神器·摸鱼
码农阿豪2 天前
Windows从零到一安装KingbaseES数据库及使用ksql工具连接全指南
数据库·windows
CC__xy2 天前
demo 通讯录 + 城市选择器 (字母索引左右联动 ListItemGroup+AlphabetIndexer)笔记
windows
LZQqqqqo3 天前
C# 中 ArrayList动态数组、List<T>列表与 Dictionary<T Key, T Value>字典的深度对比
windows·c#·list