酷!學園
2010-09-03 13:09 *
歡迎光臨, 訪客. 請先 登入註冊一個帳號.
您忘了 啟用您的帳號嗎?

請輸入帳號, 密碼以及預計登入時間
新聞:
 
   首頁 | Study-Area | 鳥園 | 鳥哥的Linux私房菜   說明 搜尋 日曆 登入 註冊  
頁: [1]
  列印  
作者 主題: 請問大大,crontab為什麼執行的結果沒辦法跟直接手動執行的一樣??  (閱讀 1253 次)
0 會員 以及 1 訪客 正在閱讀本篇主題.
JenDick
懷疑的國中生
**
文章: 84


檢視個人資料
« 於: 2008-05-29 19:55 »

最近在寫 crontab 來做 備援的動作(主要是參考鳥哥所提及的方法)

環境為 ubuntu

程式如下 (daily_back)
#!/bin/sh
day=`date +%Y%m%d`
basedir="/home/backup/daily"

cd $basedir
mkdir -m 700 $day
cd $day

mysqldump -u root -pxxxx -c xoops > backup_file_xoops.sql 2>> "$basedir"/backup_file_xoops.error
mysqldump -u root -pxxxx --all-databases > backup_file_ALL.sql 2>> "$basedir"/backup_file_ALL.error

cp -r /var/lib/mysql/xoops ./ 2> /dev/null
tar -cvf xoops.tar xoops/ 2> /dev/null
gzip xoops.tar 2> /dev/null
rm -r xoops 2> /dev/null

啥啥啥啥啥啥啥啥啥啥啥啥啥啥
cd /home/backup
tar -cvf mysql."$day".tar daily/ 2>> tar.log
gzip mysql."$day".tar 2>> tar.log
啥啥啥啥啥啥啥啥啥啥啥啥啥啥

id="xxx"
pw='xxx'
cd $basedir
ftp -n xxx.xxx.xxx.xxx >> "$basedir"/backup.ftp.log 2>&1 <<EOC
user $id $pw
binary
cd /FWserver_backup/Daily
put mysql."$day".tar.gz
bye
EOC

sync;sync

ps.
1.此程式的執行權限為 755


crontab 裡如下
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user   command
17 *   * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6   * * *   root   test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6   * * 7   root   test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6   1 * *   root   test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#

#Backup(add by myself)
30 3 * * 0 root sh /home/backup/shell_script/weekly_back.sh
39 19 * * * root sh /home/backup/shell_script/daily_back.sh

問題如下
手動執行此程式,是很正常的執行,但讓crontab來執行時,卻會有問題,主要是他在做
tar -cvf mysql."$day".tar daily/ 2>> tar.log
gzip mysql."$day".tar 2>> tar.log

這段時,
手動所產生的大小是正常的大小,
但crontab來執行時,所產生的大小,是不正常的。
將不正常的檔案解開看過,裡面是 0kb的資料,也就是跟本沒有檔案打包壓縮 。

想請問各位大大,這是什麼問題造成的??
已記錄
sitediy
活潑的大學生
***
文章: 425



檢視個人資料
« 回覆文章 #1 於: 2008-05-29 21:53 »

改成這樣呢

cd /home/backup
tar -cvf mysql."$day".tar /home/backup/daily/ 2>> tar.log
gzip mysql."$day".tar 2>> tar.log
已記錄
JenDick
懷疑的國中生
**
文章: 84


檢視個人資料
« 回覆文章 #2 於: 2008-05-29 22:17 »

改成這樣呢

cd /home/backup
tar -cvf mysql."$day".tar /home/backup/daily/ 2>> tar.log
gzip mysql."$day".tar 2>> tar.log

感謝sitediy大大回覆

但還是一樣,產生的檔案的大小是不正常的??

想請問一下~~~手動執行和crontab來執行 所造成的不一樣,是不是哪裡沒設定好呢??
已記錄
sitediy
活潑的大學生
***
文章: 425



檢視個人資料
« 回覆文章 #3 於: 2008-05-29 22:36 »

你把tar 先重導向到file去看看會出現啥訊息

cd /home/backup
tar -cvf mysql."$day".tar daily/ >> tar.log
gzip mysql."$day".tar 2>> tar.log

要用cron跑

我比較好奇的是  為何你要多寫一行呢?

打包跟壓縮可用同一個指令完成,你卻分作兩行來寫
已記錄
JenDick
懷疑的國中生
**
文章: 84


檢視個人資料
« 回覆文章 #4 於: 2008-05-29 23:09 »

你把tar 先重導向到file去看看會出現啥訊息

cd /home/backup
tar -cvf mysql."$day".tar daily/ >> tar.log
gzip mysql."$day".tar 2>> tar.log

要用cron跑

我比較好奇的是  為何你要多寫一行呢?

打包跟壓縮可用同一個指令完成,你卻分作兩行來寫


是可以用成一段
tar -zcv mysql."$day".tar.gz daily/ >> tar.log

話說回來
真得感謝sitediy 的指點
有可能是我下錯指令了

cd /home/backup
tar -cvf mysql."$day".tar daily/ 2>> tar.log
gzip mysql."$day".tar 2>> tar.log

沒辦法執行出結果

但去掉錯誤訊息的導向,就可以正常執行了
如下
cd /home/backup
tar -cvf mysql."$day".tar daily/ >> tar.log
gzip mysql."$day".tar >> tar.log

是哪裡不同??糟糕,要來翻書、google解答一下了~~~~

by the way, Thx sitediy
已記錄
JenDick
懷疑的國中生
**
文章: 84


檢視個人資料
« 回覆文章 #5 於: 2008-05-30 12:52 »

有可能是我下錯指令了

cd /home/backup
tar -cvf mysql."$day".tar daily/ 2>> tar.log
gzip mysql."$day".tar 2>> tar.log

沒辦法執行出結果

但去掉錯誤訊息的導向,就可以正常執行了
如下
cd /home/backup
tar -cvf mysql."$day".tar daily/ >> tar.log
gzip mysql."$day".tar >> tar.log

是哪裡不同??糟糕,要來翻書、google解答一下了~~~~

by the way, Thx sitediy

想請問一個疑問
因為手動的執行,都不會有 以上的問題(加了錯誤導向),但放進
crontab的時候,卻會出現無法 打包壓縮至正常檔案的部份,這是
為什麼呢??

而且,手動做出來的檔案大小是 483KB,但crontab跑出來的是
239KB,少了一半的大小耶~~~~

是為什麼呢?? 啥
« 最後編輯時間: 2008-05-30 16:52 由 JenDick » 已記錄
JenDick
懷疑的國中生
**
文章: 84


檢視個人資料
« 回覆文章 #6 於: 2008-05-30 21:38 »

想請問一個疑問
因為手動的執行,都不會有 以上的問題(加了錯誤導向),但放進
crontab的時候,卻會出現無法 打包壓縮至正常檔案的部份,這是
為什麼呢??

而且,手動做出來的檔案大小是 483KB,但crontab跑出來的是
239KB,少了一半的大小耶~~~~

是為什麼呢?? 啥

請問各位大大
我去看了一下,發現以下這段有些疑問
================================================ 方法一、此產出的檔案為282KB
cp -r /var/lib/mysql/xoops ./
tar -cvf xoops.tar xoops/
gzip xoops.tar
rm -r xoops
================================================

================================================ 方法二、此產出的檔案為 427KB
cp -r /var/lib/mysql/xoops ./ >> "$basedir"/cpxoop.log
tar -cvf xoops.tar xoops/ >> "$basedir"/cpxoop.log
gzip xoops.tar >> "$basedir"/cpxoop.log
rm-r xoops
================================================
這樣下,檔案會差一倍之多??
奇怪的是
手動執行,不管是方法一,還是方法二,所產出的檔案都為 427KB
使用crontab來執行的話,一定要使用方法二才能產出正常的檔案
ps.正常的檔案為427KB,282KB的檔案看過了,少了很多沒有拷備過來??

請問大大,是差在哪嗎??
已記錄
頁: [1]
  列印  
 
前往:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
本頁花了 0.06 秒,以及 15 次的資料庫查詢。