MySQL local backup and dual-host backup scripts

Source: Internet
Author: User

First modify the script for necessary configuration, and then run it as the root user.

1. The first parameter is used for remote backup.
2. local Parameters for local backup
3. No parameter is required for remote backup. <Note 1>
Note: You need to add a user to the Mysql user on another host ..
Configuration:

# Define host and mysql password
REMOTE_HOST = "" # remote host name or IP address
REMOTE_PASSWORD = "" # root @ local host password in mysql of the remote host.
# Define database path
DB_PATH = "/var/lib/mysql" # path of the local mysql database
LOCAL_PASSWORD = "" # local mysql root User Password
DB_BACKUP_PATH = "/root/db_bk" # data storage path during local backup
NO_BACKUP_DB = "" specifies the names of databases in the local database that do not want to be backed up. Separate them with spaces.
Direct download

#! /Bin/sh
# Mysqldump_all.sh
# This scripts is used to backup MySQL database bettwen tow MySQL SERVER!
# Define host and mysql password
REMOTE_HOST = ""
REMOTE_PASSWORD = ""
# Define database path
DB_PATH = "/var/lib/mysql"
LOCAL_PASSWORD = ""
DB_BACKUP_PATH = "/root/db_bk"
# Define which database no back up, write db anme escap whit space!
# Example;
# NO_BACKUP_DB = "yns test ps_note"
# This said that if db name is "yns" or "test" or "ps_note", then not backup them!
NO_BACKUP_DB = ""
# Get the database name and process one by one
Echo "Start to Backup ...";
# Ls/var/lib/mysql>/tmp/mydb. all
If ["$1 "! = "Help"]; then
For db in 'ls $ DB_PATH '; do
If_backup = "1"
If [-d $ DB_PATH/$ db]; then
For noBackDB in $ NO_BACKUP_DB; do
If (test "$ db" = "$ noBackDB"); then
# Echo-n "...";
If_backup = "";
Fi
Done
If [$ if_backup]; then
Case "$1" in
First)
Echo-n "Crate db [$ db] on $ REMOTE_HOST first! "
Result = 'mysqladmin-h $ REMOTE_HOST -- password = $ REMOTE_PASSWORD create $ db'
If [! $ Result]; then
Echo "... Done! ";
Else
Echo "... Fialed! Check your configure plz! ";
Echo "Remote host said: $ result ";
Exit
Fi
;;
Local)
# Back up to localhost dir.
If [! -D $ DB_BACKUP_PATH]; then
Mkdir $ DB_BACKUP_PATH
Fi
Echo-n "[$ db]... Backup to local $ DB_BACKUP_PATH with gzip ";
If [-e "$ DB_BACKUP_PATH/mongodb.gz"]; then
Mv $ DB_BACKUP_PATH/mongodb.gz $ DB_BACKUP_PATH/mongodb.1.gz;
Fi
Mysqldump -- password = Walte # Ming $ db | gzip> $ DB_BACKUP_PATH/mongodb.gz
Echo "... [OK]"
;;
*)
# Do common Backup!
Echo-n "[$ db]... Do Backup! ====>>> $ REMOTE_HOST/$ db ";
Mysqldump -- add-drop-table $ db -- password = $ LOCAL_PASSWORD | mysql-h $ REMOTE_HOST -- password = $ REMOTE_PASSWORD $ db
Echo "... [OK]";
Esac
Else
Echo "[$ db]";
Echo "*** This database set to Not backup !... Operation CANCELED! ";
Fi
Else
Echo "[$ db]";
Echo "*** Warning! Not a DataBbase. Backup Canceled ***";
Fi
Done
Else
Echo "Usage: mysqldupm_all.sh [firs | local | help]";
Echo "Some question ask for href =" mailto: walteyh@sohu.com ";"> walteyh@sohu.com ";
Fi
# Ended all backup opretion!
Echo "";
Echo "Note: if there is come wrong message white MySQL, you should check yourconfigure first! ";
Exit


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.