Because the individual station can not guarantee higher availability, there may be data easy problems, so it is necessary to achieve backup database needs, so I found some information on the Internet, combined with my actual situation, through the use of scripts to achieve 12 o'clock in the morning daily backup database, every Sunday time deleted seven days ago the database backup files. Below, I am the backup database of my script posted out for everyone to learn and reference.
First, this is the script that backs up the database.
cat /usr/local/script/backupdatabase #!/bin/bash #Shell command For Backup MySQL Database Everyday Automatically By Crontab #time 2015-5-20 #name huxianglin USER=root PASSWORD=xxxxxxxx DATABASE1=zblog database2=zabbix backup_dir=/data/backup/database/ # The path to the backup database file logfile=/data/backup/database/data_backup.log # Log files for backing up database Scripts date= ' Date +%y%m%d-%h%m -d -3minute ' #获取当前系统时间-3 minutes dumpfile1= $DATE-zblog.sql #需要备份的数据库名称 dumpfile2=$ Date-zabbix.sql archive1= $DUMPFILE 1-tar.gz #备份的数据库压缩后的名称 archive2= $DUMPFILE 2-tar.gz if [ ! -d $BACKUP _dir ]; #判断备份路径是否存在, create the path if it does not exist then mkdir -p "$BACKUP _dir" fi echo -e \ n >> $LOGFILE echo ----------------- -------------------" >> $LOGFILE echo " backup date:$ DATE ">> $LOGFILE echo ------------------------------------" >> $LOGFILE cd $BACKUP _dir # Skip to Backup path /usr/local/mysql/bin/mysqldump -u$USER -p$PASSWORD $DATABASE 1 > $DUMPFILE 1 #使用mysqldump备份数据库 if [[ $? == 0 ]]; then tar czvf $ARCHIVE 1 $DUMPFILE 1 >> $LOGFILE 2>&1 #判断是否备份成功, if the backup succeeds, compress the backup database, or write the error log to the log file. echo "$ARCHIVE 1 backup successful!" >> $LOGFILE rm -f $DUMPFILE 1 else echo "$ARCHIVE 1 backup fail!" >> $LOGFILE   &NBsp; fi /usr/local/mysql/bin/mysqldump -u$user -p$password $ database2 > $DUMPFILE 2 if [[ $? == 0 ]]; then tar czvf $ARCHIVE 2 $DUMPFILE 2 >> $LOGFILE 2 >&1 echo "$ARCHIVE 2 backup successful!" >> $LOGFILE rm -f $DUMPFILE 2 else echo "$ARCHIVE 2 backup fail!" >> $LOGFILE fi
and then, The script to delete the backup file is written.
[[Email protected] database]# cat /usr/local/script/cleandatabase #!/bin/bash#time 2015-05-21#name huxianglinbackupdir= "/data/backup/database/" #定义备份文件路径KEEPTIME =7 #定义需要删除的文件距离当前的天数DELFILE = ' find $BACKUPDIR -type f -mtime + $KEEPTIME -exec ls {} \; ' #找到天数大于7天的文件for delfile in ${delfile} #循环删除满足天数大于七天的文件dorm -f $delfiledone
Finally, It is necessary to write the automatic script execution time in crontab.
Cat /etc/crontab shell=/bin/bashpath=/sbin:/bin:/usr/sbin:/usr/binmailto=root# for details see man 4 crontabs# example of job definition:# .--------------- - minute (0 - 59) # | .------------- hour (0 - 23) #  |  | &NBSP:---------- day of month (1 - 31) # |  |  | &NBSP:------- month (1 - 12) OR jan,feb,mar,apr # | | | |  .---- day of week (0 - 6) (sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat# | | | | |# * * * * * user-name command to be executed 01 00 * * * root / Usr/local/script/backupdatabase # Define a backup database script that executes 0:01 A.M. every day 02 00 * * 0 root /usr/local/script/cleandatabase # Define every Sunday 0:02 A.M. to perform a delete database backup file
The above is my collation of the backup database and the scheduled deletion of the database script, I hope you can give comments and suggestions.
This article is from the "Lemon" blog, be sure to keep this source http://xianglinhu.blog.51cto.com/5787032/1653452
Automating backup databases with shell scripts