How Linux implements scheduled backups of MySQL database (code)

Source: Internet
Author: User
This article brings you the content is about how to implement a regular backup of Linux MySQL Database (code), there is a certain reference value, the need for a friend can refer to, I hope to help you.

1. Check the disk space situation:

[root@localhost backup]# df-h file system                 capacity  used with  available% mount point/dev/mapper/centos-root   17G  2.7G   15G   16%/devtmpfs                 476M     0  476M    0%/devtmpfs                    488M     0  488M    0%/DEV/SHMTMPFS                    488M  7.7M  480M    2%/runtmpfs                    488M     0  488M    0%/sys/fs/cgroup/dev/sda1               1014M  130M  885M   13%/boottmpfs                     98M     0   98M    0%/run/user/0[root@localhost backup]#

Select the appropriate disk to hold the backup file

2. Create a backup directory:

Cd/homemkdir BACKUPCD Backup

3. Create a backup shell script:

Create a backup script under the directory you created (vi bkdatabasename.sh)

#!/bin/bashmysqldump-uroot-proot Rtak >/data/backup/rtak_$ (Date +%y%m%d_%h%m%s). Sqlmysqldump-uroot-proot Rtak | gzip >/data/backup/rtak_$ (Date +%y%m%d_%h%m%s). sql.gz

Note:

Bkdatabasename.sh replaced with interesting names

SQL backup and GZ Backup can be either optional or full backup

User name password needs to be replaced

4. Add executable permissions:

chmod u+x bkdatabasename.sh

Test whether the file is performing normally (./bkdatabasename.sh)

Note: (1) If error mysqldump: Command not Found, execute

Ln-fs/usr/local/mysql/bin/mysqldump/usr/bin (/usr/local/mysql path to MySQL installation path)

(2) If there is a warning (warning:using a password on the command line interface can be insecure.) may be ignored.

(3) Check whether the backup SQL file is normal and import the database correctly

5. Add a Scheduled task

Confirm that the crontab is installed:

Execute the crontab command if you do not found, it indicates that there is no installation

Execute command:

Crontab-e

Enter the following and save:

*/* * 1 * */data/backup/bkdatabasename.sh

/* * 1 * */several * represents the minute of the day of the month the days of the week perform a backup operation

Example: Perform backup per minute/1 * * * */(TESTED)

Perform backup daily 3 o'clock in the morning/00 3 * * */(not tested)

6. Stop the backup operation

When you do not need a scheduled backup, perform this operation, the normal process to the fifth step is completed ~

Crontab-r

Note: Timely cleanup of long-overdue SQL backups to prevent the disk from being fully occupied

Related Article

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.