Use the crontab scheduled task function of Linux itself to execute the script \ of database backup periodically
The article directories
- Implementation function list
- Data Backup dump
- The sample
- A shell script
- Scheduled task crontab
- Crontab usage:
- Content Explanation:
Implementation function list
- The backup
- Regular backup
- Compressing backup Files
- Delete expired backup files
- Restoring backup Files
Data Backup dump
Every database has a command to export the data and structure in the database, that is, backup. Restoring the backup data will delete and rebuild the tables in the original data, and then insert the data in the backup. This is restore. Note that if there is more data before restoration than backup, the data after restoration will be lost.
Mysqldump -h -u [username] -p [database name] > mysqldump -h -u [username] -p [database name] > mysqldump -h -u [username] -p [database name] > mysqldump
The sample
- Backing up a specified database
test
mysqldump -uroot -pxxx test > /backup/mysqldump/test.db
Copy the code
- Restoring the database
test
mysql -uroot -pxxx test < /backup/mysqldump/test.db
Copy the code
Note: Before importing the backup database, if the database to be restored does not exist, it needs to be created. Db can be imported only if the database name is the same as that of test.db. (That is, the name of the backup library and the restore library must be the same)
A shell script
To complete a full-featured backup solution, shell scripts are required. We want this script to be backed up to the specified path and compressed, save up to 30, more than 30 delete the earliest, and log the operation. Don’t say anything. It’s all in the script. Go!
# username
username=root
# your password
password=nicai
The database to be backed up
database_name=l_love_you
# Maximum number of backup files to be saved
count=30
Backup save path
backup_path=/app/mysql_backup
# date
date_time=`date +%Y-%m-%d-%H-%M`
Create a folder if it does not exist
if [ ! -d $backup_path ];
then
mkdir -p $backup_path;
fi
# start backup
mysqldump -u $username -p$password $database_name > $backup_path/$database_name-$date_time.sql
# start compression
cd $backup_path
tar -zcvf $database_name-$date_time.tar.gz $database_name-$date_time.sql
Delete the source file
rm -rf $backup_path/$database_name-$date_time.sql
Update the backup log
echo "create $backup_path/$database_name-$date_time.tar.gz" >> $backup_path/dump.log
Find the backup that needs to be deleted
delfile=`ls -l -crt $backup_path/*.tar.gz | awk '{print $9 }' | head -1`
Check whether the current number of backups exceeds the threshold
number=`ls -l -crt $backup_path/*.tar.gz | awk '{print $9 }' | wc -l`
if [ $number -gt $count ]
then
# delete the earliest backup and keep only the count backup
rm $delfile
Update delete file log
echo "delete $delfile" >> $backup_path/dump.log
fi
Copy the code
Chmod +x dump_mysql.sh: chmod +x dump_mysql.sh: chmod +x dump_mysql.sh: chmod +x dump_mysql.sh: chmod +x dump_mysql.sh: chmod +x dump_mysql.sh: chmod +x dump_mysql.sh: chmod +x dump_mysql.sh: chmod +x dump_mysql.sh
Parameter Meaning of the chmod command -- + indicates that some permissions are added. X indicates that the permissions are executedCopy the code
Scheduled task crontab
Crontab is a scheduled task feature that comes with Linux. You can use it to execute the dump_mysql.sh script once every morning.
Crontab usage:
- Crontab -l Displays the scheduled task list
- Crontab -e Edit (add/delete) scheduled tasks
Run the crontab -e command to open an editable text, enter 00 01 * * * /app/dump_mysql.sh to save and exit.
Content Explanation:
00 01 * * * /app/dump_mysql.sh /app/dump_mysql.sh /app/dump_mysql.sh /app/dump_mysql.sh /app/dump_mysql.sh Periodic expressions are five placeholders representing minutes, hours, days, months, and weeks
Each placeholder with * and is used in the first minute, the second per hour, and so on placeholder with specific figures show that the specific time, in the first 10 is 10, with 10 said in the third, and so on a placeholder in – interval, 5 to 7 is used in the first 5 to 7 points, in the fifth said Friday to Sunday, And so on, the/is used to represent the interval, 5-10/2 is used in the first place to represent the interval of 2 minutes between 5 and 10, 2-5 is used in the second place to represent the interval of 2 hours between 5 and 10, and so on, 5,10 is used in the first place to represent 5 and 10, may and October are used in the fourth place, and so on
Behind will improve and optimize the reference: segmentfault.com/a/119000002…