转自http://bbs.csdn.net/topics/320136534
在windows平台下面
/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*
@echo off
echo.
echo MySQL数据库备份
echo *****************************
echo.
echo 今天是 %date%
echo 时间是 %time%
echo.
echo *****************************
set "Ymd=%date:~,4%%date:~5,2%%date:~8,2%"
md "D:\JDBC\%Ymd%"
"C:\Program Files\MySQL\MySQL Server 5.1\bin\mysqldump" --opt -Q -uroot -pabc123 --default-character-set=latin1 test > "D:\JDBC\%Ymd%\test_bak.sql"
echo.
echo MySQL数据库备份完成,请进行检查。。。
echo.
echo.
在linux上面
#!/bin/bash
#################################
#
# Author coolwind
# Time 2009-06-11
# File mysqlbackup.sh
# Desc Backup MySQL data file
#
#################################
#Set the varible.
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
# Get the date messages and backup directory
backuptime=`date +%Y%m%d%H%M%S_%N`
backupdir=/mnt/backup/db/
backuppre=db_backup_
# MySQL infomation
HOST=127.0.0.1
PORT=3307
USER=root
#PASS=password
BACKUPDBFILE=$backupdir$backuppre$backuptime.sql
echo $BACKUPDBFILE
mysqldump -u$USER -h$HOST -P$PORT --all-databases --lock-all-tables --add-drop-database --add-drop-table --comments --complete-insert --default-character-set=utf8 --result-file=$BACKUPDBFILE
echo "$BACKUPDBFILE -> $BACKUPDBFILE.bz2"
bzip2 -z $BACKUPDBFILE
[coolwind@localhost util]$ crontab -l
#分 時 日 月 週 |<==============指令串========================>|
35 4 * * * /home/coolwind/util/mysqlbackup.sh >>/home/coolwind/util/logs/mysqlbackup.log 2>&1