#!/bin/bash #-----------------------------------------------# #This is a free GNU GPL version 3.0 or abover #Copyright (C) 2008 06 05 #mysql_backup Dedicated copyright by My #-----------------------------------------------# echo -e [`date +"%Y-%m-%d %H:%M:%S"`] start #system time time=`date +"%y-%m-%d"` #host IP host="127.0.0.1" #database backup user user="root" #database password passwd="yourpasswd" #Create a backup directory mkdir -p /backup/db/"$time" #list database name all_database=`/usr/bin/mysql -u$user -p$passwd -Bse 'show databases'` #in the table from the database backup for i in $all_database do /usr/bin/mysqldump -u$user -p$passwd $i > /backup/db/"$time"/"$i"_"$time".sql done echo -e [`date +"%Y-%m-%d %H:%M:%S"`] end exit 0
腳本中的數據庫名和數據庫密碼以用戶須要備份的數據庫信息爲準,須要用戶修改下。mysql
運行 crontab -e,寫入如下內容:sql
30 5 * * * root sh /root/autobackup.sh >/dev/null 2>&1
保存退出,以後天天早上 5:30 就會自動備份數據庫了。數據庫
注意:備份會佔用磁盤空間,及時清理不須要的數據或者擴展磁盤空間。 bash