繁体   English   中英

Windows Server-目录和数据库备份

[英]Windows Server - directory and database backup

上周我们在工作中遇到一个问题,如果我们有计划的任务运行,则可以解决。

基本上我想知道的是,我需要创建一个批处理文件,该文件在Windows服务器上执行以下任务。

  • 每天早上8:30将网站(基本上是文件夹及其所有子目录)备份到格式为websitename_year_month_day的特定文件夹中

  • 备份网站(基本上是一个文件夹及其所有子目录)
    每晚5:30在格式为websitename_year_month_day的特定文件夹中

  • 同时将相应的数据库备份到网站备份所在的文件夹位置。

因此,例如,如果该网站名为swade1987

该文件夹将具有附件中显示的此文件结构。

http://imageshack.us/photo/my-images/254/screenclip1.png/

但是,理想情况下,我希望这样做,因此它仅保存过去7天的备份。

期待您对如何做到最好的答复。

如果您需要更多说明,请告诉我

史蒂文

这是您需要的零件:

  1. 获取7za.exe-一个免费的独立Windows压缩实用程序(适用于Google的7z命令行,您将找到它)。

  2. 使用Microsoft的sqlcmd.exe从批处理文件备份数据库(mssql附带的命令行sql executor)

这是为文件命名的方法:因为您只想保留最近7天,所以不要在名称中加上日期,而在文件名中加上星期日期的缩写。 例如website_mon.zip

这是执行此操作的示例批处理文件。 自行承担风险使用。 尚未经过测试。 我只是想出了办法。 遵循代码和注释,并根据需要进行修改。 如果没有错字或三字,我会感到震惊。 还请注意您添加/修改的dos命令...如果它们包含长文件名,则可能需要添加一些引号。

@echo off
set SHORTDATE=%DATE:~0,3%

set DB_ZIP_FILE=c:\backups\sitedb_%SHORTDATE%.zip
set WWW_ZIP_FILE=c:\backups\siterot_%SHORTDATE%.zip
set WEBSITE_DIR=c:\mysite\wwwroot

set SQL_FILE=c:\temp\backup.sql

rem * get rid of our old backups
rem * this is for our protection ... if there
rem * was a problem with a zip (corruption) 
rem * our backups will fail if we don't get rid
rem * of the old zips.
rem *
if exist %DB_ZIP_FILE% del %DB_ZIP_FILE%
if exist %DB_ZIP_FILE% del %WWW_ZIP_FILE%

rem * create a temp file containing the sql script
echo BACKUP DATABASE YOUR_DB_NAME> %SQL_FILE%
echo TO DISK='c:\temp\sitedb-%SHORTDATE%.bak'>>%SQL_FILE%
echo WITH FORMAT, Name='Daily sitedb %SHORTDATE%' >> %SQL_FILE%

echo %DATE% %TIME% backing up SITEDB
call sqlcmd -U YOURUSER -P YOURPASSWORD -i %SQL_FILE% > NUL

rem ** zip the files - we remove the old one before starting
call 7za a -tzip %ZIP_FILE% c:\temp\sitedb-%SHORTDATE%.bak

rem ** delete the bak file
del c:\temp\sitedb-%SHORTDATE%.bak

rem ** now zip up our web site directory
7za a -tzip -r %WEBSITE_DIR% %WWW_ZIP_FILE%

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM