Provides various official and user-released code examples. For code reference, you are welcome to learn how to regularly back up MYSQL databases using crontab in linux.
You only need to follow the three steps below, and everything is under your control:
Step 1: configure the Backup Directory code on the server:
--------------------------------------------------------------------------------
mkdir /var/lib/mysqlbackup
cd /var/lib/mysqlbackup
--------------------------------------------------------------------------------
Step 2: Write the backup script code:
--------------------------------------------------------------------------------
vi dbbackup.sh
--------------------------------------------------------------------------------
Paste the following code to change the username, password, and dbname.
Code:
--------------------------------------------------------------------------------
#! /Bin/sh
Mysqldump-uuser-ppassword dbname | gzip>/var/lib/mysqlbackup/dbname 'date encoding policy-m-1_d_1_h1_m1_s'. SQL .gz
Cd/var/lib/mysqlbackup
Rm-rf 'Find.-name' *. SQL .gz '-mtime 10' # Delete the backup file 10 days ago.
--------------------------------------------------------------------------------
Step 3: Change the backup script permission
Code:
--------------------------------------------------------------------------------
chmod +x dbbackup.sh
--------------------------------------------------------------------------------
Step 4: Use crontab to regularly execute the backup script code:
--------------------------------------------------------------------------------
crontab -e
--------------------------------------------------------------------------------
If you back up data at every night, add the following code,
Code:
--------------------------------------------------------------------------------
00 21 * * * /var/lib/mysqlbackup/dbbackup.sh
--------------------------------------------------------------------------------
Problems:
1. mysqldump is the command for backing up the database. If you don't know how to back up the database, just use Baidu.
2. Crontab is the command for scheduled tasks, if you do not understand can access http://www.thinkphp1.cn/code/1003.html
3. to back up data, you must first confirm that the password has been set for the root user. Otherwise, Got error: 1045: Access denied for user 'root' @ 'localhost' (using password: YES) will be reported during mysqldump execution) when trying to connect
Solution:
Log on to the mysql client
Mysql-hserverip-uroot-p
Mysql> use mysql; Database changed
Mysql> update user set password = password ('new password') where user = 'root'; Query OK, 4 rows affected (0.00 sec) Rows matched: 4 Changed: 4 Warnings: 0
Mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) // This command grants the user a new permission or password and directly reads data in the memory without restarting the database to prevent errors
Mysql> quit
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB