write_current_date()
{
local backup_path="$1"
- local current_date="$(date +%F)"
- local typ=$(get_backup_type)
+ local current_date=
+ local typ=
+ current_date="$(date +%F)"
+ typ=$(get_backup_type)
mkdir --parents "history"
- echo "$current_date" > $backup_path/$typ
+ echo "$current_date" > "$backup_path/$typ"
echo "$current_date" > "history/$typ"
}
[ -f "$history_file" ] || return 0
- history_lines=$(cat $history_file | wc -l)
+ history_lines=$(wc -l "$history_file")
echo "ℹ️ INFO: $history_file has $history_lines lines"
- [ $history_lines -ge 100 ] \
- && savelog -c 10 -l $history_file \
+ [ "$history_lines" -ge 100 ] \
+ && savelog -c 10 -l "$history_file" \
&& echo "ℹ️ INFO: $history_file rotated"
}
write_history_entry()
{
local msg="$1"
- local current_date=$(date --iso-8601=seconds)
- local typ=$(get_backup_type)
+ local current_date=
+ local typ=
+ current_date=$(date --iso-8601=seconds)
+ typ=$(get_backup_type)
mkdir --parents "history"
rotate_history "history/history"
local backup_path="$1"
local backup_name="$2"
local last_backup_number="$3"
- local date=$(date +%F)
+ local date=
+ date=$(date +%F)
local backup_dir=
local number=
- for number in $(seq --format "%02g" $last_backup_number 99); do
+ for number in $(seq --format "%02g" "$last_backup_number" 99); do
backup_dir="$date"_"$number"_"$backup_name"
[ -d "$backup_path/$backup_dir" ] || break;
done
- echo $backup_dir
+ echo "$backup_dir"
}
get_last_backup_dir_path()
{
local backup_path="$1"
local backup_name="$2"
+ local last_backup_dir_path=
- local last_backup_dir_path=$(find $backup_path \
+ last_backup_dir_path=$(find "$backup_path" \
-maxdepth 1 \
-type d \
-name "*_""$backup_name" \
print_line()
{
- local i=
- for i in {1..80}; do
- printf "#"
- done
-
+ printf "#%.0s" {1..80}
printf "\n"
}
get_machine_excludes()
{
local machine="$1"
- local excludes=$(cat config.yaml \
- | yq --raw-output ".machines.\"$machine\".excludes[]" \
+ local excludes=
+ excludes=$(yq --raw-output ".machines.\"$machine\".excludes[]" \
+ config.yaml \
2> /dev/null)
[ "$excludes" == "null" ] && return
[ -z "$excludes" ] && return
- local options=""
+ local options=()
for exclude in $excludes; do
- options=$(echo "$options --exclude=$exclude")
+ options+=("--exclude=$exclude")
done
- echo "$options"
+ echo "${options[@]}"
}
backup_remote_pathes()
local machine="$2"
local last_backup_dir_path="$3"
local hardlinkopt=
- local remote_user=$(cat config.yaml \
- | yq --raw-output ".machines.\"$machine\".remote_user")
- [ -n "$remote_user" ] && [ $remote_user != "null" ] \
+ local remote_user=
+ local host=
+ local port=
+ local pathes=
+ local excludes=
+ remote_user=$(yq \
+ --raw-output ".machines.\"$machine\".remote_user" config.yaml)
+ [ -n "$remote_user" ] && [ "$remote_user" != "null" ] \
|| errlog "config.yaml missing remote_user for $machine" \
|| return $?
- local host=$(cat config.yaml \
- | yq --raw-output ".machines.\"$machine\".host")
+ host=$(yq --raw-output ".machines.\"$machine\".host" config.yaml)
[ -n "$host" ] && [ "$host" != "null" ] \
|| errlog "config.yaml missing host for $machine" \
|| return $?
- local port=$(cat config.yaml \
- | yq --raw-output ".machines.\"$machine\".port")
+ port=$(yq --raw-output ".machines.\"$machine\".port" config.yaml)
[ -n "$port" ] && [ "$port" != "null" ] \
|| errlog "config.yaml missing port for $machine" \
|| return $?
- local pathes=$(cat config.yaml \
- | yq --raw-output ".machines.\"$machine\".pathes[]")
+ pathes=$(yq --raw-output ".machines.\"$machine\".pathes[]" config.yaml)
[ -n "$pathes" ] \
|| errlog "config.yaml missing pathes for $machine" \
|| return $?
- local excludes=$(get_machine_excludes "$machine")
+ excludes=$(get_machine_excludes "$machine")
local YELLOW="\e[0;33m"
local LIGHTPURPLE="\e[1;35m"
--hard-links \
--relative \
--human-readable \
- $excludes \
- $hardlinkopt \
+ "$excludes" \
+ "$hardlinkopt" \
--rsh "ssh -p $port" \
- $hostpath \
- $backup_path/$machine \
+ "$hostpath" \
+ "$backup_path/$machine" \
|| errlog "could not backup $machine with path $path" \
|| return $?
local backup_path="$1"
local last_backup_dir_path="$2"
local hardlinkopt=
- local machines=$(cat config.yaml | yq --raw-output '.machines | keys[]')
+ local machines=
+ machines=$(yq --raw-output '.machines | keys[]' config.yaml)
[ -n "$machines" ] \
|| errlog "config.yaml no machines" \
|| return $?
local machine=
for machine in $machines; do
echo "ℹ️ INFO: create directory $backup_path/$machine"
- mkdir --parents $backup_path/$machine \
+ mkdir --parents "$backup_path/$machine" \
|| errlog "could not create directory $backup_path/$machine" \
|| return $?
local backup_path="$1"
local backup_name="$2"
local delete_count=$3
+ local backups=
- local backups=$(find $backup_path -maxdepth 1 -name "*$backup_name" \
+ backups=$(find "$backup_path" -maxdepth 1 -name "*$backup_name" \
| sort \
- | head --lines $delete_count)
+ | head --lines "$delete_count")
local backup=
for backup in $backups; do
echo "🗑️ DELETE: $backup"
- rm --recursive $backup
+ rm --recursive "$backup"
write_history_entry "deleted ${backup##*/}"
done
}
local backup_path="$1"
local backup_name="$2"
local backup_count=$3
+ local backup_counted=
- local backup_counted=$(ls --directory $backup_path/*/ \
+ backup_counted=$(find "$backup_path" -max-depth 1 -type d \
| grep "_$backup_name" \
| sort \
| wc --lines)
echo
echo "ℹ️ INFO: backup count: $backup_counted"
- if [ $backup_counted -gt $backup_count ]; then
+ if [ "$backup_counted" -gt "$backup_count" ]; then
echo "ℹ️ INFO: delete backup count: $delete_count"
echo
remove_backup_dir "$backup_path" "$backup_name" $delete_count
{
local backup_typ="$1"
local backup_date="$2"
- local backup_month=$(date --date="$backup_date" '+%Y-%m')
- local now_month=$(date '+%Y-%m')
- local diff=$(( ($(date --date="$backup_date" +%s) - $(date +%s)) / 86400 ))
+ local backup_month=
+ local now_month=
+ local diff=
+ backup_month=$(date --date="$backup_date" '+%Y-%m')
+ now_month=$(date '+%Y-%m')
+ diff=$(( ($(date --date="$backup_date" +%s) - $(date +%s)) / 86400 ))
case "$backup_typ" in
- "day") [ $diff -ge 0 ] || return $? ;;
- "week") [ $diff -ge -6 ] || return $? ;;
- "month") [ "$backup_month" == "$now_month" ] || return $? ;;
+ "day") [ $diff -ge 0 ] || return 1 ;;
+ "week") [ $diff -ge -6 ] || return 1 ;;
+ "month") [ "$backup_month" == "$now_month" ] || return 1 ;;
*) return 0;;
esac
}
for typ in day week month; do
if [ -f "$history_path/$typ" ]; then
color=$LIGHTGREEN
- is_backup_old $typ $(cat $history_path/$typ) \
+ is_backup_old "$typ" "$(cat $history_path/$typ)" \
|| color=$LIGHTRED
printf "${color}last backup %s " "$typ"
- printf "am %s${RESET}\n" "$(cat $history_path/$typ)"
+ printf "am %s%s\n" "$(cat $history_path/$typ)" "$RESET"
else
printf "${LIGHTRED}no backup %s " $typ
- printf "exists${RESET}\n"
+ printf "exists%s}\n" "$RESET"
fi
done
echo
main()
{
local only_check="$1"
+ local last_backup_dir_path=
+ local today_date=
+ local backup_dir=
[ "$only_check" == "check" ] && check_backup_date && return 0
|| errlog "file config.yaml not found" \
|| return $?
- backup_path=$(cat config.yaml | yq --raw-output '.backup_path')
- backup_name=$(cat config.yaml | yq --raw-output '.backup_name')
- backup_count=$(cat config.yaml | yq --raw-output '.backup_count')
+ backup_path=$(yq --raw-output '.backup_path' config.yaml)
+ backup_name=$(yq --raw-output '.backup_name' config.yaml)
+ backup_count=$(yq --raw-output '.backup_count' config.yaml)
[ -n "$backup_path" ] && [ "$backup_path" != "null" ] \
|| errlog "config.yaml missing backup_path" \
|| return $?
echo "ℹ️ INFO: backup count $backup_count"
- local last_backup_dir_path=$(get_last_backup_dir_path \
+ last_backup_dir_path=$(get_last_backup_dir_path \
"$backup_path" \
"$backup_name")
echo "ℹ️ INFO: last backup dir path $last_backup_dir_path"
- local today_date=$(date +%F)
+ today_date=$(date +%F)
local last_backup_number=1
- [[ "$last_backup_dir_path" =~ "$today_date" ]] \
+ [[ "$last_backup_dir_path" =~ $today_date ]] \
&& last_backup_number=${last_backup_dir_path##*/} \
&& last_backup_number=${last_backup_number:11:2}
- local backup_dir=$(get_backup_dir \
+ backup_dir=$(get_backup_dir \
"$backup_path" \
"$backup_name" \
"$last_backup_number")
cleanup_backups \
"$backup_path" \
"$backup_name" \
- $backup_count \
+ "$backup_count" \
|| return $?
check_backup_date \
|| return $?
echo "✅ SUCCESS: backup"
}
-main $*
+main "$*"
partition_mount()
{
local media="$1"
- local partition=$(cat config.yaml \
- | yq --raw-output ".media.\"$media\".partition")
+ local partition=
+ local backup_path=
+ partition=$(yq --raw-output ".media.\"$media\".partition" config.yaml)
[ "$partition" != "null" ] \
|| errlog "config.yaml partition for $media missing" \
|| return $?
[ -b "$partition" ] \
|| errlog "partition $partition not found" \
|| return $?
- local backup_path=$(cat config.yaml \
- | yq --raw-output ".backup_path")
+ backup_path=$(yq --raw-output ".backup_path" config.yaml)
[ "$backup_path" != "null" ] \
|| errlog "config.yaml backup path missing" \
|| return $?
{
local media="$1"
local device="$2"
+ local key=
+ local volume=
[ -b "$device" ] \
|| infolog "block device $device for $media not found" \
|| return $?
infolog "media $media with device $device found"
- local key=$(cat "config.yaml" \
- | yq --raw-output ".media.\"$media\".key")
+ key=$(yq --raw-output ".media.\"$media\".key" config.yaml)
[ "$key" != "null" ] \
|| errlog "config.yaml key for $media missing" \
|| return $?
|| errlog "key $key not exists" \
|| return $?
infolog "use key $key"
- local volume=$(cat "config.yaml" \
- | yq --raw-output ".backup_volume")
+ volume=$(yq --raw-output ".backup_volume" config.yaml)
[ "$volume" != "null" ] \
|| errlog "config.yaml volume for $media missing" \
|| return $?
mount_backup()
{
- local medias=$(cat config.yaml \
- | yq --raw-output '.media | keys[]')
+ local medias=
+ medias=$(yq --raw-output '.media | keys[]' config.yaml)
[ -n "$medias" ] && [ "$medias" != "null" ] \
|| errlog "config.yaml missing media section" \
local media=
local device=
for media in $medias; do
- device=$(cat config.yaml \
- | yq --raw-output ".media.\"$media\".device")
+ device=$(yq --raw-output ".media.\"$media\".device" config.yaml)
[ "$device" != "null" ] \
|| errlog "missing path for $media" \
|| return $?
umount_backup()
{
+ local backup_path=
+ local volume=
- local backup_path=$(cat config.yaml \
- | yq --raw-output ".backup_path")
+ backup_path=$(yq --raw-output ".backup_path" config.yaml)
[ "$backup_path" != "null" ] \
|| errlog "config.yaml backup path missing" \
|| return $?
|| errlog "clound not remote $backup_path" \
|| return $?
- local volume=$(cat "config.yaml" \
- | yq --raw-output ".backup_volume")
+ volume=$(yq --raw-output ".backup_volume" config.yaml)
[ "$volume" != "null" ] \
|| errlog "config.yaml volume for $media missing" \
|| return $?
echo "✅ SUCCESS: backupmount"
}
-main $*
+main "$*"
print_line()
{
- for i in {1..80}; do
- printf "#"
- done
-
+ printf "#%.0s" {1..80}
printf "\n"
}
local machine="$3"
local path="$4"
local origin="$5"
-
- local backup_path=$(cat config.yaml \
- | yq --raw-output ".backup_path")
- local backup_name=$(cat config.yaml \
- | yq --raw-output ".backup_name")
- local machine=$(cat config.yaml \
- | yq --raw-output \
- ".machines | keys[] | select(. == \"$machine\")")
- local remote_user=$(cat config.yaml \
- | yq --raw-output ".machines.\"$machine\".remote_user")
- local remote_machine=$(cat config.yaml \
- | yq --raw-output ".machines.\"$machine\".host")
- local remote_port=$(cat config.yaml \
- | yq --raw-output ".machines.\"$machine\".port")
+ local backup_path=
+ local backup_name=
+ local machine=
+ local remote_user=
+ local remote_machine=
+ local remote_port=
+
+ backup_path=$(yq --raw-output ".backup_path" config.yaml)
+ backup_name=$(yq --raw-output ".backup_name" config.yaml)
+ machine=$(yq --raw-output \
+ ".machines | keys[] | select(. == \"$machine\")" config.yaml)
+ remote_user=$(yq --raw-output ".machines.\"$machine\".remote_user" \
+ config.yaml)
+ remote_machine=$(yq --raw-output ".machines.\"$machine\".host" \
+ config.yaml)
+ remote_port=$(yq --raw-output ".machines.\"$machine\".port" config.yaml)
[ -d "$backup_path" ] \
|| errlog "could not find directory $backup_path" \
&& echo "⚠️ WARN: delete mode on" && echo
local ask_restore="N"
- read -p "❓ QUESTION: Do you want restore? [N/y] " ask_restore
+ read -rp "❓ QUESTION: Do you want restore? [N/y] " ask_restore
local ask_restore=${ask_restore:-N}
local ask_restore=${ask_restore,,}
- echo ${ask_restore,,}
+ echo "${ask_restore,,}"
[ "$ask_restore" == "y" ] \
|| errlog "you want not do restore" \
--acls \
--hard-links \
--human-readable \
- $deletemode \
+ "$deletemode" \
--rsh "ssh -p $remote_port" \
- $restore_path \
- $remote_path \
+ "$restore_path" \
+ "$remote_path" \
|| errlog "could not restore" \
|| return $?
local number=
local machine=
local path=
- local key=
local origin="deletemode-off"
echo "⭐ START: restore"
- while getopts "d:k:n:r:p:ho" optname; do
+ while getopts "d:n:r:p:ho" optname; do
case "$optname" in
"d") date="$OPTARG" ;;
"n") number="$OPTARG" ;;
"p") path="$OPTARG" ;;
"o") origin="deletemode-on" ;;
"h") useage && return 0 ;;
+ *) usage && return 0 ;;
esac
done
echo "✅ SUCCESS: restore"
}
-main $*
+main "$"*