源系统:
- 操作系统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