[root@mail ~]# crontab -e
crontab: no changes made to crontab
[root@mail ~]# crontab -l
55 05 * * * /bin/sh /root/mysql.sh

 
 

chmod +x  mysql.sh

 
全备备份
mysql.sh内容如下:
#!/bin/sh
time=$(date +%Y-%m-%d-%H:%M:%S)
backup_dir="/var/backup/mysql"
backup_old_dir="/var/backup/mysqlold"
logfile=/var/log/mysqlbak.log
newfile="$backup_dir"all-databases$time.sql.gz
mysqldump -pmysql  --all-databases |gzip > $backup_dir/all-databases$time.sql.gz
echo "[$newfile]backup success" >>$logfile
echo "--------------------------------------">>$logfile
find $backup_dir -name "*.sql.gz" -mtime +5 -exec mv {} $backup_old_dir \;
exit 0;
 
 
 
 
 
 
 
以下是分开处理的脚本
 
#!/bin/sh
time=$(date +%Y-%m-%d-%H:%M:%S)
backup_dir="/var/backup/mysql"
backup_old_dir="/var/backup/mysqlold" 
 
 
db="rt3"
db2="mysql"
db3="vpopmail"
#dbx="xxxx"
 
 
logfile=/var/log/mysqlbak.log
 
 
newfile="$backup_dir"$db$time.sql.gz
newfile2="$backup_dir"$db2$time.sql.gz
newfile3="$backup_dir"$db3$time.sql.gz
#newfilex="$backup_dir"$dbx$time.sql.gz
 
 
mysqldump -pmysql  rt3 |gzip > $backup_dir/rt3$time.sql.gz
echo "[$newfile]backup success" >>$logfile
 
 
mysqldump -pmysql  mysql |gzip > $backup_dir/mysql$time.sql.gz
echo "[$newfile2]backup success" >>$logfile
 
 
mysqldump -pmysql  vpopmail |gzip > $backup_dir/vpopmail$time.sql.gz
echo "[$newfile3]backup success" >>$logfile
 
 
#mysqldump -pmysql  xxxx |gzip > $backup_dir/xxxx$time.sql.gz
#echo "[$newfile3]backup success" >>$logfile
 
 
echo "--------------------------------------">>$logfile
 
 
find $backup_dir -name "*.sql.gz" -mtime +2 -exec mv {} $backup_old_dir \;
 
 
find $backup_old_dir -name "*.sql.gz" -mtime +5 |xargs rm -rf;
 
 
exit 0;
 
 
a.txt 内容为mysql vpopmail
#!/bin/sh
while read name ; do
time=$(date +%Y-%m-%d-%H:%M:%S)
backup_="/var/backup/"
mysqldump -pmysql  $name > $backup_$name/$name$time.sql
done< a.txt
 
 
 
 
 
#!/bin/sh
while read name ; do
time=$(date +%Y-%m-%d-%H:%M:%S)
backup="/var/backup/"
test ! -d "$backup$name" && mkdir -p "$backup$name"  #检测备份目录是否存在,不存在则建立目录
mysqldump -padmin  $name --skip-lock-tables > $backup$name/$name$time.sql #不同目录下备份数据库
find $backup$name -name "*.sql" -mtime +5 |xargs rm -rf; #删除旧的备份数据
done< mysql_bak_list.txt

 

 

========================================================================

1.备份数据库
#!/bin/sh
while read name ; do
time=$(date +%Y-%m-%d-%H:%M:%S)
backup="/var/backup/"
test ! -d "$backup$name" && mkdir -p "$backup$name"  #检测备份目录是否存在,不存在则建立目录
mysqldump -padmin  $name --skip-lock-tables > $backup$name/$name$time.sql #不同目录下备份数据库
#find $backup$name -name "*.sql" -mtime +5 |xargs rm -rf; #删除旧的备份数据
done< mysql_bak_list.txt
-----------------------------------------------------------------------------------
其中mysql_bak_list.txt 内容为(根据实际情况写)
information_schema
emailmgm          
mysql             
osticket          
rt                
rt3               
sugarcrm          
test              
vpopmail
2.初始化数据库
[root@mail mysql]# mysql_install_db --user=mysql

 

3.导入数据库

[root@mail mysql]# mysql -padmin mysql </var/backup/mysql/mysql2010-03-31-01\:23\:22.sql