Linux shell script regular maintenance of the database

After Hanshun Ping followed the teacher's video to learn the basic Linux commands and shell programming a week to
review the application and make a shell script regular maintenance case database for later.

Script requirements are as follows:I put my name into the database myDB

Database backup timer #
# backup path
the BACKUP = / Data / Backup / DB
# Get the current time as the file name
DATETIME = $ (date +% Y_ % m_% d_% H% M% S)

echo “=Start Backup= "
Path echo 'backup is B A C K The P / BACKUP/ DATETIME.tar.gz’

# The host
HOST = localhost
# Username
DB_USER = root
DB_PWD = root
DATABASE = myDB
# Create a backup path
[! -D " B A C K The P / BACKUP/ DATETIME” ] && mkdir -p “ B A C K The P / BACKUP/ DATETIME "
instruction performs backup of the database of mysql #
mysqldump -u D B The S E R p {DB_USER} -p {DB_PWD} --host=$HOST $DATABASE | gzip > B A C K The P / BACKUP/ DATETIME / $ DATETIME.sql.gz
# packaged backup file
cd $ the BACKUP
tar -zcvf $ DATETIME.tar.gz $ DATETIME
# delete temporary directory
rm -rf B A C K The P / BACKUP/ DATETIME

# Delete the backup file 10 days before
the Find the BACKUP $ -mtime +10 -name "* .tar.gz" -exec RM -rf {};
echo "Backup success"
As FIG sourceFinally, the crontab -e add instruction timer task
10 2 * * * /usr/sbin/mysql_db_backup.sh

Finally, to complete the shell script!

Released seven original articles · won praise 8 · views 860

Guess you like

Origin blog.csdn.net/qq_44320146/article/details/104883062