Back up MySQL database using shell

Source: Internet
Author: User

Although this script is relatively simple, I think it will help us a lot!
To put it simply:
Db_user = "user" is the user name.
Db_pass = "password" is the password

Mkdir/home/mysqlbak/$ date specifies and creates the directory to be backed up
Bin_dir = "/usr/local/MySQL/bin" specifies the location of your MySQL
Bck_dir = "/home/mysqlbak/$ date" Back up your database to a folder in the date directory.

$ Bin_dir/mysqldump-opt-U $ db_user-p $ db_pass discuz> $ bck_dir/discuz. SQL
This means you can add the database you want to back up the database, and add a database. "discuz" is the name of the database to be backed up, "discuz. SQL is the data file name backed up.
The script is as follows:

#! /Bin/sh <br/> # file:/home/MySQL/backup. sh <br/> # Database info bakupmysql <br/> db_user = "user" <br/> db_pass = "password" <br/> # Others vars <br/> date = 'date + % Y-% m-% d' <br/> mkdir/home/mysqlbak/$ date <br/> bin_dir = "/usr/local/MySQL/bin" <br/> bck_dir = "/home/mysqlbak/$ date" <br/> # todo <br/> $ bin_dir/mysqldump -- opt-U $ db_user-p $ db_pass discuz> $ bck_dir/discuz. SQL <br/> $ bin_dir/mysqldump -- opt-U $ db_user-p $ db_pass zjblog> $ bck_dir/zjblog. SQL $ bin_dir/mysqldump -- opt-U $ db_user-p $ db_pass openads> $ bck_dir/openads. SQL <br/>

 

 

 

 

 

 


Linux

System

MySQL

Automatic backup script

 

MySQL
The database stores the core and most valuable data of the website. If data is damaged or lost due to unpredictable reasons, the attack on a website will be devastating, the lessons learned once and again remind us that we must back up data, but manual backup is really troublesome and requires manual operations every day. If there are many sites, it would be a torment, however, we can write scripts to allow the system to automatically back up data, saving us effort. Several automatic backups are collected below
MySQL
The database script is usually enough for the station, but note: if the database is too large or the system is improperly set, automatic backup may fail. Therefore, even if automatic backup is used, remember to check whether the backup is successful and whether the backup data is complete and available.



You only need to save the following script content
Mysqlbackup. Sh
And add
Crontab
You can. For example
SSH
Input:

Crontab-e

Input:

00 00 ***/home/website/mysqlbackup. Sh

That is, daily
00: 00
Automatically run the backup script.


1
, Automatic Export
MySQL
Backup and upload to the specified
FTP

#! /Bin/bash

# Ftp
Set

Host = ftp_ip

Username = ftp_user

Passwd = ftp_passwd

#
Backup
MySQL
And export to a file

Mysqldump-u
User
-P
Password
Database Name
> $ (Date + % Y % m % d )-
Database Name
. SQL

#
Compressing backup files

Tar-zcvf $ (date + % Y % m % d) MySQL
Database Name
.Tar.gz $ (date + % Y % m % d )*

#
Upload
FTP

Echo "Open $ host


User $ username $ passwd


Bin


CD/hzg/


Prompt off


Mput $ (Date
+ % Y % m % d) MySQL
Database Name
.Tar.gz


Printf "/N"


Close


Bye "| FTP-I-n

#
Delete old backup files

Rm-RF $ (date + % Y % m % d )-
Database Name
. SQL $ (date + % Y % m % d) MySQL
Database Name
.Tar.gz

# Rm-RF $ (date + % Y % m % d )*

Rm-RF $ (date + % Y % m % d )*

 

2
Automatic Backup
MySQL
Database and send it to the specified
Email

#
Back up and export
MySQL
Database to file

Mysqldump-uroot-ppassword -- databases db1 DB2 db3>
/Home/website/backups/databackup. SQL

#
Compressing backup files

Tar zcf/home/website/backups/databackup. SQL .tar.gz
/Home/website/backups/

#
Send to specified mailbox

Echo"
Topic
:
Database Backup
"|
Mutt-A/home/website/backups/mysqlbackup. SQL .tar.gz-s"
Content
:
Database Backup
Www@gmail.com

#
Delete old backup files

Rm-r/home/website/backups /*

 

Set
-Ppassword
In
Password
For your own
Root
Password, Set
Db1 DB2 db3
Replace it with the name of the database you want to back up. Replace the path and email address.

You can also use the following command to export
MySQL
Back up as a compressed file:

Mysqldump-u
User
-P
Password
Database Name
| Gzip-C >$ (date + % Y % m % d )-
Database Name
. SQL.

 

 

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.