源系統:
- 作業系統7
- bash shell
- 壓縮
目的地系統:
- FTP伺服器
- CentOS 6.4
- bash shell
- 解壓縮
我編寫了一個腳本來歸檔文件並使用 shell 腳本將其發送到 FTP 伺服器
#!/bin/bash
# Declare no. of days
days=15
# Declare Source path of sql files and Destination path of backup directory
dumps=/home/applications/backup
bkpdir=/home/applications/backup/olddumps
# Find sql dumps of ets
files=($(find $dumps/*.sql -mtime +"$days"))
for file in ${files[*]}
do
# Move each file into backup dir which is 15 days old
echo "file is: $file\n";
mv $file $bkpdir
# Find the sql files and compress them
cd $bkpdir
filename=$(basename $file)
zip $bkpdir/$filename.zip $filename
# FTP Login
HOST=a.b.c.d
USER=xxxx
PASS=yyyyy
REM_DIR=/olddumps/sqlfiles
echo "Uploading file via FTP:"
ftp -in $HOST <<EOF
quote USER $USER
quote PASS $PASS
cd $REM_DIR
put $filename.zip
bye
EOF
# Remove sql files if any
rm $bkpdir/$filename
done
# Remove compressed files which are 6 months old
find $bkpdir/*.zip -type f -mtime +180 -exec rm {} \;
現在的問題是目標系統中的壓縮檔案無法使用unzip
命令提取並顯示以下錯誤:
檔案:emt_bus-08-09-16-03-29.sql.zip
caution: zipfile comment truncated
error [emt_bus-08-09-16-03-29.sql.zip]: missing 49666528 bytes in zipfile
(attempting to process anyway)
error [emt_bus-08-09-16-03-29.sql.zip]: start of central directory not found;
zipfile corrupt.
(please check that you have transferred or created the zipfile in the
appropriate BINARY mode and that you have compiled UnZip properly)
我曾經tar
存檔過,但沒有運氣。它不會在目標系統中提取檔案並顯示以下錯誤
gzip: stdin: invalid compressed data--format violated
emt_bus-08-09-16-03-29.sql
tar: Unexpected EOF in archive
tar: Unexpected EOF in archive
tar: Error is not recoverable: exiting now
如何解決這個問題?
答案1
您可能已經有錯誤訊息,只是在運行該腳本時沒有看到。
透過執行
script.sh > log.txt 2>&1將所有輸出擷取到日誌中
最後一部分將 stderr 重新導向到 stdout,在本例中為 log.txt
您可能會發現超時(您發送了多少文件,您的連接有多穩定),或者您認為等待下一個命令的命令實際上並沒有在腳本中等待。
答案2
您的腳本應該設定binary
(在quote
命令之後)。否則,它將對此腳本使用文字模式。
如果是互動的,我剛剛測試的 ftp 用戶端將採用二進位模式。但當它是非互動的時候就不行了。
這是我測試/並修復的:
#!/bin/sh
ftp -in invisible-island.net <<EOF
quote USER anonymous
quote PASS anonymous
binary
cd cproto
get cproto.tar.gz
bye
EOF