TShopping

 找回密碼
 註冊
搜索
查看: 2707|回復: 6

[教學] mysql_backup.sh MySQL資料庫自動備份程式

[複製鏈接]
發表於 2009-7-18 00:58:00 | 顯示全部樓層 |閱讀模式
 
Push to Facebook Push to Plurk Push to Twitter 
這四個地方記得改一下
db_user="root"
db_passwd=""
db_host="localhost"
backup_dir=""
------------------------------------------------------------

  1. #!/bin/sh
  2. # mysql_backup.sh: backup mysql databases and keep newest 5 days backup.
  3. #
  4. # Last updated: 20 March 2006
  5. # ----------------------------------------------------------------------
  6. # This is a free shell script under GNU GPL version 2.0 or above
  7. # Copyright (C) 2006 Sam Tang
  8. # Feedback/comment/suggestions : http://www.real-blog.com/
  9. # ----------------------------------------------------------------------

  10. # your mysql login information
  11. # db_user is mysql username
  12. # db_passwd is mysql password
  13. # db_host is mysql host
  14. # -----------------------------
  15. db_user="root"
  16. db_passwd=""
  17. db_host="localhost"

  18. # the directory for story your backup file.
  19. backup_dir=""

  20. # date format for backup file (dd-mm-yyyy)
  21. time="$(date +"%d-%m-%Y")"

  22. # mysql, mysqldump and some other bin's path
  23. MYSQL="$(which mysql)"
  24. MYSQLDUMP="$(which mysqldump)"
  25. MKDIR="$(which mkdir)"
  26. RM="$(which rm)"
  27. MV="$(which mv)"
  28. GZIP="$(which gzip)"

  29. # check the directory for store backup is writeable
  30. test ! -w $backup_dir && echo "Error: $backup_dir is un-writeable." && exit 0

  31. # the directory for story the newest backup
  32. test ! -d "$backup_dir/backup.0/" && $MKDIR "$backup_dir/backup.0/"

  33. # get all databases
  34. all_db="$($MYSQL -u $db_user -h $db_host -p$db_passwd -Bse 'show databases')"

  35. for db in $all_db
  36. do
  37. $MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db | $GZIP -9 > "$backup_dir/backup.0/$time.$db.gz"
  38. done

  39. # delete the oldest backup
  40. test -d "$backup_dir/backup.5/" && $RM -rf "$backup_dir/backup.5"

  41. # rotate backup directory
  42. for int in 4 3 2 1 0
  43. do
  44. if(test -d "$backup_dir"/backup."$int")
  45. then
  46. next_int=`expr $int + 1`
  47. $MV "$backup_dir"/backup."$int" "$backup_dir"/backup."$next_int"
  48. fi
  49. done

  50. exit 0;
複製代碼

 

臉書網友討論
 樓主| 發表於 2009-8-5 18:15:50 | 顯示全部樓層
或是按照日期來備份檔案
  1. #!/bin/sh
  2. # mysql_backup.sh: backup mysql databases at day
  3. updated: 5 Aug 2009
  4. # ----------------------------------------------------------------------
  5. # This is a free shell script under GNU GPL version 2.0 or above
  6. # Copyright (C) 2009 Woff Lin
  7. # Feedback/comment/suggestions : http://www.tshopping.com.tw
  8. --------------------------------------------------------

  9. # your mysql login information
  10. # db_user is mysql username
  11. # db_passwd is mysql password
  12. # db_host is mysql host
  13. # -----------------------------
  14. db_user="root"
  15. db_passwd=""
  16. db_host="localhost"

  17. # the directory for story your backup file.
  18. backup_dir=""

  19. # mysql, mysqldump and some other bin's path
  20. MYSQL="$(which mysql)"
  21. MYSQLDUMP="$(which mysqldump)"
  22. MKDIR="$(which mkdir)"
  23. RM="$(which rm)"
  24. MV="$(which mv)"
  25. GZIP="$(which gzip)"

  26. # check the directory for store backup is writeable
  27. test ! -w $backup_dir && echo "Error: $backup_dir is un-writeable." && exit 0

  28. # the directory for story the newest backup
  29. test ! -d "$backup_dir/" && $MKDIR "$backup_dir/"

  30. # get all databases
  31. all_db="$($MYSQL -u $db_user -h $db_host -p$db_passwd -Bse 'show databases')"

  32. for db in $all_db
  33. do
  34. $MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db | $GZIP -9 > "$backup_dir/mgtsql.$db.`date +%Y%m%d`.gz"
  35. done
  36. exit 0;
複製代碼

版主招募中

發表於 2011-12-12 00:31:39 | 顯示全部樓層
我等你喲!  


發表於 2012-3-20 00:49:19 | 顯示全部樓層
HOHO~~~~感謝分享~~  


您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2016-12-4 14:02 , Processed in 0.057004 second(s), 18 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表