ms sqlserver备份脚本,自动FTP上传。

@ECHO off

REM Backup Database Script Log Files
SET dbbak_dir=e:backupdir\
if not exist %dbbak_dir% mkdir %dbbak_dir%

REM Backup Database Name
SET B_Database_Name=model
SET B_DATE=%date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%
ECHO use master ;>%dbbak_dir%FULL_backup.sql
ECHO go >>%dbbak_dir%FULL_backup.sql
rem ECHO SP_ADDUMPDEVICE 'DISK',>>%dbbak_dir%FULL_backup.sql
rem ECHO '%B_Database_Name%_%B_DATE%_FULL', >>%dbbak_dir%FULL_backup.sql
rem ECHO '%dbbak_dir%%B_Database_Name%_%B_DATE%_FULL.bak'; >>%dbbak_dir%FULL_backup.sql
rem ECHO go >>%dbbak_dir%FULL_backup.sql
ECHO BACKUP DATABASE %B_Database_Name% >>%dbbak_dir%FULL_backup.sql
ECHO TO DISK = '%dbbak_dir%%B_Database_Name%_%B_DATE%_FULL.bak' >>%dbbak_dir%FULL_backup.sql
ECHO WITH INIT , NOUNLOAD ;>>%dbbak_dir%FULL_backup.sql
ECHO go >>%dbbak_dir%FULL_backup.sql
ECHO .

REM osql.exe path
SET OSQL_PATH=C:\Program Files\Microsoft SQL Server\90\Tools\Binn\

REM Database user name and password
SET DBUser=sa
SET DBpassword=1q2w3e4R
ECHO BACKUP DATABASE
"%OSQL_PATH%sqlcmd.exe" -i %dbbak_dir%FULL_backup.sql -o %dbbak_dir%%B_Database_Name%_%B_DATE%

_FULL.log
if not %errorlevel%==0 exit 1

REM FTP info
set FTP_User=ftp
set FTP_PW=ftp
set FTP_IP=127.0.0.1
set FTP_PORT=21
set FTP_DIR=E:\dir

ECHO %FTP_User%>%dbbak_dir%FTP_CMD.txt
ECHO %FTP_PW%>>%dbbak_dir%FTP_CMD.txt
ECHO bin>>%dbbak_dir%FTP_CMD.txt
ECHO cd %FTP_dir%>>%dbbak_dir%\FTP_CMD.txt
ECHO mput %dbbak_dir%%B_Database_Name%_%B_DATE%_FULL.bak>>%dbbak_dir%FTP_CMD.txt
ECHO BYE>>%dbbak_dir%\FTP_CMD.txt
ftp -i -s:%dbbak_dir%\FTP_CMD.txt %FTP_IP%
if not %errorlevel%==0 exit 1

REM Delete old backup files
FORFILES /P %dbbak_dir% /D -7 -M %dbbak_dir%*.bak /C "cmd /c del @file"
FORFILES /P %dbbak_dir% /D -7 -M %dbbak_dir%*.log /C "cmd /c del @file"

REM Delete Temp files
DEL /Q %dbbak_dir%FULL_backup.sql
DEL /Q %dbbak_dir%FTP_CMD.txt