MySQL scheduled backup script

Source: Internet
Author: User

#/bin/bash
Set-x
#-------------------------------------------------------------
Database=cmdb
User=root
Password=root
#-------------------------------------------------------------
time=$ (Date +%y%m%d%h%m%s)
folder=$ (ls/opt | grep-w mysqldatabase_bak)
#-------------------------------------------------------------
#database Exprot
if [$folder = = Mysqldatabase_bak]; Then
Mysqldump-u$user-p$password $database >/opt/mysqldatabase_bak/$database $time.sql
Zip-r/opt/mysqldatabase_bak/$database $time.sql.zip/opt/mysqldatabase_bak/$database $time.sql
Rm-f/opt/mysqldatabase_bak/$database $time.sql
Else
Mkdir/opt/mysqldatabase_bak
Mysqldump-u$user-p$password $database >/opt/mysqldatabase_bak/$database $time.sql
Zip-r/opt/mysqldatabase_bak/$database $time.sql.zip/opt/mysqldatabase_bak/$database $time.sql
Rm-f/opt/mysqldatabase_bak/$database $time.sql
Fi
#-------------------------------------------------------------
#database periodically delete
Find/opt/mysqldatabase_bak-mtime +7-name "*.zip"-exec rm-rf {} \;
#-------------------------------------------------------------
#crontab
crontab=$ (cat/var/spool/cron/root | grep mysqldatabase_bak_for_centos.sh | awk ' {print $8} ')
if [$crontab = =./mysqldatabase_bak_for_centos.sh]; Then
echo "Crontab is ok!"
Else
echo "1 1 * * * cd/opt/mysqldatabase_bak;bash./mysqldatabase_bak_for_centos.sh" >>/var/spool/cron/root
Fi
#-------------------------------------------------------------
#move
pwd=$ (PWD)
if [$pwd = =/opt/mysqldatabase_bak]; Then
echo "The Scripts directory is ok!"
echo "The Scripts directory Is/opt/mysqldatabase_bak"
Else
CP Mysqldatabase_bak_for_centos.sh/opt/mysqldatabase_bak
Rm-f mysqldatabase_bak_for_centos.sh
Fi

MySQL scheduled backup 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.