Using the shell to automatically back up the database

Source: Internet
Author: User

#!/bin/sh#mysql Address # Detects if the user entered the password manually mysql_host= "" #mysql用户mysql_user = "" #mysql密码mysql_password = "#mysql数据库名 # If you do not fill in the database name you want to export the error message and terminate execution, note [] There must be a space on both sides if [!-n ""];then echo "You did not enter the database name" echo "format: xxx.sh database name" EXITFIMYSQ L_name= "$" #mysql安装路径mysql_path = "/usr/bin/" #备份文件存放路径bak_path = "/root/shell/mysqlbak/${mysql_name}" #删除过期数据 True means false to represent no delete_overdue_data= "true" #删除多少天前数据 default 30-day delete_overdue_data_days=30# detect if directory exists if there is no automatic creation of directory if [!-X] ${bak_path} "];then mkdir" ${mysql_name} "chmod 755" ${bak_path} "fi# Backup time bak_date= ' Date +%f ' ${mysql_path}/mysqldump- -opt-u${mysql_user}-p${mysql_password}-h${mysql_host} ${mysql_name} | Gzip > ${bak_path}/${bak_date}.sql.gz# detect whether to delete expired data if ["${delete_overdue_data}"! = "true"];then echo "Backup data Complete ..." E xitfi# Delete Outdated backup data # Get today's date the_date_today= ' Date +%y%m%d ' #获取要删除的文件find_date_file = ' date-d ' ${the_date_today} ${delete_ Overdue_data_days} days ago "+%y-%m-%d" ' #进入指定目录cd ${bak_path} #查找要删除的数据ls-l|grep ${find_date_file} #取得查找结果0不存在 1 existence Result=$?echo${result}if [${result} = = 0];then echo "Delete expired data" ' Rm-f ${find_date_file}.* ' 

  

Using the shell to automatically back up the database

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.