使用特定名称(或最新的名称)通过网络复制文件

时间:2015-03-19 09:29:16

标签: powershell command-line command command-prompt powershell-v3.0

我必须每天通过网络复制我的SQL Server备份文件。为此,我正在考虑编写批处理文件或PowerShell脚本并将其作为计划任务运行 此过程中的问题是区分特定数据库的最新文件,因为同一数据库的三天备份保留在源目录中。命名约定是:databaseName_backup_yyyy_mm_dd_hhmmss.bak
例如,数据库myDatabase的源目录下会有这三个文件:
myDatabase_backup_2015_03_19_230000.bak
myDatabase_backup_2015_03_20_230000.bak
myDatabase_backup_2015_03_21_230000.bak

该文件可能是一个特定数据库的最新创建文件,也可能是其名称包含当前日期的文件。

谢谢大家。

编辑:我可以在目标服务器上实现它,而不是在源上实现。

1 个答案:

答案 0 :(得分:1)

我这样做,没有太多聪明的Powershell逻辑,借用传统的CMD,因为SQL正在处理你的保留(从它的外观看3个完整备份)我做了以下几点。通过存储这些SQL备份的服务器上的任务调度程序运行(因此它们是PULL而不是从SQL发送)

$remoteserver = "servername"
$remoteshare = "backups$"

$toEmail = "something@something.com"
$subject = "$remoteserver Robocopy Log"
$fromEmail = "anemail@domain.com"
$smtpServer = "exchange.local"
$Attachment = "C:\scripts\$remoteserver-copy.log"


robocopy "$remoteshare\$remoteserver\" G:\LocalDestination\"$remoteserver"\ /s | Out-File $Attachment
Send-MailMessage $toEmail $subject -SmtpServer $smtpServer -From $fromEmail -Attachment $Attachment

`