随笔 - 911  文章 - 5  评论 - 94  阅读 - 243万

定时同步和定时备份xcopy

 

每天定时同步数据

复制代码
#脚本功能:
#该脚本用来从远程服务器同步文件,会对文件修改时间进行比较,如果远程文件修改时间不小于当前服务器文件修改时间,则进行拷贝
#远程源文件夹不要包含有子目录

$remote_ip = "10.10.22.34"
$username = "test\administrator"
$password = "Yipassword"
$remote_dir = "D:\Scripts"
$local_dir = "C:\mac"
net use \\$remote_ip $password /user:$username

$remote_dir = $remote_dir.Replace(':','$')
$remote_files = gci \\$remote_ip\$remote_dir
foreach ($file in $remote_files)
    {
    $local_file = Join-Path $local_dir $file
    if (Test-Path $local_file)
        {
        if ( $file.LastWriteTime -gt (gci $local_file).LastWriteTime )
            { xcopy.exe $file.FullName $local_dir /h /r /s /y }
        }
        else { xcopy.exe $file.FullName $local_dir /h /r /s /y }
    }

net use * /del /y
复制代码

 

 

每天定时备份目录,将该目录下所有文件拷贝到备份目录(以当前时间命名),并删除早于14天的备份

复制代码
$local_dir = "C:\mac"
$backup_dir = "C:\Backup"

$today = Get-Date -UFormat "%Y-%m-%d_%H-%M"
$backup_dir_today = join-path $backup_dir $today
if (!(Test-Path $backup_dir_today))
    { mkdir $backup_dir_today }
xcopy $local_dir\* $backup_dir_today /h /r /s /y

$dirs = gci $backup_dir | ? {$_.Mode -eq "d-----"}
foreach ($dir in $dirs)
    {
    if ( $dir.CreationTime.AddDays(14) -lt (Get-Date) )  #AddDays
        {
        Remove-Item -Path $dir.FullName -Recurse -Force
        }
    
    }
复制代码

 

posted on   momingliu11  阅读(343)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2015-06-29 批处理当前脚本所在路径
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示