forked from Shinonome/dots-hyprland
Improve backup functions
This commit is contained in:
@@ -309,3 +309,49 @@ function auto_get_git_submodule(){
|
|||||||
x git submodule update --init --recursive
|
x git submodule update --init --recursive
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function backup_clashing_targets(){
|
||||||
|
# For non-recursive dirs/files under target_dir, only backup those which clashes with the ones under source_dir
|
||||||
|
# However, ignore the ones listed in ignored_list
|
||||||
|
|
||||||
|
# Deal with arguments
|
||||||
|
local source_dir="$1"
|
||||||
|
local target_dir="$2"
|
||||||
|
local backup_dir="$3"
|
||||||
|
local -a ignored_list=("${@:4}")
|
||||||
|
|
||||||
|
# Find clash dirs/files, save as clash_list
|
||||||
|
local clash_list=()
|
||||||
|
local source_list=($(ls -A "$source_dir"))
|
||||||
|
local target_list=($(ls -A "$target_dir"))
|
||||||
|
local -A target_map
|
||||||
|
for i in "${target_list[@]}"; do
|
||||||
|
target_map["$i"]=1
|
||||||
|
done
|
||||||
|
for i in "${source_list[@]}"; do
|
||||||
|
if [[ -n "${target_map[$i]}" ]]; then
|
||||||
|
clash_list+=("$i")
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
local -A delk
|
||||||
|
for del in "${ignored_list[@]}" ; do delk[$del]=1 ; done
|
||||||
|
for k in "${!clash_list[@]}" ; do
|
||||||
|
[ "${delk[${clash_list[$k]}]-}" ] && unset 'clash_list[k]'
|
||||||
|
done
|
||||||
|
clash_list=("${clash_list[@]}")
|
||||||
|
|
||||||
|
# Construct args_includes for rsync
|
||||||
|
local args_includes=()
|
||||||
|
for i in "${clash_list[@]}"; do
|
||||||
|
if [[ -d "$target_dir/$i" ]]; then
|
||||||
|
args_includes+=(--include="/$i/")
|
||||||
|
args_includes+=(--include="/$i/**")
|
||||||
|
else
|
||||||
|
args_includes+=(--include="/$i")
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
args_includes+=(--exclude='*')
|
||||||
|
|
||||||
|
x mkdir -p $backup_dir
|
||||||
|
x rsync -av --progress "${args_includes[@]}" "$target_dir/" "$backup_dir/"
|
||||||
|
}
|
||||||
|
|||||||
@@ -18,50 +18,10 @@ function warning_rsync_normal(){
|
|||||||
printf "${STY_RST}"
|
printf "${STY_RST}"
|
||||||
}
|
}
|
||||||
|
|
||||||
function backup_clashing_targets(){
|
function backup_configs(){
|
||||||
# For dirs/files under target_dir, only backup those which clashes with the ones under source_dir
|
backup_clashing_targets dots/.config $XDG_CONFIG_HOME "${BACKUP_DIR}/.config"
|
||||||
# However, ignore the ones listed in ignored_list
|
backup_clashing_targets dots/.local/share $XDG_DATA_HOME "${BACKUP_DIR}/.local/share"
|
||||||
|
printf "${STY_BLUE}Backup into \"${BACKUP_DIR}\" finished.${STY_RST}\n"
|
||||||
# Deal with arguments
|
|
||||||
local source_dir="$1"
|
|
||||||
local target_dir="$2"
|
|
||||||
local backup_dir="$3"
|
|
||||||
local -a ignored_list=("${@:4}")
|
|
||||||
|
|
||||||
# Find clash dirs/files, save as clash_list
|
|
||||||
local clash_list=()
|
|
||||||
local source_list=($(ls -A "$source_dir"))
|
|
||||||
local target_list=($(ls -A "$target_dir"))
|
|
||||||
local -A target_map
|
|
||||||
for i in "${target_list[@]}"; do
|
|
||||||
target_map["$i"]=1
|
|
||||||
done
|
|
||||||
for i in "${source_list[@]}"; do
|
|
||||||
if [[ -n "${target_map[$i]}" ]]; then
|
|
||||||
clash_list+=("$i")
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
local -A delk
|
|
||||||
for del in "${ignored_list[@]}" ; do delk[$del]=1 ; done
|
|
||||||
for k in "${!clash_list[@]}" ; do
|
|
||||||
[ "${delk[${clash_list[$k]}]-}" ] && unset 'clash_list[k]'
|
|
||||||
done
|
|
||||||
clash_list=("${clash_list[@]}")
|
|
||||||
|
|
||||||
# Construct args_includes for rsync
|
|
||||||
local args_includes=()
|
|
||||||
for i in "${clash_list[@]}"; do
|
|
||||||
if [[ -d "$target_dir/$i" ]]; then
|
|
||||||
args_includes+=(--include="/$i/")
|
|
||||||
args_includes+=(--include="/$i/**")
|
|
||||||
else
|
|
||||||
args_includes+=(--include="/$i")
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
args_includes+=(--exclude='*')
|
|
||||||
|
|
||||||
x mkdir -p $backup_dir
|
|
||||||
x rsync -av --progress "${args_includes[@]}" "$target_dir/" "$backup_dir/"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function ask_backup_configs(){
|
function ask_backup_configs(){
|
||||||
@@ -71,27 +31,19 @@ function ask_backup_configs(){
|
|||||||
printf "${STY_RST}"
|
printf "${STY_RST}"
|
||||||
while true;do
|
while true;do
|
||||||
echo " y = Yes, backup"
|
echo " y = Yes, backup"
|
||||||
echo " n = No, skip to next"
|
echo " n/s = No, skip to next"
|
||||||
local p; read -p "====> " p
|
local p; read -p "====> " p
|
||||||
case $p in
|
case $p in
|
||||||
[yY]) echo -e "${STY_BLUE}OK, doing backup...${STY_RST}" ;local backup=true;break ;;
|
[yY]) echo -e "${STY_BLUE}OK, doing backup...${STY_RST}" ;local backup=true;break ;;
|
||||||
[nN]) echo -e "${STY_BLUE}Alright, skipping...${STY_RST}" ;local backup=false;break ;;
|
[nNsS]) echo -e "${STY_BLUE}Alright, skipping...${STY_RST}" ;local backup=false;break ;;
|
||||||
*) echo -e "${STY_RED}Please enter [y/n].${STY_RST}";;
|
*) echo -e "${STY_RED}Please enter [y/n].${STY_RST}";;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
if $backup;then
|
if $backup;then backup_configs;fi
|
||||||
backup_clashing_targets dots/.config $XDG_CONFIG_HOME "${BACKUP_DIR}/.config"
|
|
||||||
backup_clashing_targets dots/.local/share $XDG_DATA_HOME "${BACKUP_DIR}/.local/share"
|
|
||||||
printf "${STY_BLUE}Backup into \"${BACKUP_DIR}\" finished.${STY_RST}\n"
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
function auto_backup_configs(){
|
function auto_backup_configs(){
|
||||||
# Backup when $BACKUP_DIR does not exist
|
# Backup when $BACKUP_DIR does not exist
|
||||||
if [[ ! -d "$BACKUP_DIR" ]]; then
|
if [[ ! -d "$BACKUP_DIR" ]]; then backup_configs;fi
|
||||||
backup_clashing_targets dots/.config $XDG_CONFIG_HOME "${BACKUP_DIR}/.config"
|
|
||||||
backup_clashing_targets dots/.local/share $XDG_DATA_HOME "${BACKUP_DIR}/.local/share"
|
|
||||||
printf "${STY_BLUE}Backup into \"${BACKUP_DIR}\" finished.${STY_RST}\n"
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#####################################################################################
|
#####################################################################################
|
||||||
|
|||||||
Reference in New Issue
Block a user