1. 程式人生 > >bat批處理實現資料庫備份、壓縮、刪除

bat批處理實現資料庫備份、壓縮、刪除

在專案運營的過程中,為保證資料庫能及時備份,我們一般啟動系統的定時任務來進行備份。以下bat指令碼實現刪除1天前的壓縮備份檔案,同時生成新的備份壓縮檔案

@echo off
rem 刪除1天前的資料庫備份
forfiles /m *.rar /d -1 /c "cmd /c del @file /f"

rem 生成備份資料庫
cd /d C:\Program Files\MySQL\MySQL Server 5.6\bin
SET filePath=D:\databackup\db\%date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%
SET sqlPath=%filePath%.sql
SET
rarPath=%filePath%.rar mysqldump.exe dbname -P 3306 -uroot -proot > %sqlPath% rem 壓縮備份的資料庫檔案 "D:\Program Files (x86)\WinRAR\WinRAR.exe" a -ibck -m5 %rarPath% %sqlPath% rem 刪除資料加檔案 del %sqlPath%