平常報表時中經常需要自動備份mysql資料庫中的各種資料表,來簡單的說一下如何在shell指令碼下連線mysql資料庫並備份mysql資料表。
注意:eof中只能使用mysql語句。#!/bin/bash
host=127.0.0.1 #ip (127.0.0.1表示本機位址)
user=lgl #資料庫使用者名稱
password=123 #資料庫密碼
database=backup #資料庫名
backup_path=/home/lgl/bkdata #備份目錄
logfile=/home/lgl/bkdata/data.log #記錄日誌
#連線mysql資料庫
mysql -h $host -u $user -p$password -s $database <>$logfile
exit 4
fiecho "init data successful!">>$logfile
#如果不存在備份目錄就建立
if [ ! -d $backup_path ]
then
mkdir $backup_pathfi
#進入備份目錄
cd $backup_path
#匯出資料表test到test.sql中
mysqldump -h $host -u $user -p$password $database test > test.sql
if [ $? = 0 ]
then
echo "test backup successful!">>$logfile
else
echo "test backup fail!">>$logfile
exit 4
fi
shell指令碼中利用spool命令匯出資料
shell指令碼中利用spool命令,將資料匯出到檔案中 廢話不多說直接上 sqlplus s dev null set heading off set feedback off set pagesize 0 setecho off set verify off set termout off se...
mysql匯出sql指令碼
注意事項 必須在d ltsoft mysql4 bin mysqldump u root p test c a.sql 先得進入mysql安裝目錄的bin下 可以看到裡面有mysqldump.exe 通常mysql預設安裝在c program files mysql mysql server 5.0...
git日誌 每天匯出 shell指令碼
bin bash 用途 這個指令碼是用來每天匯出git的日誌用的 history 2015.01.25 by kaifeng gameclient path d yr xcq gameclient txt path d phpstudy www xlegend client log date y m...