#!/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