linux环境shell脚本 压缩 解压 安装 备份文件夹

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 21:01   3633   0

使用方法:

1. cd bin; 执行build.sh文件,会打包与bin目录同层级的目录py_common,生成打包文件py_common_install_V0.01.bsx

2. 执行bash py_common_install_V0.01.bsx 完成解压、安装和备份

具体脚本文件内容如下:

打包压缩文件:/bin/build.sh

#!/bin/bash

# 要打包的文件夹,py_common是需要打包的文件夹,与bin处于同一层级
app_name="py_common"

CUR_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
WORK_DIR="$( cd ${CUR_DIR}/.. && pwd )"
version=0.01
BIN_DIR="$WORK_DIR/bin"


# 打包后的安装文件
bsx_file="${CUR_DIR}/${app_name}_install_V${version}.bsx"
if [ -f $bsx_file ]; then
    rm -v ${bsx_file}
fi

TMPDIR=`mktemp -d /tmp/py_common-build.XXXXXX`
echo "tempdir : $TMPDIR"

cp -rp ${WORK_DIR}/${app_name} ${TMPDIR}/
cd $TMPDIR

tar --exclude='.git' --exclude='cscope*' --exclude="${app_name}.tar" \
    --exclude='*pyc' --exclude='log' \
    --exclude='release' \
    --exclude='*.bsx' --exclude='build.sh' -cf \
    $BIN_DIR/${app_name}.tar ./*
cd -

if [ -e ${BIN_DIR}/${app_name}.tar ]; then
    gzip ${BIN_DIR}/${app_name}.tar

    if [ -e ${BIN_DIR}/${app_name}.tar.gz ]; then
        cat $BIN_DIR/decompress.sh $BIN_DIR/${app_name}.tar.gz \
            > ${bsx_file}
    else
        echo "$BIN_DIR/${app_name}.tar.gz doesn't exist"
        exit 1
    fi
else
    echo "$BIN_DIR/${app_name}.tar doesn't exist"
    exit 1
fi


rm $BIN_DIR/${app_name}.tar.gz
rm -rf $TMPDIR
md5=$(md5sum ${bsx_file}|awk '{print $1}')
echo "the ${bsx_file} is created"
echo "md5sum is ${md5}"
exit 0

解压安装备份文件:/bin/decompress.sh

#!/bin/bash

echo ""
echo "Self Extracting Installer"
echo ""

export TMPDIR=`mktemp -d /tmp/selfextract.XXXXXX`

ARCHIVE=`awk '/^__ARCHIVE_BELOW__/ {print NR + 1; exit 0; }' $0`

tail -n+$ARCHIVE $0 | tar xz -C $TMPDIR

CDIR=`pwd`
cd $TMPDIR

app_name="py_common"

# 安装目录
install_path="/usr/local/my_test_project"
data_path="/data"

# 备份目录
bak_time=`date "+%Y%m%d%H%M%S"`
backup_dir="${data_path}/backup/${app_name}/${bak_time}"

rsync -av --backup --backup-dir=${backup_dir} * ${install_path}/


cd $CDIR
rm -rf $TMPDIR

exit 0

__ARCHIVE_BELOW__

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP