6

I'm trying to use PowerShell to create some backups, and then to copy these to a web folder (or, in other words, upload them to a WebDAV share).

At first I thought I'd do the WebDAV stuff from within PowerShell, but it seems this still requires a fair amount of "manual labour", ie: constructing HTTP requests. I then settled for creating a web folder from the script and letting Windows handle the WebDAV stuff. It seems that all it takes to create a web folder is to create a standard shortcut, as described here.

What I can't figure out is how to actually copy files to the shortcut's target..? Maybe I'm going about this the wrong way.

It would be ideal if I could somehow encrypt the credentials for the WebDAV in the script, then have it create the web folder, shunt over the files, and delete the web folder again. Or even better, not use a web folder at all. Third option would be to just create the web folder manually and leave it there, though I'd rather not.

Any ideas/pointers/tips? :)

4

2 回答 2

7

如果您使用 powershell 使用 svnadmin dump 备份您的 SVN 存储库,那么请注意,通过管道传输到文件会默默地破坏您的备份。

Powershell 喜欢在管道时将内容更改为 UTF-16,它还将 unix 换行符更改为 windows 换行符。当您尝试恢复时,这将再次困扰您。

问题在这里很好地描述:

http://thoughtfulcode.wordpress.com/2010/01/29/powershells-object-pipeline-corrupts-piped-binary-data/

这里的解决方案:

http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.windows.powershell&tid=e4cd89e9-427b-407d-a94f-c24be3f1e36f&cat=&lang=&cr=&sloc=&p= 1

总之,使用 cmd.exe 代替 powershell:

cmd.exe /c svnadmin dump ... `> dumpfile.dump

请注意,输出重定向上的反引号是停止 powershell 解析它所必需的。

于 2010-03-23T20:57:43.603 回答
5

好吧,与此同时,我拼凑了另一个解决方案。也许它会对某人有用..

[Run.cmd] --可能需要修改powershell路径

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -nologo -noninteractive -command "C:\Scripts\RunBackup.ps1"

[RunBackup.ps1] --Out-File 没有达到预期的效果,也许有人可以弄清楚为什么?

C:\Scripts\SqlBackup.ps1 | Out-File "C:\Scripts\log.txt"
C:\Scripts\SVNBackup.ps1 | Out-File "C:\Scripts\log.txt"
C:\Scripts\Zip.ps1 | Out-File "C:\Scripts\log.txt"

[SqlBackup.ps1] -- 您可能需要修改加载哪些 SMO 程序集,具体取决于您的 SQL Server 版本。不要忘记设置 $instance 和 $bkdir。

#http://www.mssqltips.com/tip.asp?tip=1862&home

$instance = ".\SQLEXPRESS"


[System.Reflection.Assembly]::LoadFrom("C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies\Microsoft.SqlServer.SMO.dll") | out-null

[System.Reflection.Assembly]::LoadFrom("C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies\Microsoft.SqlServer.SMOExtended.dll") | out-null

$s = new-object ("Microsoft.SqlServer.Management.Smo.Server") $instance

$bkdir = "c:\Backups" #We define the folder path as a variable
$dbs = $s.Databases
foreach ($db in $dbs)
{
     if($db.Name -ne "tempdb") #We don't want to backup the tempdb database
     {
     $dbname = $db.Name
     $dt = get-date -format yyyyMMddHHmm #We use this to create a file name based on the timestamp
     $dbBackup = new-object ("Microsoft.SqlServer.Management.Smo.Backup")
     $dbBackup.Action = "Database"
     $dbBackup.Database = $dbname
     $dbBackup.Devices.AddDevice($bkdir + "\" + $dbname + "_db_" + $dt + ".bak", "File")
     $dbBackup.SqlBackup($s)
     }

     if($db.RecoveryModel -ne 3) #Don't issue Log backups for DBs with RecoveryModel=3 or SIMPLE
     {
     $dbname = $db.Name
     $dt = get-date -format yyyyMMddHHmm #Create a file name based on the timestamp
     $dbBackup = new-object ("Microsoft.SqlServer.Management.Smo.Backup")
     $dbBackup.Action = "Log"
     $dbBackup.Database = $dbname
     $dbBackup.Devices.AddDevice($bkdir + "\" + $dbname + "_log_" + $dt + ".trn", "File")
     $dbBackup.SqlBackup($s)
     } 
}

[SVNBackup.ps1] --修改repo和备份路径

#set alias to svnadmin exe
set-alias svnadmin "C:\Program Files (x86)\CollabNet Subversion Server\svnadmin.exe"

#create dump
cmd.exe /c svnadmin dump "C:\Repo" `> "C:\Backups\svn.dmp"

#remove alias
remove-item alias:svnadmin

[Zip.ps1] --需要安装7zip,必要时修改7z.exe路径

#set alias to command line version of 7zip
set-alias sevenz "c:\program files\7-zip\7z.exe"

#Backups location
cd 'C:\Backups'

#rar the contents of the directory
$dt = get-date -format yyyyMMddHHmm #We use this to create a file name based on the timestamp
$outputFileName = "SQLSVNBackup$dt.7z"
$exclude1 = "-x!*.rar"
$exclude2 = "-x!*.7z"
sevenz a -t7z "$outputFileName" *.* "$exclude1" "$exclude2"

#find all .bak files in the immediate directory 
dir '*.bak' | foreach-object{

#remove the bak file
remove-item $_.name

}


#find all .dmp files in the immediate directory 
dir '*.dmp' | foreach-object{

#remove the dmp file
remove-item $_.name

}

#find all .trn files in the immediate directory 
dir '*.trn' | foreach-object{

#remove the trn file
remove-item $_.name

}

#remove 7zip alias
remove-item alias:sevenz

我使用GoodSync备份到 WebDAV 并安排了两个任务来运行 .cmd 文件,然后在异地同步/备份。

于 2009-11-20T23:22:14.713 回答