Automatic MySQL backup in Linux

Source: Internet
Author: User
Tags mysql backup

MySQL supports regular backup using MySQL administrator in windows, which is troublesome in Linux, if there is no ready-made product, you can only write it by yourself.

1. Create a MySQL. Sh file and enter
! /Bin/sh
# backup MySQL database

# delete file
Find/usr/local/backup/MySQL-type F-mtime + 7 -exec RM {}\;

RQ = 'date + % Y % m % d % H % m'
# backup each database
for dB in 'echo "show databases "|/usr/local/MySQL/bin/MySQL-uroot-p654123 | grep-V database ';
DO
echo $ dB;
/usr/local/MySQL/bin/mysqldump-Q-a-c-uroot-pxxxx $ db | gzip> 'echo "/usr/local/backup/MySQL/ other_db ($ Db1__1_rq. SQL .gz "'
done

The general idea is to delete the backup file seven days ago to avoid occupying too much hard disk space and then traverse all databases, backup files one by one. The backup files are compressed to save space.
modify the directory name and password in the file as needed.

2. Modify the file attributes so that they can be executed.
# chmod + x MySQL. sh

3. If you manually execute the file, the backup can be completed. However, our goal is to enable the system to automatically back up the file regularly, therefore, you also need to use the Linux Crontab function.
# vi/etc/crontab
Add
01 3 *** root/usr/sbin/MySQL. sh
indicates that the backup is performed at three o'clock every day. Modify the file name
4. Restart crond
#/etc/rc. d/init. d/crond restart
. You can view the backup file in the Backup Directory every day.

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.