How to automatically back up MySQL files every day

Source: Internet
Author: User
Keywords MySQL every day constantly

Intermediary transaction http://www.aliyun.com/zixun/aggregation/6858.html ">seo diagnose Taobao guest cloud host technology Hall

With the continuous application of the database in the construction of the website, most of the websites have been inseparable from the database, because the database can not only store the content of the website, but also can store the information submitted by the users. Since this information is invaluable, it is almost impossible to recover once it is lost.

All webmaster should not only pay attention to the Backup Server page HTML files, pictures or program code, but also to the database file priority backup.

The simplest database backup can be manually backed up by phpMyAdmin and downloaded to a specified location, but for webmasters who need to back up daily in a timely manner, manually backing up the database is more cumbersome, So this article explains how to use the foreign host clock program with the shell command code to implement a regular daily backup database files and download to the specified directory.

Shell Backup Database

First create a file named: "Backup.sh", in which you enter the following command:

#!/bin/bash # Set A value that we can use for a datestamp date= ' DATE +%y-%m-%d ' $ # Our Base backup directory Baseba ckup= "/backup/daily" for DATABASE in ' Cat/backup/db-list.txt ' does # where we throw our backups. Filedir= "$BASEBACKUP/$DATABASE" # Test to the If our backup directory exists. # If not, create it. if [! d $FILEDIR] then mkdir-p $FILEDIR fi echo-n "Exporting database: $DATABASE" mysqldump--user=root--opt $DATABA SE | Gzip-c-9 > $FILEDIR/$DATABASE-$DATE sql.gz echo "... [Done exporting to local backup, now exporting for remote backup] "CP $FILEDIR/$DATABASE-$DATE. sql.gz/backup/uploads/$DATABASE. sql.gz echo" ... [Done] "Do # Autoprune our backups. This would find all the files # that are ' maxfileage ' and delete them. maxfileage=4 find $BASEBACKUP-name ' *.gz '-type f-mtime + $MaxFileAge-exec rm-f {} \;

In simple terms, the command will fetch the database and output + compressed data file (datastamped) to the/backup/daily/databasename/directory from the Db-list.txt file (a list text file with the database name). The command then stores a non-datestamped file under the/backup/uploads/directory and overwrites the original file.

At the same time, to conserve resources, the command automatically deletes the database file 4 days ago.

Clock command (Cron Jobs)

After the backup to use the clock command to the database backup and storage in an automated manner daily, the first need to activate the backup.sh;

4 * * * sh/backup/backup.sh

This command is to allow the server to start the database backup every day 4:10, of course, when the implementation of the time is up to you.

Then enter the following command in the clock command:

6 * * */usr/local/bin/ncftpput-ef/home/admin/ncftpputlogin//backup/uploads/*

This command tells a program named Ncftpput (which is located in the Ncftputlogin directory) to upload the database files to the/backup/uploads directory 6:10 every day. Ncftpputlogin directory contains the server's user information, if your server is not installed NCFTP, you can install one, very simple.

Summary

If you have a large database file, you may need to back up a lot of server resources, so the webmaster should try to select the minimum number of visitors to perform automatic backup command, such as the early morning about 4-5 o ' clock. Also note that you leave enough time for the backup and then execute the Save command.

Because a server may suffer from natural and man-made disasters, backed up database files to have different servers or download to local more insurance.

If this article is helpful to you, please keep the original link when you reprint: http://hostgatorfans.com/mysql-daily-backup.html

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.