forked from mkbula/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup-files
More file actions
executable file
·152 lines (126 loc) · 3.74 KB
/
Copy pathbackup-files
File metadata and controls
executable file
·152 lines (126 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/bin/bash
source "$HOME/.local/share/dotfiles/bin/lib/helpers.sh"
set -e
log_header "Backup Files"
backup_dir="$HOME/backups"
timestamp=$(date +%Y%m%d_%H%M%S)
current_dir="$HOME"
selected_for_backup=()
if ! command -v fzf &>/dev/null; then
log_error "fzf is not installed. Please install it to continue."
exit 1
fi
if ! command -v pv &>/dev/null; then
log_error "pv is not installed. Please install it for progress bars."
exit 1
fi
mkdir -p "$backup_dir"
while true; do
items=$(find "$current_dir" -mindepth 1 -maxdepth 1 \
! -path "$backup_dir" \
! -path "$HOME/.cache" \
! -path "$HOME/.claude" \
! -path "$HOME/.java" \
! -path "$HOME/.chromium" \
! -path "$HOME/.npm" \
! -path "$HOME/.oh-my-zsh" \
! -path "$HOME/.var" \
! -path "$HOME/.zsh_history" \
! -path "$HOME/.cargo" \
2>/dev/null | sort)
if [ "$current_dir" != "$HOME" ]; then
items="[..]
$items"
fi
selection_info=""
if [ ${#selected_for_backup[@]} -gt 0 ]; then
selection_info=" (${#selected_for_backup[@]} selected)"
fi
header_text="enter: navigate, tab: select, ctrl-d: done, ctrl-c: cancel${selection_info}"
term_width=$(tput cols)
padding=$(( (term_width - ${#header_text}) / 2 ))
centered_header=$(printf "%${padding}s%s" "" "$header_text")
fzf_args=(
--multi
--header="$centered_header"
--expect=ctrl-d
--prompt="[$current_dir] > "
--color 'pointer:green,marker:green'
)
result=$(echo "$items" | fzf "${fzf_args[@]}")
exit_code=$?
if [ $exit_code -eq 130 ]; then
break
fi
key=$(echo "$result" | head -1)
selection=$(echo "$result" | tail -n +2)
if [ -z "$selection" ]; then
continue
fi
if [ "$key" = "ctrl-d" ]; then
while IFS= read -r item; do
if [ "$item" != "[..]" ] && [ -n "$item" ]; then
selected_for_backup+=("$item")
fi
done <<< "$selection"
break
fi
item_count=$(echo "$selection" | wc -l)
if [ $item_count -gt 1 ]; then
while IFS= read -r item; do
if [ "$item" != "[..]" ] && [ -n "$item" ]; then
selected_for_backup+=("$item")
fi
done <<< "$selection"
echo "Added $item_count items to backup list"
sleep 0.5
else
if [ "$selection" = "[..]" ]; then
current_dir=$(dirname "$current_dir")
elif [ -d "$selection" ]; then
current_dir="$selection"
else
selected_for_backup+=("$selection")
echo "Added to backup list: $(basename "$selection")"
sleep 0.5
fi
fi
done
if [ ${#selected_for_backup[@]} -eq 0 ]; then
log_info "No items selected. Exiting."
exit 0
fi
log_step "Starting backup of ${#selected_for_backup[@]} items..."
for full_path in "${selected_for_backup[@]}"; do
if [ ! -e "$full_path" ]; then
log_error "Warning: '$full_path' does not exist, skipping..."
continue
fi
base_name=$(basename "$full_path")
display_path=$(echo "$full_path" | sed "s|$HOME|~|")
if [ -d "$full_path" ]; then
backup_file="$backup_dir/${base_name}_${timestamp}.tar.gz"
log_info "Backing up: $display_path"
total_size=$(du -sb "$full_path" 2>/dev/null | awk '{print $1}')
tar -cf - -C "$(dirname "$full_path")" "$(basename "$full_path")" |
pv -pterb -s "$total_size" |
gzip >"$backup_file"
log_success "$display_path backed up"
log_detail "$backup_file"
else
extension="${base_name##*.}"
name="${base_name%.*}"
if [ "$extension" = "$base_name" ]; then
backup_file="$backup_dir/${base_name}_${timestamp}"
else
backup_file="$backup_dir/${name}_${timestamp}.${extension}"
fi
log_info "Backing up: $display_path"
pv "$full_path" >"$backup_file"
log_success "$display_path backed up"
log_detail "$backup_file"
fi
echo ""
done
log_detail "Backup location: $backup_dir"
show_done