blob: dd40f25695cd98af32e64f1467e18627470f9796 (
plain)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
#! /bin/bash
#####################################################################
# ___ __ ___ __
# __| _/ ____ _/ |_ \_ |__ _____ ____ | | ____ ________
# / __ | / __ \\ __\ | __ \\__ \ _/ ___\| |/ / | \____ \
# / /_/ |( \_\ )| | | \_\ \/ __ \_ \___| \| | / |_\ \
# \____ | \____/ |__| |___ /____ /\___ /__|_ \____/| ___/
# \/ \/ \/ \/ \/ |__|
#
# by danix
#
#####################################################################
set -euo pipefail
# Colors for output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
### DIRECTORIES
WORKDIR=$(pwd)
# Defaults (can be overridden by config file)
DEFAULT_OUTPUT_DIR="${HOME}/Programming/GIT/my-dotfiles"
LOG_FILE="${HOME}/.local/share/dot-backup/backup.log"
DOTFILES_LIST="${HOME}/.config/dot-backup/files.list"
CONFIG_FILE="${HOME}/.config/dot-backup/config"
# Bootstrap config and log directories on first run
FIRST_RUN=false
if [[ ! -d "$(dirname "$CONFIG_FILE")" ]]; then
FIRST_RUN=true
fi
mkdir -p "$(dirname "$CONFIG_FILE")"
mkdir -p "$(dirname "$LOG_FILE")"
# Load config file if present
if [[ -f "$CONFIG_FILE" ]]; then
# shellcheck source=/dev/null
source "$CONFIG_FILE"
fi
# Flags
DRY_RUN=false
VERBOSE=false
RESTORE=false
QUIET=false
usage() {
echo "Usage: $0 [options]"
echo " -n, --dry-run Show what would be copied without copying"
echo " -v, --verbose Print each file as it is processed"
echo " -r, --restore Restore dotfiles from backup to their original locations"
echo " -q, --quiet Suppress stdout; write output to log instead"
echo " -h, --help Show this help"
exit 0
}
for arg in "$@"; do
case $arg in
-n|--dry-run) DRY_RUN=true ;;
-v|--verbose) VERBOSE=true ;;
-r|--restore) RESTORE=true ;;
-q|--quiet) QUIET=true ;;
-h|--help) usage ;;
*) echo -e "${RED}Unknown option: $arg${NC}"; usage ;;
esac
done
if [[ "$QUIET" == true ]]; then
# save original stdout so we can notify user after redirect
exec 3>&1
# strip color codes and write to log
exec > >(sed 's/\x1b\[[0-9;]*m//g' > "$LOG_FILE") 2>&1
fi
# The list of dotfiles
DOTFILES=(
# Shell configurations
".bashrc"
".bash_profile"
".bash_aliases"
".bash_functions"
".dir_colors"
".lessfilter"
".profile.d"
# Shell history (optional - comment out if you don't want history)
# ".bash_history"
# Git configuration
".gitconfig"
".gitignore_global"
".gitmessage"
# Desktop
".fonts"
".icons"
".local/share/applications"
".local/share/icons"
".user-icon.jpg"
# Editors
".config/nvim"
".config/sublime-text"
# hyprland
".config/hypr"
".config/wal"
".config/waybar"
".config/qarma.conf"
# firefox
# ".mozilla"
# thunderbird
# ".thunderbird"
# music players
".config/mpd"
".config/rmpc"
".config/sayonara"
".config/haruna"
".config/harunarc"
# other programs
".config/bash-notes.rc"
".config/cherrytree"
".config/conky"
".config/draw.io"
".config/featherpad"
".config/htop"
".config/joplin-desktop"
".config/kitty"
".config/lximage-qt"
".config/lxqt"
".config/mimeapps.list"
".config/pcmanfm-qt"
".config/powerline"
".config/qpdfview"
".config/rofi"
# pandoc
".local/share/pandoc/"
# my executables
"bin"
### SYSTEM FILES
# bash completion
"/etc/bash_completion.d"
)
# Load external list if it exists, appending to built-in list
if [[ -f "$DOTFILES_LIST" ]]; then
while IFS= read -r line || [[ -n "$line" ]]; do
[[ "$line" =~ ^#.*$ || -z "$line" ]] && continue
DOTFILES+=("$line")
done < "$DOTFILES_LIST"
fi
# helper function to check if a string ($2) begins with another string ($1)
beginswith() {
case $2 in
"$1"*)
true
;;
*)
false
;;
esac;
}
lastupdate() {
D=$(date)
[ ! -f ${DEFAULT_OUTPUT_DIR}/lastupdate ] && touch ${DEFAULT_OUTPUT_DIR}/lastupdate
echo "Last update: $D" > ${DEFAULT_OUTPUT_DIR}/lastupdate
}
log_verbose() {
[[ "$VERBOSE" == true ]] && echo -e "$1"
}
do_rsync() {
local src="$1"
local dst="$2"
local is_dir="${3:-false}"
if [[ "$DRY_RUN" == true ]]; then
echo -e "\t ${YELLOW}[dry-run] would copy: $src → $dst${NC}"
else
if [[ "$is_dir" == true ]]; then
mkdir -p "$dst"
if [[ "$VERBOSE" == true ]]; then
rsync -av "$src/" "$dst/"
else
rsync -a "$src/" "$dst/" && echo -e "\t ${GREEN}Copied${NC}"
fi
else
if [[ "$VERBOSE" == true ]]; then
rsync -av "$src" "$dst"
else
rsync -a "$src" "$dst" && echo -e "\t ${GREEN}Copied${NC}"
fi
fi
fi
}
# ── RESTORE MODE ──────────────────────────────────────────────────────────────
do_restore() {
if [[ ! -d "$DEFAULT_OUTPUT_DIR" ]]; then
echo -e "${RED}Backup directory not found: $DEFAULT_OUTPUT_DIR${NC}"
exit 1
fi
echo -e "${YELLOW}Restoring dotfiles from: ${BLUE}$DEFAULT_OUTPUT_DIR${NC}\n"
for i in "${DOTFILES[@]}"; do
if beginswith "/" "$i"; then
# system file
local backup_path="${DEFAULT_OUTPUT_DIR}/system/${i#/}"
local dest="$i"
else
local backup_path="${DEFAULT_OUTPUT_DIR}/home/$i"
local dest="${HOME}/$i"
fi
if [[ -f "$backup_path" ]]; then
echo -e "${GREEN}$dest${NC}"
if [[ "$DRY_RUN" == true ]]; then
echo -e "\t ${YELLOW}[dry-run] would restore: $backup_path → $dest${NC}"
else
mkdir -p "$(dirname "$dest")"
if [[ "$VERBOSE" == true ]]; then
rsync -av "$backup_path" "$dest"
else
rsync -a "$backup_path" "$dest" && echo -e "\t ${GREEN}Restored${NC}"
fi
fi
elif [[ -d "$backup_path" ]]; then
echo -e "${BLUE}$dest${NC}"
if [[ "$DRY_RUN" == true ]]; then
echo -e "\t ${YELLOW}[dry-run] would restore: $backup_path/ → $dest/${NC}"
else
mkdir -p "$dest"
if [[ "$VERBOSE" == true ]]; then
rsync -av "$backup_path/" "$dest/"
else
rsync -a "$backup_path/" "$dest/" && echo -e "\t ${GREEN}Restored${NC}"
fi
fi
else
log_verbose "\t ${YELLOW}SKIP (not in backup): $i${NC}"
fi
done
if [[ "$DRY_RUN" == true ]]; then
echo -e "\n${YELLOW}Dry run complete. Nothing restored.${NC}"
else
echo -e "\n${GREEN}Restore complete.${NC}"
fi
if [[ "$QUIET" == true ]]; then echo "Log written to: $LOG_FILE" >&3; fi
exit 0
}
# ── MAIN ──────────────────────────────────────────────────────────────────────
clear
if [[ "$FIRST_RUN" == true ]]; then
echo -e "${GREEN}First run — config directories created:${NC}"
echo -e " ${BLUE}$(dirname "$CONFIG_FILE")${NC} — drop config and files.list here"
echo -e " ${BLUE}$(dirname "$LOG_FILE")${NC} — logs written here when using -q"
echo -e "\nOptional next steps:"
echo -e " cp config.example ${CONFIG_FILE}"
echo -e " touch ${DOTFILES_LIST}"
echo -e "\nProceeding with backup using built-in defaults...\n"
fi
if [[ "$DRY_RUN" == true ]]; then
echo -e "${YELLOW}*** DRY RUN — no files will be copied ***${NC}\n"
fi
if [[ "$RESTORE" == true ]]; then
do_restore
fi
echo -e "${YELLOW}Local Git Repository: ${NC}"
if [[ -d $DEFAULT_OUTPUT_DIR ]]; then
echo -e "\t${BLUE}$DEFAULT_OUTPUT_DIR ${GREEN} ${NC}"
[ -d $DEFAULT_OUTPUT_DIR/home ] || mkdir -p $DEFAULT_OUTPUT_DIR/home
[ -d $DEFAULT_OUTPUT_DIR/system ] || mkdir -p $DEFAULT_OUTPUT_DIR/system
cd $DEFAULT_OUTPUT_DIR
if [[ $(git rev-parse --is-inside-work-tree) == "true" ]]; then
echo -e "${GREEN}Git Repository already initialized.${NC}"
else
echo -e "${YELLOW}Initializing Git Repository. Don't forget to add your remotes.${NC}"
git init
git add .
fi
else
echo -e "\t${BLUE}$DEFAULT_OUTPUT_DIR ${RED} ${NC}"
if [[ "$DRY_RUN" == false ]]; then
echo -e "${YELLOW}creating our backup directories${NC}"
mkdir -p $DEFAULT_OUTPUT_DIR/{home,system}
echo -e "${YELLOW}Initializing Git Repository. Don't forget to add your remotes.${NC}"
cd $DEFAULT_OUTPUT_DIR
git init
git add .
fi
fi
if [[ "$DRY_RUN" == false ]]; then
lastupdate
fi
echo -e $NC
cd $WORKDIR
# we iterate all dotfiles in the list
for i in "${DOTFILES[@]}"; do
# if it's a file in my home
if [[ -f ${HOME}/$i ]]; then
echo -e "${GREEN}${HOME}/$i${NC}"
do_rsync "${HOME}/$i" "${DEFAULT_OUTPUT_DIR}/home/$i"
# if it's a directory in my home
elif [[ -d ${HOME}/$i ]]; then
echo -e "${BLUE}${HOME}/$i${NC}"
do_rsync "${HOME}/$i" "${DEFAULT_OUTPUT_DIR}/home/$i" true
# if it begins with a / it's a system file/directory
elif beginswith "/" $i; then
if [[ -f $i ]]; then
echo -e "${GREEN}$i${NC}"
do_rsync "$i" "${DEFAULT_OUTPUT_DIR}/system/$i"
elif [[ -d $i ]]; then
echo -e "${BLUE}$i${NC}"
do_rsync "$i" "${DEFAULT_OUTPUT_DIR}/system/${i#/}" true
fi
else
echo -e "\n${RED}NOT FOUND: ${i}${NC}\n"
fi
done
if [[ "$DRY_RUN" == true ]]; then
echo -e "\n${YELLOW}Dry run complete. No files copied, no git changes made.${NC}"
exit 0
fi
echo -e "\n${GREEN}Adding all copied files to git.${NC}"
cd $DEFAULT_OUTPUT_DIR
git add .
if git diff --cached --quiet; then
echo -e "${YELLOW}Nothing new to commit.${NC}"
else
git commit -m "backup: $(date '+%Y-%m-%d %H:%M:%S')"
echo -e "${GREEN}Committed.${NC}"
fi
echo -e $NC
cd $WORKDIR
if [[ "$QUIET" == true ]]; then echo "Log written to: $LOG_FILE" >&3; fi
exit 0
|