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:
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
DATETIME.tar.gz’
# The host
HOST = localhost
# Username
DB_USER = root
DB_PWD = root
DATABASE = myDB
# Create a backup path
[! -D "
DATETIME” ] && mkdir -p “
DATETIME "
instruction performs backup of the database of mysql #
mysqldump -u
{DB_PWD} --host=$HOST $DATABASE | gzip >
DATETIME / $ DATETIME.sql.gz
# packaged backup file
cd $ the BACKUP
tar -zcvf $ DATETIME.tar.gz $ DATETIME
# delete temporary directory
rm -rf
DATETIME
# Delete the backup file 10 days before
the Find the BACKUP $ -mtime +10 -name "* .tar.gz" -exec RM -rf {};
echo "Backup success"
Finally, the crontab -e add instruction timer task
10 2 * * * /usr/sbin/mysql_db_backup.sh
Finally, to complete the shell script!