iT邦幫忙

0

maria db 備份錯誤

GJ 2019-01-28 19:35:061940 瀏覽
  • 分享至 

  • twitterImage

在GOOGLE 找了一個.sh來排程備份

跑的時候錯在這行

get all databases

all_db="$($MYSQL -u $db_user -h $db_host -p$db_passwd -Bse 'show databases')"

錯誤訊息:line 41: -u: command not found

請問如和修正才對?

程式碼

#!/bin/sh
# mysql_backup.sh: backup mysql databases and keep newest 5 days backup.
#
# Last updated: 20 March 2006
# ----------------------------------------------------------------------
# This is a free shell script under GNU GPL version 2.0 or above
# Copyright (C) 2006 Sam Tang
# Feedback/comment/suggestions : http://www.real-blog.com/
# ----------------------------------------------------------------------

# your mysql login information
# db_user is mysql username
# db_passwd is mysql password
# db_host is mysql host
# -----------------------------
db_user="root"
db_passwd="11111111"
db_host="192.168.xxxxx"

# the directory for story your backup file.
backup_dir="/volume1/backup"

# date format for backup file (dd-mm-yyyy)
time="$(date +"%d-%m-%Y")"

# mysql, mysqldump and some other bin's path
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
MKDIR="$(which mkdir)"
RM="$(which rm)"
MV="$(which mv)"
GZIP="$(which gzip)"

# check the directory for store backup is writeable
test ! -w $backup_dir && echo "Error: $backup_dir is un-writeable." && exit 0

# the directory for story the newest backup
test ! -d "$backup_dir/backup.0/" && $MKDIR "$backup_dir/backup.0/"

# get all databases
all_db="$($MYSQL -u $db_user -h $db_host -p$db_passwd -Bse 'show databases')"

for db in $all_db
do
	$MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db | $GZIP -9 > "$backup_dir/backup.0/$time.$db.gz"
done

# delete the oldest backup
test -d "$backup_dir/backup.5/" && $RM -rf "$backup_dir/backup.5"

# rotate backup directory
for int in 4 3 2 1 0
do
	if(test -d "$backup_dir"/backup."$int")
	then
		next_int=`expr $int + 1`
		$MV "$backup_dir"/backup."$int" "$backup_dir"/backup."$next_int"
	fi
done

exit 0;
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

2
Ray
iT邦大神 1 級 ‧ 2019-01-28 21:00:39
最佳解答

看起來似乎是你的:

MYSQL="$(which mysql)"

這個結果得到是空值, 所以代入下面這段碼:

all_db="$($MYSQL -u $db_user -h $db_host -p$db_passwd -Bse 'show databases')"

會變成前面的 $MYSQL 是 NULL, 直接就從 -u 開始, shell 以為 -u 是指令, 跑去執行 -u, 結果找不到這個指令, 然後就跳你的錯誤訊息了..

(偷偷問一下: 我知道 MYSQL 有 -B 的參數; 但是後面的 se 這兩個又是幹嘛的?)

neo2124 iT邦新手 2 級 ‧ 2019-01-30 11:56:31 檢舉

查了下是這個
-s, --silent Be more silent. Print results with a tab as separator,
each row on new line.
-e, --execute=name Execute command and quit. (Disables --force and history
file.)

加入 -s 把欄位名稱Database 去掉,只留結果
加入 -e 直接執行後面語句(這個我比較常用)

GJ iT邦好手 1 級 ‧ 2019-02-25 08:40:13 檢舉

後來仔細研究GOOGLE到
在root帳號下找不到mysql 指令
mariadb 的指令需要先ln -s 到安裝路徑下的BIN資料夾
裡面才有MYSQL相關指令的lib
因為我是在nas上這個套件,所以不知道是不是跟一般linux會不一樣

我要發表回答

立即登入回答