windows数据自动压缩为tar.gz文件,支持linux,linux 拷贝windows主机上的文件

本文介绍了一个用于Windows环境下数据压缩打包的过程,并详细展示了如何通过批处理脚本实现文件压缩、移动及MD5校验。同时,在Linux环境下通过脚本实现了文件的下载、MD5校验以及文件完整性的验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

windows2003下数据压缩打压缩包

@echo off

set day=%date:~8,2%
set month=%date:~5,2%
set year=%date:~0,4%
set yyyymmdd=%year%%month%%day%

set dataDir=F:/data/
set dataDisk=F:
set datafiles=*.dat

set sshDir=C:/Program Files/OpenSSH/home/Administrator/work
set sshDisk=C:

set stockMarket=ShenZhen
set tarPackagefile=%stockMarket%-%yyyymmdd%.tar
set dataInfoListfile=%stockMarket%HostDataInfoList-%yyyymmdd%.txt
set gzipPackagefile=%tarPackagefile%.gz
set md5file=%stockMarket%Checkmd5-%yyyymmdd%.windows

echo %packagefile%
echo %dataInfoListfile%
echo %gzipPackagefile%
echo %md5file%


@echo 检查目录是否存在
if exist "%dataDir%" (@echo %dataDir% 目录存在) else (@echo %dataDir% 目录不存在)
if not exist "%dataDir%" goto quit

if exist "%sshDir%" (@echo %sshDir% 目录存在) else (@echo %sshDir% 目录不存在)
if not exist "%sshDir%" goto quit

@echo 进入当前目录:cd %dataDir%
cd\
%dataDisk%
cd %dataDir%
if exist %tarPackagefile% (@echo 删除文件:"%gtarPackagefile%")
if exist %tarPackagefile% del %tarPackagefile%

if exist %gzipPackagefile% (@echo 删除文件:"%gzipPackagefile%")
if exist %gzipPackagefile% del %gzipPackagefile%

@echo 进入当前目录:cd %sshDir%
cd\
%sshDisk%
cd %sshDir%

if exist %dataInfoListfile% (@echo 删除文件:"%dataInfoListfile%")
if exist %dataInfoListfile% del %dataInfoListfile%

if exist %md5file% (@echo*删除文件:"%md5file%")
if exist %md5file% del %md5file%

if exist %gzipPackagefile% (@echo 删除文件:"%gzipPackagefile%")
if exist %gzipPackagefile% del %gzipPackagefile%

@echo 进入当前目录:cd %dataDir%
cd\
%dataDisk%
cd %dataDir%

@echo 获取目录%dataDir%下文件名和大小并存入文件 "%sshDir%/%dataInfoListfile%"
for /f "delims=" %%a in ('dir /b/a-d/oN *.*') do echo %%a %%~za >> "%sshDir%/%dataInfoListfile%"

@echo 进入当前目录:cd %sshDir%
cd\
%dataDisk%
cd %dataDir%

@echo 归档目录%dataDir%下原始文件为 "%tarPackagefile%" ......
tar -cvf %tarPackagefile% %datafiles%
@echo;

@echo 正在压缩 "%tarPackagefile%" 为: %gzipPackagefile% ......
if exist %tarPackagefile% (gzip "%tarPackagefile%")

@echo 正在移动文件%gzipPackagefile% ,从%dataDir%到%sshDir%
if exist %gzipPackagefile% (move "%gzipPackagefile%" "%sshDir%")

@echo 进入当前目录:cd %sshDir%
@echo 检查文件是否创建成功......

cd\
%sshDisk%
cd %sshDir%

if exist "%sshDir%/%dataInfoListfile%" (@echo 成功创建 "%dataInfoListfile%" ) else (@echo 创建 "%dataInfoListfile%" 失败)
if exist "%sshDir%/%gzipPackagefile%" (@echo 成功创建 "%gzipPackagefile%") else (@echo 创建 "%gzipPackagefile%" 失败)
if exist "%sshDir%/%gzipPackagefile%" (md5sum -b %gzipPackagefile% >> %md5file%) else (@echo 创建 "%md5file%" 失败)
if exist "%sshDir%/%md5file%" (@echo 成功创建 "%md5file%") else (@echo 创建 "%md5file%" 失败)
@echo;
@echo;
@echo;

:quit
exit



linux拷贝windows主机上的文件

#主机IP
hostIp='192.168.1.254'

#主机端口
port='22'

#网络传输带宽 例如:1000/8=125kB/s 8000/8=1MB/S 10000/8=1.25MB/s
networkBandwidth=10000

#主机用户登录名
user='admin'

#主机本地存放的数据目录
hostLocalDir='/home/admin/work'

#远程目录
hostdir=$user'@'$hostIp':'$hostLocalDir


#本地存储目录
localdir='/home/cyp/Desktop'


#定义日志输出类型 0:OK message Type,1:Error Message type,2:Label Message Type
okType=0
errorType=1
sysType=2


#系统提示信息
shellStart='**********************************ShellStart**********************************'
shellEnd='**********************************ShellEnd**********************************'

#日志打印函数
printlogMsg()
{
#体统时间
yyyymmddhhmmss=$(date +"%Y-%m-%d %H:%M:%S")
echo ''$1','$2'',$yyyymmddhhmmss >> $localdir/$resultLogfile
echo ''$1','$2'',$yyyymmddhhmmss >> $localdir/$historyResultLogfile
}

#获取系统时间,格式为YYYYMMDD
yyyymmdd=$(date +%Y%m%d)

#定义原始文件压缩文件名
stockMarket='ShenZhen'

#原始文件压缩包文件名
packagefile=$stockMarket-$yyyymmdd'.tar.gz'

#原始文件压缩包md5验证windows编码格式文件
md5WindowsFormatfile=$stockMarket'Checkmd5-'$yyyymmdd'.windows'

#原始文件压缩包md5验证unix编码格式文件
md5UnixFormatfile=$stockMarket'Checkmd5-'$yyyymmdd'.unix'

#原始文件压缩包md5验证unix编码格式临时文件
tempCheckMd5Resultfile=$stockMarket'TempCheckmd5-'$yyyymmdd'.txt'

#存储原始文件压缩包信息列表临时文件
tempDataInfoListfile=$stockMarket'TempDataInfoList-'$yyyymmdd'.txt'

#存储原始文件压缩包信息列表文件
clientDataInfoListfile=$stockMarket'ClientDataInfoList-'$yyyymmdd'.txt'

#存储原始文件信息列表文件
hostDataInfoListfile=$stockMarket'HostDataInfoList-'$yyyymmdd'.txt'

#最新脚本运行日志记录
resultLogfile=$stockMarket'CheckResult-'$yyyymmdd'.log'

#历史脚本运行日志记录,存了当天所有的运行日志
historyResultLogfile=$stockMarket'HistoryCheckResult-'$yyyymmdd'.log'

printlogMsg $sysType $shellStart
#-------------------------------------------------------------------------
#判断本地硬盘Unix编码格式文件
if [ -f "$localdir/$md5UnixFormatfile" ]; then
echo 'deleting file:' '"'$md5UnixFormatfile'"'
rm $localdir/$md5UnixFormatfile
fi

#判断本地硬盘删除文件
if [ -f "$localdir/$clientDataInfoListfile" ]; then
echo 'deleting file:' '"'$clientDataInfoListfile'"'
rm $localdir/$clientDataInfoListfile
fi

#判断本地硬盘删除文件
if [ -f "$localdir/$hostdataInfoListfile" ]; then
echo 'deleting file:' '"'$hostdataInfoListfile'"'
rm $localdir/$hostdataInfoListfile
fi


#判断本地硬盘删除文件
if [ -f "$localdir/$resultLogfile" ]; then
echo 'deleting file:' '"'$resultLogfile'"'
rm $localdir/$resultLogfile
fi

#判断主机IP是否ping通,并输出结果写入日志文件
ping -c 1 $hostIp
if [ $? -eq 0 ]
then
printlogMsg $sysType 'HOST "'$hostIp'" is online'
else
printlogMsg $sysType 'HOST "'$hostIp'" is offline'
quit
fi


#复制原始文件压缩包,并输出结果写入日志文件
if scp -l $networkBandwidth -P $port -r $hostdir/$packagefile $localdir
then
printlogMsg $okType 'Succeed to copy file: "'$packagefile'" from Host:'$hostIp' Port:'$port
else
printlogMsg $errorType 'Failed to copy file: "'$packagefile'" from Host:'$hostIp' Port:'$port
fi

#复制原始文件压缩包md5验证文件,并输出结果写入日志文件
if scp -l $networkBandwidth -P $port -r $hostdir/$md5WindowsFormatfile $localdir
then
printlogMsg $okType 'Succeed to copy file: "'$md5WindowsFormatfile'" from Host:'$hostIp' Port:'$port
else
printlogMsg $errorType 'Failed to copy file: "'$md5WindowsFormatfile'" from Host:'$hostIp' Port:'$port
fi

#拷贝原始文件信息列表文件,并输出结果写入日志文件
if scp -l $networkBandwidth -P $port -r $hostdir/$hostDataInfoListfile $localdir
then
printlogMsg $okType 'Succeed to copy file: "'$hostDataInfoListfile'" from Host:'$hostIp' Port:'$port
else
printlogMsg $errorType 'Failed to copy file: "'$hostDataInfoListfile'" from Host:'$hostIp' Port:'$port
fi

#转化windows 编码格式成Unix编码
sed -e 's/.$//' $localdir/$md5WindowsFormatfile >> $localdir/$md5UnixFormatfile

#测试远程去的文件,md5sum 验证完整性
cd $localdir
md5sum -c $md5UnixFormatfile >> $tempCheckMd5Resultfile

#读取Md5验证后的结果文件,并判断结果写入日志文件
while read md5line
do

checkMd5fileName=`echo $md5line | awk '{print $1}'`
checkedMd5Status=`echo $md5line | awk '{print $2}'`

if [ $checkedMd5Status = "FAILED" ]; then
printlogMsg $errorType $checkMd5fileName'md5 checking failed'
elif [ $checkedMd5Status = "OK" ]; then
printlogMsg $okType $checkMd5fileName'md5 checking OK'
else
printlogMsg $errorType $line
fi
done < $localdir/$tempCheckMd5Resultfile

#获取解压文件存储文件信息列表,输出到指定临时文件当中
tar -tvf $localdir/$packagefile >> $localdir/$tempDataInfoListfile

#解析临时文件,取文件名和文件大小信息,并保存到本地指定文件当中,此文件为本地文件信息列表文件,数据最终来源压缩包里的信息
while read line
do
filePath=`echo $line | awk '{print $6}'`
fileName=`echo $filePath |awk -F\/ '{print $NF}'`
fileSize=`echo $line | awk '{print $3}'`
echo $fileName $fileSize >> $localdir/$clientDataInfoListfile

done < $localdir/$tempDataInfoListfile


#主机信息文件与本地获取的文件信息进行比对,比较文件名和文件大小,并输出文件验证结果写入日志文件
while read line
do
hostfileName=`echo $line | awk '{print $1}'`
hostfileSize=`echo $line | awk '{print $2}'`
condition='\<'$hostfileName'\>'
count=`grep -c $condition $clientDataInfoListfile`
if [ $count -eq 1 ]; then
clientIndexLine=`grep $condition $clientDataInfoListfile`
clientfileSize=`echo $clientIndexLine | awk '{print $2}'`
if [ $hostfileSize -ne $clientfileSize ]; then
printlogMsg $errorType 'The size of "'$hostfileName'" is not correct.'
else
printlogMsg $okType 'The size of "'$hostfileName'" is correct.'
fi
else
printlogMsg $errorType 'The file "'$hostfileName'" is lost.'
fi
done < $localdir/$hostDataInfoListfile


#删除临时本地硬盘windows编码格式文件
if [ -f "$localdir/$md5WindowsFormatfile" ]; then
echo 'deleting file:' '"'$md5WindowsFormatfile'"'
rm $localdir/$md5WindowsFormatfile
fi

#删除临时本地硬盘windows编码格式文件
if [ -f "$localdir/$tempDataInfoListfile" ]; then
echo 'deleting file:' '"'$tempDataInfoListfile'"'
rm $localdir/$tempDataInfoListfile
fi


#删除原始文件压缩包md5验证unix编码格式临时文件
if [ -f "$localdir/$tempCheckMd5Resultfile" ]; then
echo 'deleting file:' '"'$tempCheckMd5Resultfile'"'
rm $localdir/$tempCheckMd5Resultfile
fi

#删除存储原始文件信息列表文件
if [ -f "$localdir/$hostDataInfoListfile" ]; then
echo 'deleting file:' '"'$hostDataInfoListfile'"'
rm $localdir/$hostDataInfoListfile
fi

#删除存储原始文件压缩包信息列表文件
if [ -f "$localdir/$clientDataInfoListfile" ]; then
echo 'deleting file:' '"'$clientDataInfoListfile'"'
rm $localdir/$clientDataInfoListfile
fi

printlogMsg $sysType $shellEnd

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值