Shell Exercises (iv)

Source: Internet
Author: User
Tags error status code fpm mysql backup

Exercise 1:Backing Up the database

Requirements: Design a shell script to back up the database, first save a copy of the data on the local server, and then remotely copy a copy, save the week of data locally, Remote Storage for one months

Assume that we know the MySQL root account password, to back up the library is discuz, the local backup directory is/bak/mysql, the remote server IP is, Remote provides a rsync service, the backup address is

After the script is written, it needs to be added to Cron and executed 3 o'clock in the morning every day.

Reference Answer:

#!/bin/bash# date:2018 February 8 path= $PATH:/usr/local/mysql/bin/d= ' date + '%y%m%d ' ' passwd= ' Your_mysql_password ' lo_bak_ Dir= "/bak/mysql" re_bak_dir= "" exec 1>/var/log/mysqlbak.log 2>/var/log/ Mysqlbak.logecho "MySQL backup begin at ' date + '%F%T" '. " MYSQLDUMP-UROOT-P$PASSWD discuz > $lo _bak_dir/$d. Sqlrsync-az $lo _bak_dir/$d. sql $re _bak_dir/$d. Sqlecho "MySQL Backup end at ' date + '%F%T '. '

Cron Task schedule:0 3 * * */bin/bash/usr/local/sbin/

Save the week's data locally for one months at a distance:

Use the Find command to implement: find/lo_bak_dir/-type f-name "*.sql"-exec rm-f {} \;

Exercise 2: automatic restart of the PHP-FPM service

Requirements: The server runs the LNMP environment, there are always 502 phenomena in the near future. 502 Status code for website access, 200 normal, 502 error is the most common error status code nginx. Since 502 is only temporary, and as long as a restart of the PHP-FPM service 502 disappears, but does not restart, it will continue for a long time. So it is necessary to write a monitoring script, monitoring the Access log status code, once the occurrence of 502, then automatically restart the PHP-FPM.

We set:
1. Access_log/data/log/access.log
2. Script dead loop, every 10s detection (assuming the number of log bars per 10s clock is about 300)
3. The way to restart PHP-FPM is/etc/init.d/php-fpm restart

Reference Answer:

Shell Exercises (iv)

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: 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.