Fetch the repository succeeded.
比如:静态文件目录发布传输,配置产物路径为 / ,可能会把 ./git 等无关的文件发布到目标路径。由于大项目 ./git 目录会逐步增大,对于改动频繁的项目可能影响发布效率
建议:可支持文件、文件夹过滤
1.支持全局配置统一的产物过滤器
2.支持按项目特性配置产物过滤器
是否可以借鉴使用 rsync 命令的 方式处理。
rsync -a --exclude-from=/home/www/exclude.txt /home/www/xr/ root@192.168.12.52:/home/www/xr.202311301500/
rsync_192_168_12_52.sh 脚本代码如下:
#!/bin/bash
# 定义脚本所在目录的路径
script_dir=$(cd "$(dirname "$0")" && pwd)
# 定义默认的exclude_file和include_file
default_username="root"
default_server="192.168.12.52"
default_sources=$(ls $script_dir)
default_destination="$script_dir/"
default_exclude_file="$script_dir/exclude.txt"
default_include_file="$script_dir/include.txt"
#################################################################
####脚本变量定义#################################################
#################################################################
# 移动脚本
move_script() {
read -p "请输入移动路径: " dist_dir
mv rsync_*.sh include.txt exclude.txt $dist_dir
}
# 定义主函数
rsync_files() {
read -p "请输入同步的文件(逗号分隔): " key
server="$default_server"
username="$default_username"
sources="$default_sources"
destination="$default_destination"
# 检查必要参数是否提供,否则使用默认值
if [[ -z "$server" ]]; then
server="$default_server"
fi
if [[ -z "$username" ]]; then
username="$default_username"
fi
# 检查目标目录是否为空,如果是,则设置为当前脚本所在的目录
if [[ -z "$sources" ]]; then
sources= $(ls $default_sources)
fi
# 检查目标目录是否为空,如果是,则设置为当前脚本所在的目录
if [[ -z "$destination" ]]; then
destination="$default_destination"
fi
if [[ -z "$exclude_file" ]]; then
exclude_file="$default_exclude_file"
fi
if [[ -z "$include_file" ]]; then
include_file="$default_include_file"
fi
# 输出信息
echo "---------------------------------------------"
echo "服务器:$server"
echo "用户名:$username"
#echo "所有资源:$sources"
echo "同步文件:$key"
echo "目标目录:$destination"
echo "使用 exclude_file 文件: $exclude_file"
echo "使用 include_file 文件: $include_file"
echo "开始同步至目录..."
echo "---------------------------------------------"
# 检查文件是否存在
if [[ ! -f "$exclude_file" ]]; then
echo "exclude_file 文件不存在: $exclude_file"
exit 1
fi
if [[ ! -f "$include_file" ]]; then
echo "include_file 文件不存在: $include_file"
exit 1
fi
for source in $sources; do
if [ "$source" != "$key" ]; then
# echo "当前匹配 $script_dir/$key, 当前文件: $source"
continue
fi
if [ "$source" == "exclude.txt" ]; then
continue
fi
if [ "$source" == "include.txt" ]; then
continue
fi
if [[ $source =~ rsync_.*.sh ]]; then
continue
fi
echo "当前同步资源文件: $script_dir/$source"
# 判断是否为文件
if [ -f "$script_dir/$source" ]; then
echo "文件:rsync -a --exclude-from=$exclude_file "$script_dir/$source" $username@$server:$destination$source"
# rsync -a --exclude-from="$exclude_file" \
# "$script_dir/$source" \
# "$username@$server:$destination$source"
fi
# 判断是否为目录
if [ -d "$script_dir/$source" ]; then
echo "目录:rsync -a --exclude-from=$exclude_file "$script_dir/$source/" $username@$server:$destination$source/"
# rsync -a --exclude-from="$exclude_file" \
# "$script_dir/$source/" \
# "$username@$server:$destination$source/"
fi
done
}
# 定义主函数
rsync_all_files() {
server="$default_server"
username="$default_username"
sources="$default_sources"
destination="$default_destination"
# 检查必要参数是否提供,否则使用默认值
if [[ -z "$server" ]]; then
server="$default_server"
fi
if [[ -z "$username" ]]; then
username="$default_username"
fi
# 检查目标目录是否为空,如果是,则设置为当前脚本所在的目录
if [[ -z "$sources" ]]; then
sources= $(ls $default_sources)
fi
# 检查目标目录是否为空,如果是,则设置为当前脚本所在的目录
if [[ -z "$destination" ]]; then
destination="$default_destination"
fi
if [[ -z "$exclude_file" ]]; then
exclude_file="$default_exclude_file"
fi
if [[ -z "$include_file" ]]; then
include_file="$default_include_file"
fi
# 输出信息
echo "---------------------------------------------"
echo "服务器:$server"
echo "用户名:$username"
# echo "所有资源:$sources"
echo "目标目录:$destination"
echo "使用 exclude_file 文件: $exclude_file"
echo "使用 include_file 文件: $include_file"
echo "开始同步至目录..."
echo "---------------------------------------------"
# 检查文件是否存在
if [[ ! -f "$exclude_file" ]]; then
echo "exclude 文件不存在: $exclude_file"
exit 1
fi
if [[ ! -f "$include_file" ]]; then
echo "include 文件不存在: $include_file"
exit 1
fi
for source in $sources; do
if [ "$source" == "exclude.txt" ]; then
continue
fi
if [ "$source" == "include.txt" ]; then
continue
fi
if [[ $source =~ rsync_.*.sh ]]; then
continue
fi
# echo "当前同步资源文件: $script_dir/$source"
# 判断是否为文件
if [ -f "$script_dir/$source" ]; then
echo "rsync -a --exclude-from=$exclude_file "$script_dir/$source" $username@$server:$destination$source"
# rsync -a --exclude-from="$exclude_file" \
# "$script_dir/$source" \
# "$username@$server:$destination$source"
fi
# 判断是否为目录
if [ -d "$script_dir/$source" ]; then
echo "rsync -a --exclude-from=$exclude_file "$script_dir/$source/" $username@$server:$destination$source/"
# rsync -a --exclude-from="$exclude_file" \
# --include-from="$include_file" \
# "$script_dir/$source/" \
# "$username@$server:$destination$source/"
fi
done
}
#################################################################
#################################################################
show(){
echo -e "\n"
echo "----------------------------------"
echo "please enter your choise:"
echo "(1) 异步迁移指定文件|目录"
echo "(2) 异步迁移所有文件"
echo "(3) 移动脚本"
echo "(6) 退出菜单"
echo "----------------------------------"
}
menu(){
input=0
while [ $input -ne 6 ]
do
show
read input
case $input in
1) rsync_files;; #异步迁移指定
2) rsync_all_files;; #异步迁移所有文件
3) move_script;; #移动脚本
6) exit;;
esac
done
}
menu
简单的排除可以使用配置,如果需要复杂的规则请自行使用脚本实现
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
Sign in to comment