Scenario: The server database needs to be backed up regularly every day1. First determine the location of the backup scriptI put it in /usr/local/backup and named it bkDatabase.sh 2. Write a shell script# A few points to note# 1. -password If the password contains brackets or underscores, please quote the password in double quotes# 2. This script backs up the database course1 and compresses it# 3. The name of the backup file is course and is timestampedmysqldump -uroot -ppassword course1 | gzip > /usr/local/backup/course_$(date +%Y%m%d_%H%M%S).sql.gz 3. Add permissions to bashchmod u+x bkDatabase.sh 4. Test to see if the script is executable correctly./bkDatabase.sh 5. Open the scheduled task# The first time you set up a scheduled task, you may be asked to enter vim to edit the scheduled task. Select basic and you can use crontab -e 6. Editorial ContentI have three scheduled tasks here as shown below: The second one # Indicates execution once every minute. The script to be executed is /usr/local/backup/bkDatabase.sh */1 * * * * /usr/local/backup/bkDatabase.sh 7. View scheduled tasks crontab -lYou can see whether the addition is successful (as shown in the figure) Check whether the backup is successful in the backup directory 8. Step into the pit Some scheduled tasks are closed. Use the command If it is normal, just ignore it. If it does not start, restart it once. For different Linux versions, the distribution has this service Restart service command: [root@centos6 /]# service crond restart Start service command: [root@centos6 /]# service crond start Stop service command: [root@centos6 /]# service crond stop The distribution does not have this service Stop service: [root@centos6 /]# /etc/init.d/cron stop Start the service: [root@centos6 /]# /etc/init.d/cron start Server data restorationThere are too many pitfalls here, pay attention to the way of decompressing gz files! ! ! 1. First, decompress the data backed up by the scheduled taskNote that the course_20190511_214326.sql.gz generated above is decompressed # Unzip the gz file to generate the course_20190511_214326.sql file gunzip course_20190511_214326.sql.gz 2. Then import the data into the databaseIf the target server does not have the database you want, you need to create database target database; # Send the backup file sql to the course table through the < symbol mysql -u root -p indicates the use of mysql database mysql -u root -p course< course_20190511_214326.sql Then you will be prompted to Enter Password, enter the mysql password to import The above is the details of how to implement scheduled database backup in Linux. For more information about scheduled database backup in Linux, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: 20 Signposts on the Road to Becoming an Excellent UI (User Interface) Designer
>>: How to use CSS to fill the parent container div with img images and adjust the container size
Mininet Mininet is a lightweight software defined...
This technique comes from this article - How to a...
Preface In the previous article, I shared with yo...
Table of contents Function call optimization Func...
MySQL is divided into Community Edition (Communit...
I recently bought a Tencent Cloud server and buil...
1. Introduction A few days ago, a development col...
Inject axios into Vue import axios from 'axio...
The company had a well-configured server that was...
#docker ps check, all ports are mapped CONTAINER ...
Table of contents 1. v-bind: can bind some data t...
The table caption can be placed above or below th...
Hide version number The version number is not hid...
Table of contents Use Cases Reactive API related ...
Preface: The installation process will not be des...