Use Crontab to automatically back up databases under Linux

Source: Internet
Author: User

Use Crontab to automatically back up databases under Linux

Here are the steps to perform a simple database backup using the Add and mysqldump of the crontab Timer task, as follows:


1. Write a script:/serverback/autobackmysql.sh

The contents are as follows:

Method One:

# #使用mysqldump备份数据库erms

/usr/local/mysql/bin/mysqldump-uroot-ppwd erms >>/serverback/mysql_back/erms_$ (date+ "%Y_%m_%d"). sql

# #找到/serverback/mysql_back/The file name starts with Erms_, ends with. sql, and the file was modified by the system 7 days ago, delete it

find/serverback/mysql_back/-mtime +7-name "Erms_*.sql"-exec rm-rf {} \;

Method Two:

/usr/local/mysql/bin/mysqldump-uroot-ppwd dbname > dir/db_ ' Date +%f '. sql

# #保留近一周的备份文件, delete earlier

Find/dir-mtime +7-name "Db_*.sql"-exec rm-rf {} \;

Method Three:

Filename= ' Date +%y%m%d '

/usr/local/mysql/bin/mysqldump-uroot-proot erms >>/serverback/mysql/$filename. sql


Note: A. The mysqldump is best used in absolute path, if the direct use of mysqldump may be backed up into empty files

B. To ensure that the contents of the script are accurate, you can run each command individually, such as the Execute mysqldump command:/usr/local/mysql/bin/mysqldump-uroot-ppwd erms >>/serverback/ mysql_back/erms_$ (date+ "%y_%m_%d"). sql

C.find in the command, at the end; Semicolons cannot be omitted


2. Add a Scheduled task

Crontab–e # #编辑定时任务

Add timed Task content:

* * * * */serverback/autobackmysql.sh # #每天定时15:00:00 Execute script/serverback/autobackmysql.sh


Command:

Crontab–e # #编辑定时任务

Crontab–r Delete all Scheduled Tasks

Crontab–l List all Scheduled tasks



Use Crontab to automatically back up databases under Linux

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.