Sample sql script to zip and transfer database backup file

家住魔仙堡 提交于 2019-12-22 06:06:07

问题


I was looking for a sample sql script to zip my database backup file (.bak) and transfer to a remote location. Please share if you have it with you.


回答1:


You can use xp_cmdshell to invoke the commands for zipping and copying. In the sample here, I am using winzip command line (for zipping/unzipping) and xcopy for transferring files.

EXEC master..xp_cmdshell 'C:\"Program Files"\WinZip\wzzip C:\Database.bak.zip C:\Database.bak';
EXEC master..xp_cmdshell 'C:\"Program Files"\WinZip\wzunzip -o "C:\Database.bak.zip" "C:\Database"';
EXEC master..xp_cmdshell 'xcopy "C:\Database.bak.zip" "\\networkshare\Backups" /Y'



回答2:


xp_cmdshell is one way, although it isn't ideal since enabling it makes the server less secure.

My open source project, SQL Server Compressed Backup, does what you are looking for in one step:

msbp.exe backup "db(database=model)" "zip64" "local(path=\\server\share\path\model.full.bak.zip)"

The installation of SQL Server Compressed Backup is just "xcopy" deployment -- there is nothing to install/uninstall, ideal if you just need to do this once.

It uses zip64 since the standard zip format has a 4 GB limit. Alternative compression formats available are gzip and bzip2 which have no limits.




回答3:


Why not using such simple tools like SqlBackupAndFtp? They do exactly what you need (sql backup + moving to remote location) with a simple interface and you don't need to write any scripts.



来源:https://stackoverflow.com/questions/128166/sample-sql-script-to-zip-and-transfer-database-backup-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!