shell編程系列25--shell操做數據庫實戰之備份MySQL數據,並經過FTP將其傳輸到遠端主機

shell編程系列25--shell操做數據庫實戰之備份MySQL數據,並經過FTP將其傳輸到遠端主機


備份mysql中的庫或者表

mysqldump
經常使用參數詳解:
-u    用戶名
-p    密碼
-h    服務器ip地址
-d    等價於--no-data    只導出表結構
-t    等價於--no-create-info    只導出數據,不導出建表語句
-A    等價於--all-databases
-B    等價於--databases    導出一個或多個數據庫

需求:將school中的score表備份,而且將備份數據經過ftp傳輸到 10.11.0.148(windows) 的ftp的 bak 目錄下


FTP經常使用指令:
    open    與FTP服務器創建鏈接,例子:open 10.11.0.148
    user    有權限登陸FTP服務器的用戶名和密碼,例子:user ftpuser ftpuser


# 備份school庫
[root@localhost shell]# mysqldump -udbuser -p123456 -h 10.11.0.215 school >school.sql
# 備份school庫下的score表
[root@localhost shell]# mysqldump -udbuser -p123456 -h 10.11.0.215 school score >score.sql
# -A備份全部數據庫,全部有權限的數據庫
# mysqldump -udbuser -p123456 -h 10.11.0.215 -A >score.sql


[root@localhost shell]# sh auto_backup.sh aaa
Connected to 10.11.0.148 (10.11.0.148).
220-FileZilla Server 0.9.60 beta
220-written by Tim Kosse (tim.kosse@filezilla-project.org)
220 Please visit https://filezilla-project.org/
Remote system type is UNIX.
331 Password required for ftpuser
230 Logged on
250 CWD successful. "/bak" is current directory.
local: aaa remote: aaa
227 Entering Passive Mode (10,11,0,148,220,39)
150 Opening data channel for file upload to server of "/bak/aaa"
226 Successfully transferred "/bak/aaa"
5 bytes sent in 9.4e-05 secs (53.19 Kbytes/sec)
221 Goodbye
[root@localhost shell]# cat auto_backup.sh 
#!/bin/bash
#

db_user="dbuser"
db_password="123456"
db_host="10.11.0.215"

ftp_user="ftpuser"
ftp_password="ftpuser"
ftp_host="10.11.0.148"

src_dir="/data01/bak"
dst_dir="/bak"

function auto_ftp
{
    ftp -niv << EOF
        open $ftp_host
        user $ftp_user $ftp_password

        cd $dst_dir
        put $1
        bye
EOF
}

auto_ftp aaa


# 自動備份數據庫並傳輸備份文件到ftp的腳本

[root@localhost shell]# cat auto_backup.sh 
#!/bin/bash
#

# 須要備份的數據庫的帳號信息
db_user="dbuser"
db_password="123456"
db_host="10.11.0.215"

# 遠端ftp服務器的信息
ftp_user="ftpuser"
ftp_password="ftpuser"
ftp_host="10.11.0.148"

# 源目錄,目標目錄
src_dir="/data01/bak"
dst_dir="/bak/"
time_date="`date +%Y%m%d%H%M%S`"
# 備份文件名,根據當前的年月日時分秒 做爲文件名
file_name="school_score_${time_date}.sql"

function auto_ftp
{
    ftp -niv << EOF
        open $ftp_host
        user $ftp_user $ftp_password

        cd $dst_dir
        put $1
        bye
EOF
}
#chown -R ftpuser.ftpuser /data01
#auto_ftp "/data01/bak/school_score_20190612181401.sql"
# 備份數據
mysqldump -u"$db_user" -p"$db_password" -h"$db_host" school score > $file_name && auto_ftp $file_name

# 執行腳本
[root@localhost shell]# sh auto_backup.sh 
Connected to 10.11.0.148 (10.11.0.148).
220-FileZilla Server 0.9.60 beta
220-written by Tim Kosse (tim.kosse@filezilla-project.org)
220 Please visit https://filezilla-project.org/
Remote system type is UNIX.
331 Password required for ftpuser
230 Logged on
250 CWD successful. "/bak" is current directory.
local: school_score_20190612182616.sql remote: school_score_20190612182616.sql
227 Entering Passive Mode (10,11,0,148,197,15)
150 Opening data channel for file upload to server of "/bak/school_score_20190612182616.sql"
226 Successfully transferred "/bak/school_score_20190612182616.sql"
2349 bytes sent in 7.9e-05 secs (29734.18 Kbytes/sec)
221 Goodbye

搭建windows的ftp服務
 
  
相關文章
相關標籤/搜索