Mysql automatic backup script and remote timed FTP

Source: Internet
Author: User
Tags mysql automatic backup mysql backup

Share a self-written mysql automatic backup script, timed execution settings, and windows automatic FTP. Please advise.

Prerequisites: vsftpd is enabled on the mysql database server, and an appropriate account is configured for FTP

Step 1: Compile the mysql Automatic Execution script

 

 
 
  1. #!/bin/sh 
  2. # mysql_db_backup.sh: backup mysql databases. 
  3. # Last updated: Wed Nov  9 07:01:01 CST 2011 
  4. # ---------------------------------------------------------------------- 
  5. # This is a free shell script under GNU GPL version 2.0 or above 
  6. # Copyright (C) 2011 Andy Yao 
  7. # Blog:http://t.qq.com/andy_microblog 
  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.  
  15. db_user="root" 
  16. db_passwd="123456" 
  17. db_host="192.168.1.11" 
  18. # the directory for story your backup file. 
  19. backup_dir="/mnt/sdb1/mysql_db_backup" 
  20. # date format for backup file (dd-mm-yyyy) 
  21. time="$(date +"%Y-%m-%d_%H-%M-%S")" 
  22. file_time="$(date +"%Y-%m-%d_%H-%M-%S")" 
  23.  
  24.  
  25. mysql_backup_path="$backup_dir/$file_time" 
  26. mkdir $backup_dir/$file_time 
  27. log_path="$backup_dir/$file_time.log.txt" 
  28.  
  29. #------------this log is for monitor ssh status 
  30. ssh_log_path="$backup_dir/log.txt" 
  31.  
  32.  
  33. echo "---------------------" >> $ssh_log_path 
  34. date >> $ssh_log_path 
  35.  
  36.  
  37. echo "-------------------------------------------------------------------------------" >> $log_path 
  38. echo "--------------" >> $log_path 
  39. echo "--------" >> $log_path 
  40.  
  41.  
  42. echo "backup mysql db start" >> $log_path 
  43. date >> $log_path 
  44. echo "---------------------" >> $log_path 
  45.  
  46.  
  47. #!/bin/bash 
  48. cat /dev/null > $backup_dir/mysqlback.txt 
  49.   connmsg=`mysql -h$db_host -u$db_user -p$db_passwd $db <<EOF 
  50.   show databases; 
  51.   exit 
  52.   EOF` 
  53.   echo "$connmsg" > $backup_dir/mysqlback.txt 
  54.  
  55. while read line 
  56.   do 
  57.  
  58. if [ "$line" != "Database" ]; then 
  59. #mysqldump -u$user -p$ps "$line" >/share/"$line".sql 
  60.  
  61.         echo "--------" >> $log_path 
  62.         date >> $log_path 
  63.         echo "$line" >> $log_path 
  64.          
  65.         mysqldump -h$db_host -u$db_user -p$db_passwd "$line" --lock-tables=false | gzip -9 > "$mysql_backup_path/$line.$time.sql.gz" 
  66.  
  67.         date >> $log_path 
  68.         echo "--------" >> $log_path 
  69.  
  70. fi 
  71.  
  72. done < $backup_dir/mysqlback.txt 
  73.  
  74.  
  75. echo "---------------------" >> $log_path 
  76. echo "backup mysql db stop" >> $log_path 
  77. date >> $log_path 
  78.  
  79. echo "--------" >> $log_path 
  80. echo "--------------" >> $log_path 
  81. echo "-------------------------------------------------------------------------------" >> $log_path 
  82.  
  83. #------------this log is for monitor ssh status 
  84. date >> $ssh_log_path 
  85. echo "---------------------" >> $ssh_log_path 
  86.  
  87. ls -l $mysql_backup_path >> $log_path 
  88.  
  89. echo "--------------" >> $log_path 
  90.  
  91. cd $backup_dir 
  92. du -s >> $log_path 
  93. du -sm >> $log_path 
  94. du -sh >> $log_path 
  95.  
  96. echo "--------------" >> $log_path 
  97.  
  98. du -h |sort -rk2 >> $log_path 
  99.  
  100. exit 0; 

 

Step 2: Execute the mysql backup script regularly and set crontab. Do you have to explain this?

 

 
 
  1. [root@localhost /]# cat /etc/crontab 
  2. SHELL=/bin/bash 
  3. PATH=/sbin:/bin:/usr/sbin:/usr/bin 
  4. MAILTO=root 
  5. HOME=/ 
  6.  
  7. # run-parts 
  8. 01 * * * * root run-parts /etc/cron.hourly 
  9. 02 4 * * * root run-parts /etc/cron.daily 
  10. 22 4 * * 0 root run-parts /etc/cron.weekly 
  11. 42 4 1 * * root run-parts /etc/cron.monthly 
  12. 01 7 * * * root /mysql_db_backup/mysql_db_backup_auto_db_filelist.sh 
  13. * */1 * * * root ntpdate 203.129.68.14 && hwclock -w 
  14. [root@localhost /]# cat /etc/crontab 

 

Step 3: automatically schedule FTP on windows, save the following code as bat, and set the scheduled task

 

 
 
  1. @ Echo off & color 1f & title automatic FTPMYSQL backup file
  2. Mode con: cols = 60 lines = 10
  3. Echo ===================================================== ==============================
  4. Echo --
  5. Echo --
  6. Echo -- ---- ### automatic FTPMYSQL backup file ###----
  7. Echo --
  8. Echo --
  9. Echo -- processing in progress. Do not close the program window manually,
  10. Echo --
  11. After echo -- is completed, the program will automatically close...
  12.  
  13. Set xtime = % time: = %
  14. Set xdate = % date %
  15. Set copy_path = % xdate :~ 0, 4%-% xdate :~ 5, 2%-% xdate :~ 8, 2% _ 07-01-01
  16.  
  17. Rem specifies the LOG storage path
  18. Set log_path = c: \ bat \ log \ ftp_mysql_copy.log.txt
  19.  
  20. Echo --------------------------------------> % log_path %
  21. Echo --------------------> % log_path %
  22. Date/t> % log_path % & time/t> % log_path %
  23. Echo -- start ------------------> % log_path %
  24.  
  25. Cd E: \ MYSQL_BACKUP_12
  26. E:
  27. Md % copy_path %
  28. Cd % copy_path %
  29.  
  30. Echo open 192.168.1.11> ftp. src
  31. Echo username> ftp. src
  32. Echo password> ftp. src
  33. Echo cd/software/mysql_db_backup/% copy_path %/> ftp. src
  34. Echo pwd> ftp. src
  35. Echo ls> ftp. src
  36. Echo prompt> ftp. src
  37. Echo bin> ftp. src
  38. Echo mget *> ftp. src
  39. Echo bye> ftp. src
  40. Ftp-s: ftp. src
  41. Del ftp. src
  42.  
  43. Echo -- end ------------------> % log_path %
  44. Date/t> % log_path % & time/t> % log_path %
  45. Echo --------------------> % log_path %
  46. Echo --------------------------------------> % log_path %

 

After completing the above steps, you can start testing.

This article is from the "Bodhi Tree" blog, please be sure to keep this source http://andyptz.blog.51cto.com/5736197/965420

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.