MySQL定时备份并压缩数据命令

发布于 2014-01-07  19.3k 次阅读


1、windows平台 ,保存bat执行,设置【任务计划】定时备份

set d=%date:~0,10%
set d=%d:/=%
set d=%d:-=%
set t=%time:~0,8%
set t=%t::=%
set t=%t: =%
set dzxpath=192-168-1-250-mysql-csch-%d%-%t%.sql
set ucpath=192-168-0-1-uc-csch-%d%-%t%.sql
set folder=D:BACKUPDB
echo off
"C:Progra~1MySQLMySQL Server 5.5binmysqldump.exe" -h localhost --opt -u root --password=123456789 csch> "%folder%%dzxpath%"
"C:Progra~2WinRARRar.exe" a -ep1 -r -o+ -m5 -s -df "%folder%%dzxpath%".rar "%folder%%dzxpath%"
pause
       
%其它:%
%还原命令%
%C:Program FilesMySQLMySQL Server 5.5bin>mysql -h localhost -u root -p test < C:UsersAdministratorDesktop192-168-0-1-mysql-test-20121026-91356.sql%
%Enter password: ******%

2、linux平台,保存sh执行,内置【cron】命令定时备份

mysqldump-u root -p 123456789 csch | gzip > /home/backup/db/`csch_date +%Y-%m-%d_%H%M%S`.sql.gz
   
   
#--------------------------------gzip压缩-----------------------
#1、备份
#mysqldump-u root -p 123456789 csch | gzip > csch_date +`%Y-%m-%d_%H%M%S`.sql.gz
#2、修改文件属性,使其可以执行
#chmod +x /home/backupDB/csch
#3、修改/etc/crontab,在下面添加
#0 3 * * * root /usr/sbin/bakmysql
#4、重新启动crond
#/etc/rc.d/init.d/crond restart
   
#系统崩溃,重建系统时,可以这样恢复数据:
#mysql -u root -p 123456789 csch < csch_date2010-04-01.sql
#从压缩文件直接恢复:
#gzip < csch_date2010-04-01.sql.gz | mysql -u root -p 123456789 csch