使用指令碼自動化遠程備份MySQL資料庫

來源:互聯網
上載者:User

通常情況下、mysql都需要備份,備份的方法有很多種。下面是我用指令碼配合計劃任務完成的自動備份遠端資料庫。

一、 確認備份方案:

備份機:ip192.168.8.51

資料庫伺服器:ip192.168.8.46

備份的內容:對mysql的studydb、coursedb庫進行遠程備份、每天2:30執行,每個庫備份為獨立的sql檔案。然後壓縮為tar.gz格式,檔案中嵌入備份的日期和時刻。

1. 建立資料庫:

[root@ns ~]# service mysqld status

mysqld (pid 3554) 正在運行...

[root@ns ~]# mysqladmin -uroot -p password 123123 ##更改密碼

Enter password: ##輸入原密碼

[root@ns ~]# mysql -uroot -p123123

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 8

Server version: 5.0.77-log Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

mysql> create database studydb; ##建立資料庫

Query OK, 1 row affected (0.05 sec)

mysql> create database couredb;

Query OK, 1 row affected (0.00 sec)

2. 給資料庫賦權:

mysql> grant select,lock tables on studydb.* to 'operator'@'192.168.8.51' identified by 'pwd123'; ##使用邏輯工具備份工具mysqldump是需要對庫有select和lock tables的許可權

Query OK, 0 rows affected (0.00 sec)

mysql> grant select,lock tables on coursedb.* to 'operator'@'192.168.8.51' identified by 'pwd123';

Query OK, 0 rows affected (0.00 sec)

3. 在192.168.8.51測試備份:

[root@localhost ~]# cd /tmp

[root@localhost tmp]# mysqldump -u operator -ppwd123 -h 192.168.8.46 --databases studydb >test.sql

[root@localhost tmp]# ls

bin games test.sql

二、 在192.168.8.51編寫Mysql備份指令碼:

[root@localhost ~]# mkdir /root/bin

[root@localhost ~]# cd /root/bin

[root@localhost bin]# vi dbbak.sh

#!/bin/bash

#這是一個簡單的mysql資料庫邏輯備份指令碼

#1.定義資料庫連接、目標庫資訊

MY_USER="operator"

MY_PASS="pwd123"

MY_HOST="192.168.8.46"

MY_CONN="-u $MY_USER -p$MY_PASS -h $MY_HOST"

MY_DB1="studydb"

MY_DB2="coursedb"

#2.定義備份目錄、工具、時間、檔案名稱主體

BF_DIR="/opt/dbbak/"

BF_CMD="/usr/local/bin/mysqldump"

BF_TIME=`date +%Y%m%d-%H%M`

NAME_1="$MY_DB1-$BF_TIME"

NAME_2="$MY_DB2-$BF_TIME"

#3.先匯出為.sql指令碼,然後再進行壓縮(打包後刪除原檔案)

cd $BF_DIR/

/bin/tar zcf $NAME_1.tar.gz $NAME_1.sql --remove &>/dev/null

/bin/tar zcf $NAME_2.tar.gz $NAME_2.sql --remove &>/dev/null

[root@localhost bin]# mkdir /opt/dbbak ###建立備份目錄

[root@localhost bin]# chmod a+x dbbak.sh ###給指令碼執行許可權

[root@localhost bin]# dbbak.sh ##運行指令碼

[root@localhost bin]# ls /opt/dbbak/ ###驗證效果

coursedb-20130505-0311.tar.gz studydb-20130505-0311.tar.gz

三、 在192.168.8.51上設定計劃任務:

[root@localhost bin]# crontab -e

30 2 * * * /root/bin/dbbak.sh

[root@localhost bin]# chkconfig crond on

[root@localhost bin]# service crond status

crond (pid 3263) 正在運行...

好了Database Backup這件事就搞定了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.