简体   繁体   中英

Windows Server - directory and database backup

We had a issue at work last week which would be sorted if we had a scheduled task running.

Basically want I want to know is, I need to create a batch file which performs the following tasks on a Windows server box.

  • Backup the website (basically a folder and all its sub-directories) at 8:30am every morning to a specific folder in the format websitename_year_month_day

  • Backup the website (basically a folder and all its sub-directories)
    at 5:30 every night to a specific folder in the format websitename_year_month_day

  • Backup the corresponding DBs at the same times to the same folder location the website backup is in.

So, for example if the website is called swade1987

The folder would have this file structure shown in the attachment.

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

However, ideally I would like it so it only saves backups for the past 7 days.

Looking forward to your replies on how best to do this.

If you need more clarification let me know

Steven

here are the pieces you need:

  1. get 7za.exe - a free, standalone windows compression utility (google for 7z command line and you'll find it).

  2. use microsoft's sqlcmd.exe to backup your database from a batch file (command line sql executor included with mssql)

here is how to name your files: because you only want to keep the last 7 days, do not put the date in the name, put the abbreviation for the week date in the filename. eg website_mon.zip

here is a sample batch file that does this. USE AT YOUR OWN RISK. it has not been tested. i just came up with it off the top of my head. follow the code and comments and modify as necessary. i would be shocked if there wasn't a typo or three. also watch the dos commands that you add/modify ... if they include long file names, you might need to add some surrounding quotes.

@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%

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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