Scheduled Database Backup in Linux

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.