使用备份批处理脚本中的当前日期作为文件名的参数



我正在尝试用一项计划任务备份我的MySQL数据库。备份SQL的命令是:

mysqldump.exe -h localhost -u root databasename > databasebackup.sql

我想做的是将当前日期添加到文件名中,使其成为databasebackup_2012-01-31.sql。更理想的情况是,我会将文件databasebackup_2012-01-31.sql压缩为zip文件databasebackup_2012-01-31.zip,因为sql文件大多包含可压缩的文本。压缩后,sql文件将被删除,只有zip文件保留备份。

CMD.exe中有一个提供日期的伪变量。此外,您还可以从http://dotNetZip.codeplex.com。

一个用来做你想做的事情的批处理文件看起来像这样:

@echo off
@setlocal
echo The date is %DATE%
@set tag=%DATE:~-4%-%DATE:~7,2%-%DATE:~4,2%
set backupfile=databasebackup.%tag%.sql
echo backing up to:  %backupfile%
echo.
echo ^<do the backup here^>
echo.
echo hello hello hello hello > %backupfile%

@REM The DotNetZip download for ZIP Tools includes a command-line zip utility.
@REM Get it from http://dotnetzip.codeplex.com.
set zipit=Program Files (x86)Dino ChiesaDotNetZip Tools 1.9ZipIt.exe
set unzip=Program Files (x86)Dino ChiesaDotNetZip Tools 1.9UnZip.exe
set zipfile=%backupfile%.zip
if exist %zipfile% (
echo deleting existing zip...
del %zipfile%
)
"%zipit%"  %zipfile%  -s Readme.txt  "Backed up on %DATE% at %TIME%"  %backupfile%
echo.
@REM list the contents of the created zip
"%unzip%" -l %zipfile%
echo.
echo ^<delete the sql file here^>
echo.
del %backupfile%
@endlocal

相关内容

  • 没有找到相关文章

最新更新