Linux shell scripts automatically delete data for the first 3 months of MySQL tables

Source: Internet
Author: User

RDS has a database table Tbl_online table with 60 million data that needs to be cleaned up and retained for 3 months.

The steps are as follows:

    1. Create a monthly table

    2. Write data that needs to be deleted to the monthly table

    3. Delete data

    4. Optimizing tables


The shell is as follows:


#!/bin/bash


Base= "Rdsxx.mysql.rds.aliyuncs.com"


#前3个月的日期

months_ago= ' date-d '-3 month "+"%y-%m-%d "'

#前3个月的月份, remove the previous 0

months_ago_mon= ' date-d '-3 month "+"%-m "'

#前3个月年份

Years_ago_year= ' date-d '-3 month "+"%-y "'


#+++++++++++++++++++++++++++++++++++++

Statime= ' Date +%y-%m-%d ' "%h:%m:%s '

echo "############################" >>/opt/tbl_online.log

#创建月表

/usr/local/mysql/bin/mysql-u root-h $base-E "Use kd_shop;create table if not exists tbl_online_bak_" $years _ago_year "_$m Onths_ago_mon like Tbl_online; "


echo "$statime start inserting" $months _ago "data" >>/opt/tbl_online.log

/usr/local/mysql/bin/mysql-u root-h $base-E "use Kd_shop;insert into Tbl_online_bak_" $years _ago_year "_$months_ago_ Mon select * from tbl_online where Create_time >= ' $months _ago 00:00:00 ' and Create_time <= ' $months _ago 23:59:59 ';


Statime= ' Date +%y-%m-%d ' "%h:%m:%s '

echo "$statime Insert data Complete" >>/opt/tbl_online.log


#++++++++++++++++++++++++++++++++++++++

Statime= ' Date +%y-%m-%d ' "%h:%m:%s '


echo "$statime start deleting data and Tuning tables" >>/opt/tbl_online.log

/usr/local/mysql/bin/mysql-u root-h $base-E "use Kd_shop;delete from Tbl_online where Create_time >= ' $months _ago 00 : 00:00 ' and Create_time <= ' $months _ago 23:59:59 '; optimize table tbl_online; "


Statime= ' Date +%y-%m-%d ' "%h:%m:%s '

echo "$statime Delete and optimize table completion" >>/opt/tbl_online.log


You can use the final task plan bar

This article is from the "Falling Star" blog, make sure to keep this source http://xiao987334176.blog.51cto.com/2202382/1703300

Linux shell scripts automatically delete data for the first 3 months of MySQL tables

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.