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)