數據庫備份的腳本,記錄下,還需優化下
阿新 • • 發佈:2017-08-25
還需 from use %d 需要 xiaomi bin sts cal
由於公司數據比較重要,隔一段時間就需要備份,就寫了個簡單的shell來維護下。。。
#!/bin/bash #backup database if [ $# -ge 1 ] ; then [email protected] else databaselist="dodo_account dodo_edu" fi for database in $databaselist do if [ ! -e /home/guimingming/backup_databases/$database ] then mkdir -p /home/guimingming/backup_databases/$database fi mysqldump --opt -hxx.xx.xx.x -u{username} -p{pwd} $database |gzip > /home/guimingming/backup_databases/$database/$database$(date +"%Y-%m-%d %H:%M:%S").sql.gz if [ $? -eq 0 ] then echo "$(date +"%Y-%m-%d %H:%M:%S") Database ---$database--- Backup Successfully Completed\r\n" find /home/guimingming/backup_databases/$database/ -name $database\* -ctime +1 -exec rm -rf {} \; else echo "$(date +"%Y-%m-%d %H:%M:%S") Database ---$database--- Backup unsuccessfully,please check out the reason\r\n" fi done #!/bin/bash # 假設將database_name數據庫名改為database_name_2_2016_06_28 # MyISAM直接更改數據庫目錄下的文件即可 mysql -uroot -e ‘create database if not exists database_name;‘ list_table=$(mysql -uroot -Nse "select table_name from information_schema.TABLES where TABLE_SCHEMA=‘database_name_2‘") for table in $list_table do mysql -uroot -e "rename table database_name_2.$table to database_name.$table" done #mysql -u root database_name_2<database_name_220160628-frm 0 3 * * 1 sh /usr/local/shell/backup_database.sh >/dev/null 2>&1 0 3 1 * * sh /usr/local/shell/backup_database.sh {param}>/dev/null 2>&1 原文:http://www.cnblogs.com/guixiaoming/p/7427076.html數據庫備份的腳本,記錄下,還需優化下