配置文件以下mysql
##########BACKUP MYSQL CONFIG####### #database name db_name=test db_user=root db_pass=123456 #####backup day config begin ##### day_backupdir=/home/backup/db/day day_backup_fix=day day_interval=1 #####backup day config end ####### #####backup week config begin ##### week_backupdir=/home/backup/db/week week_backup_fix=week week_interval=2 #####backup week config end ####### #####backup month config begin ##### month_backupdir=/home/backup/db/month month_backup_fix=month month_interval=3 #####backup month config end #######
名稱爲: db_config
sql
shell腳本以下:shell
#!/bin/bash # Name:bak_ftp_day.sh # This is a ShellScript For Auto File Backup and Delete old Backup # #DIR變量爲當前shell腳本的目錄 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #使用source命令導入平級目錄下的配置文件,引入之後就能夠直接用配置中的變量名來獲取文件中的值了 source $DIR"/"db_config time=_` date +%Y_%m_%d_%H_%M_%S `_ echo "------bakup---<<<--`date +%Y-%m-%d-%H-%M-%S`----begin--->>>---"; echo $day_backupdir/$db_name$time$day_backup_fix.sql.gz"---is --begin---"; mysqldump -u $db_user -p$db_pass $db_name | gzip > $day_backupdir/$db_name$time$day_backup_fix.sql.gz echo $day_backupdir/$db_name$time$day_backup_fix.sql.gz"---is --finsh---"; find $day_backupdir -name $db_name"*.sql.gz" -type f -mmin +$day_interval -exec rm -rf {} \; > /dev/null 2>&1 echo "------bakup----<<<---`date +%Y-%m-%d-%H-%M-%S`----finsh--->>>---"; echo "";