MySQL timed automatic backup Database script code (linux/windows)

Source: Internet
Author: User
Tags bz2

Operation Steps:

Method One

1, installation P7zip: Because the source does not have this package, directly download the source code installation.

Address: http://p7zip.sourceforge.net/

The code is as follows Copy Code

wget http://sourceforge.net/projects/p7zip/files/p7zip/9.20.1/p7zip_9.20.1_src_all.tar.bz2
Tar xvf p7zip_9.20.1_src_all.tar.bz2
CD P7zip_9.20.1_src_all
Make && make install

wget http://sourceforge.net/projects/p7zip/files/p7zip/9.20.1/p7zip_9.20.1_src_all.tar.bz2
Tar xvf p7zip_9.20.1_src_all.tar.bz2
CD P7zip_9.20.1_src_all
Make && make install


2. Backup script (back.sh):

The code is as follows Copy Code

#!/bin/sh
Filename= "/home/www/data/" ' date-d last-day +%y%m%d '
Viewdate= ' date-d last-day +%y-%m-%d '
Mysqlcheck-uuser-ppassword Database
Mysqldump-uuser-ppassword--opt--compact--skip-comments database table--where "DataField like

' $viewdate% ' "> $filename". sql "
Sed-i ' s/create/table/&/if/not/exists/' $filename ". sql"
/usr/local/bin/7za a $filename ". 7z" $filename ". sql"
RM $filename ". SQL"

3, with Cron to achieve a daily 0-point automatic backup

The code is as follows Copy Code

* * * * */home/www/data/bash.sh >/dev/null 2>&1

In this way, 100M data tables are generated daily, compressed to more than 7M

Method Two

Shell scripts for automatically backing up databases under Linux
The program on the Linux server updates the MySQL database every day, and then remembers to write a shell script, combined with crontab, the number of scheduled backups

According to the library. Actually very simple, the main is to use the mysqldump command with MySQL.

The script reads as follows:

The code is as follows Copy Code

#!/bin/sh
# File:/home/mysql/backup.sh
# Database Info
Db_name= "Test"
Db_user= "username"
db_pass= "Password"

# others VARs
Bin_dir= "/usr/local/mysql/bin"
Bck_dir= "/home/mysql/backup"
Date= ' Date +%f '

# TODO
$BIN _dir/mysqldump--opt-u$db_user-p$db_pass $DB _name | gzip > $BCK _dir/db_$date.gz


Then use this script to add to the/etc/crontab timed task:

The code is as follows Copy Code

5 * * 0 mysql/home/mysql/backup.sh

OK, every Sunday 5:01 the system will automatically run backup.sh file backup MySQL database.

The code is as follows Copy Code

/home/www/inc/back

Automatic backup of MySQL under Windows Server

Let's first save the following code as a Bakmysql.bat file and then save it. The location is free to go.

The code is as follows Copy Code

net stop MySQL

xcopy C:/mysql/data/bbs/*.* d:/db_bak/bbs/%date:~0,3%//y

net start MySQL

Then we use the Windows Planning task to manipulate

1. Start-Program-Accessories-System Tools-planning tasks

2, add the scheduled task.

3, the final step is to specify the backup time.
This is the time to complete the automatic scheduled backup, very simple.

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.