[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
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
echo "[$newfile]backup success" >>$logfile
echo "--------------------------------------">>$logfile
exit 0;
========================================================================
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