Database backup and detach log table (by month) SH script

Source: Internet
Author: User

#!/bin/SH Year=`Date+%Y ' Month=`Date+%m ' Day=`Date+%d ' Hour=`Date+%H 'dir="/data/dbbackup/file"#备份路径 #db数据备份db_sqlname="Db_bk.sql"#备份成的文件名 Db_sqlnamelog="Db_bklog.sql"#备份成的文件名 db_host="127.0.0.1"#数据库主机 Db_user=""#数据库用户名 db_passwd=""#数据库密码 Db_dbname="Threecards"#数据库名mkdir$dir/$db _dbname"-"$year $month$day$hour #创建备份路径 bkdir=$dir"/"$db _dbname"-"$year $month$day$hourignore="--ignore-table="$db _dbname"."Ignore_log_credit= $ignore"Log_credit"#忽略两个按月份拆表的log ignore_log_table= $ignore"log_table"db_LOG="Log_credit log_table"#忽略两个按月份拆表的log表名 Count=$ (($year- -)* A+ $month-4))   forIinch$(seq$count) DoSyear=$ (($i +3)/ A+ -)) Smonth=$ (($i +4-($syear- -)* A))  if[$smonth-geTen ]   ThenSmonth=$smonthElseSmonth=0$smonthfiLog_credit= $ignore"Log_credit_"$syear $smonth log_table= $ignore"Log_table_"$syear $smonth Ignore_log_credit= $ignore _log_credit" "$log _credit ignore_log_table= $ignore _log_table" "$log _table db_LOG= $db _log"Log_credit_"$syear $smonth"Log_table_"$syear $smonth" "   DoneIgnore_log= $ignore _log_table" "$ignore _log_creditEcho "Main DB backup Start"/usr/bin/mysqldump $ignore _log-h$db_host-u$db_user-p$db_passwd $db _dbname $db _tbname > $bkdir/$db _sqlnameEcho "Log db backup start"  Echo$db _log/USR/BIN/MYSQLDUMP-H$DB_HOST-U$DB_USER-P$DB_PASSWD $db _dbname $db _tbname $db _log> $bkdir/$db _sqlnamelogEcho "Delete expired File"  Find$dir-type F-mtime +5-execRM-f {} \; #清除5天前过期备份Echo "Tar"CD $bkdirTar-ZCVF $bkdir". tar.gz"./Echo "Delete"  RM-RF $bkdir

Database backup and detach log table (by month) SH script

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.