MySQL backup of Shell programming under Linux (for beginners)

Source: Internet
Author: User
Tags mysql backup

1,shell Common Variable parsing:

The name of the current script

$n the nth parameter of the current script

$* all parameters of the current script (excluding the program itself)

$# the number of arguments to the current script (excluding the program itself)

$? After the execution of a command or program is completed, a general return of 0 indicates success.

$UID the ID of the current user

$PWD the directory where you are currently

-ne Not equal to

-eq equals

2, automatic backup MySQL script idea

①, backing up the DB command

mysqldump-h127.0.0.1-uroot-p123456 du >du_ ' date +%y%m%d '. sql

②, the mechanism of backup

③, backup targets and libraries

④, how many days ago data is deleted periodically

#!/bin/bash#auto backup mysql datebases. #by  2017#define db info pathsql_cmd = "/usr/bin/mysqldump" sql_host= "127.0.0.1" sql_usr= "root" sql_pwd= "123456" sql_db= "du" sql_dir= "/data/backup/" date  +%y%m%d ' "#判断是否为root用户if  [  $UID  -ne 0 ];thenecho " Only use root  to exec. " exit fi# determines if the path exists and does not exist to create a if [ ! -d  $SQL _dir ];thenmkdir -p  $SQL _dirfi #备份数据库 $SQL _cmd -h$sql_host -u$sql_usr -p$sql_pwd  $SQL _db > $SQL _dir/$SQL _db.sql# Determine if the backup succeeded, print out the path successfully if [ $? -eq 0 ];thenecho  "backup mysql already  Successful. " echo  "Backup path: $SQL _dir" elseecho  "backup mysql failed." fi# Delete backup files from 30 days ago cd  $SQL _dir/. / ; find . -mtime +30 -exec rm -rf {} \;echo  "Done" # Add the script to the crontab task plan grep  "MySQL"  /var/spool/cron/root >> /dev/nullif [ $? -ne 0 ];thenecho  "0 0 * * * /bin/bash /root/ Shell/mysql.sh > /tmp/mysql.log 2>&1 " >>/var/spool/cron/root/etc/init.d/ Crond restartfi



650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/9A/BA/wKioL1lZ5uagVze4AAQF1aT1ixQ418.png "title=" Enjoy. png "alt=" Wkiol1lz5uagvze4aaqf1at1ixq418.png "/>

This article is from the "Tiandaochouqin" blog, make sure to keep this source http://taindaochouqin.blog.51cto.com/12995943/1944062

MySQL backup of Shell programming under Linux (for beginners)

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.