Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2346064
  • 博文数量: 276
  • 博客积分: 5998
  • 博客等级: 大校
  • 技术积分: 5175
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-24 14:43
文章分类

全部博文(276)

文章存档

2014年(25)

2013年(11)

2012年(69)

2011年(167)

2010年(4)

分类: Mysql/postgreSQL

2012-01-18 17:40:55



点击(此处)折叠或打开

  1. 1、vim mysql_bak.sh
  2.  
  3. #!/bin/bash

  4. #define connection parameters
  5. DB_USER="root"
  6. DB_PASSWORD="123456"
  7. DB_HOST="localhost"
  8. BACKUP_DATABASES="test D1 D2"

  9. #define the directory for store backup file.
  10. BACKUP_DIR="/backup"
  11. BACKUP_LOG="/scripts/mysqlbak/mysqlbak.log"
  12. BACKUP_EMAIL="/scripts/mysqlbak/mail.py"

  13. #mysql,mysqldump and other unilities's path
  14. FIND="/usr/bin/find"
  15. MYSQL="/usr/local/mysql/bin/mysql"
  16. MYSQLDUMP="/usr/local/mysql/bin/mysqldump"
  17. MKDIR="/bin/mkdir"
  18. RM="/bin/rm"
  19. MV="/bin/mv"
  20. TAR="/bin/tar"
  21. XARGS="/usr/bin/xargs"
  22. PYTHON="/usr/local/bin/python"
  23. DATE="/bin/date"
  24. #define the date format for backup file(dd-mm-yyyy)
  25. TIME="$($DATE %d-%m-%y)"
  26. DATETIME="$($DATE "%Y-%m-%d %T")"

  27. # mkdir backuo direcotry if not exits
  28. test ! -d $BACKUP_DIR && $MKDIR $BACKUP_DIR

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

  31. #begin to backup
  32. echo "##########$DATETIME begin to backup mysql datebases##############" >$BACKUP_LOG
  33. echo -e '\n' >> $BACKUP_LOG
  34. cd $BACKUP_DIR
  35. for DB in $BACKUP_DATABASES

  36. do
  37.     $MYSQLDUMP -u$DB_USER -h$DB_HOST -p$DB_PASSWORD $DB > "$BACKUP_DIR/$TIME.$DB.sql" 2>> $BACKUP_LOG

  38.     #check the backup process is successfull
  39.     if [[ $? == 0 ]];then
  40.         $TAR czf $TIME.$DB.tar.gz $TIME.$DB.sql && $RM -rf $TIME.$DB.sql
  41.         DATETIME="$($DATE "%Y-%m-%d %T")"
  42.         echo "$DATETIME BACKUP $DB is successfull" >>$BACKUP_LOG
  43.         BAK_SUCCESS_DB="$BAK_SUCCESS_DB$DB,"
  44.       else
  45.   
  46.         DATETIME="$($DATE "%Y-%m-%d %T")"
  47.         echo "$DATETIME BACKUP $DB fail" >> $BACKUP_LOG
  48.         BAK_FAIL_DB="$BAK_FAIL_DB$DB,"
  49.     fi

  50. done

  51. DATETIME="$($DATE "%Y-%m-%d %T")"
  52. echo "$DATETIME BACKUP process is end" >>$BACKUP_LOG

  53. #delete old files
  54. #echo -e '\n' >> $BACKUP_LOG
  55. DATETIME="$($DATE "%Y-%m-%d %T")"
  56. echo "$DATETIME begin to delete 30 old back files " >> $BACKUP_LOG
  57. for i in `$FIND $BACK_DIR -type f -mtime 30`
  58. do
  59.    $RM -rf $i && echo "has rm $i sucessfully" >>$BACKUP_LOG
  60.    RM_BAK_DB="$RM_BAK_DB$i,"
  61. done

  62. echo -e '\n' >> $BACKUP_LOG
  63. DATETIME="$($DATE "%Y-%m-%d %T")"
  64. echo "$DATETIME backup $BAK_SUCCESS_DB is sucessfull" >> $BACKUP_LOG
  65. echo "$DATETIME backup $BAK_FAIL_DB is failed" >> $BACKUP_LOG
  66. echo "$DATETIME $RM_BAK_DB is deleted" >> $BACKUP_LOG
  67. echo "$DATETIME backup mysql databases and delete 30 old backup files sucessfully " >> $BACKUP_LOG

  68. # begin to send email to xxx@xxx.com
  69. $PYTHON $BACKUP_EMAIL




  70. 2、cat mail.py
  71. #/usr/bin/env python

  72. # import smtplib library and email modules we'll need

  73. import smtplib
  74. import email
  75. from email.MIMEText import MIMEText
  76. #from email.mime.text import MIMEText
  77. #define the smtp server address and it's port
  78. mail_server= 'xxx.xxx.com'
  79. mail_server_port = 25

  80. #define from and to address
  81. from_addr= 'xxx@xxxx.com'
  82. to_addr= 'xxx1@xxx.com,xxx2@xxx.com,xxx3@xxx.com'

  83. #format the from and to address
  84. from_header='From: %s\r\n' % from_addr
  85. to_header= 'To: %s\r\n' % to_addr
  86. subject_header= 'Subject: Mysql databases backup log'

  87. #get the email content from txt file
  88. textfile='/data/vmstat'
  89. fp = open(textfile,'rb')
  90. msg= MIMEText(fp.read());
  91. fp.close()
  92. #msg='tset teste test'
  93. email_message = '%s%s%s\r\n\r\n%s' % (from_header,to_header,subject_header,msg)

  94. #begin to send email
  95. s = smtplib.SMTP(mail_server,mail_server_port)
  96. s.set_debuglevel(1)
  97. s.starttls()
  98. s.login("xxx@xxx.com","mima")
  99. s.sendmail(from_addr,to_addr.split(',


阅读(1680) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~