定时同步和定时备份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 } }