文章目录
#!/bin/bash
handle_error(){
echo "command:execute dataBackup.sh occur error"
exit 1
}
trap 'handle_error' ERR
removeOldZipFiles(){
basePath=$1
cleanDate=$2
echo "clean base path is $basePath"
for file in ${basePath}*_backupdata.zip; do
fileName=$(echo $(basename $file) | cut -c 1-8)
echo $fileName
if [ $fileName -lt $cleanDate ];then
rm -r $file
echo "del file $file"
fi
done
echo "delete old zip files successfully"
}
date=$1
saveDays=$2
env=$4
profile=$3
rpath="hdfs://testdomain/tmp/$profile/TEST_PATH/$date"
lpath="/root/project/spark/$env/TEST_PATH"
vpath="/STASH/TEST_PATH/$env/"
cleandate=$(date -d " -$[saveDays-1] days" '+%Y%m%d')
echo "date: $date,"
echo "cleandate : $cleandate,"
echo "env: $env,"
echo "profile: $profile,"
echo "remote path: $rpath,"
echo "local path: $lpath,"
echo "VMfolder path: $vpath"
container_id=`docker ps -a|grep workspace|grep -v grep|head -n 1|awk '{print $1}'`
echo $container_id
if [[ $container_id != "" ]];then
if [[ $date != "" && $cleandate != "" && $env != "" && $profile != "" ]];then
docker exec ${container_id} hdfs dfs -copyToLocal ${rpath} ${lpath}
cp -R /TEST/projects/spark/${env}/TEST_PATH/${date} ${vpath}
zip -r ${vpath}${date}_backupdata.zip ${vpath}${date}
rm -r ${vpath}${date}
removeOldZipFiles $vpath $cleandate
echo "finished"
else
echo "Invalid parameter found!"
fi
else
echo "Not found docker container!"
fi