-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackup
More file actions
executable file
·119 lines (105 loc) · 3.07 KB
/
backup
File metadata and controls
executable file
·119 lines (105 loc) · 3.07 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
#!/usr/bin/env bash
set -e
DOTFILES_BACKUP_DELETE_LOCAL=0
DOTFILES_BACKUP_DIR=".dotfiles.bak.$(date +'%Y%m%d-%H%M')"
# Backup original files that might be overwritten
dotfiles_backup() {
local file_name
for file_name in \
".bash_profile" \
".bashrc" \
".bash_aliases" \
".bash_functions" \
".bash_logout" \
\
".config/git/config" \
".config/git/config.local" \
".config/git/ignore" \
".config/lazydocker/config.yml" \
".config/tmux/local" \
".config/tmux/tmux.conf" \
".config/tmux/tmux.conf.local" \
".config/tmux/tmux.plugins.local" \
\
".config/AutoHotkey/config.local.ahk" \
".minttyrc" \
\
".config/autokey/data" \
".config/xbindkeys" \
".config/xmodmap" \
\
".config/karabiner"
do
dotfile_backup "${file_name}"
done
}
dotfile_backup() {
if [[ ! -f "${HOME}/${file_name}" && ! -d "${HOME}/${file_name}" ]]; then
return
fi
if [[ -z "$DOTFILES_BACKUP_DIR" ]]; then
echo "Undefined backup directory"
exit 1
fi
local file_name=$1
if [[ ! -d "${HOME}/${DOTFILES_BACKUP_DIR}" ]]; then
mkdir -p "${HOME}/${DOTFILES_BACKUP_DIR}"
fi
local dir_name; dir_name=$(dirname "$file_name")
if [[ $dir_name != "." ]]; then
mkdir -p "${HOME}/${DOTFILES_BACKUP_DIR}/${dir_name}"
fi
echo -e "\e[1;32mBacking up ~/${file_name}\e[0m"
cp -RL "${HOME}/${file_name}" "${HOME}/${DOTFILES_BACKUP_DIR}/${file_name}"
}
# Delete original files that should be linked
dotfile_delete_default() {
local file_name file_path
for file_name in \
".bash_profile" \
".bashrc" \
".bash_aliases" \
".bash_functions" \
".bash_logout" \
\
".config/bat" \
".config/fastfetch" \
".config/git" \
".config/gitmux" \
".config/lazydocker/config.yml" \
".config/tmux" \
".config/vim" \
\
"AutoHotkey" \
"Documents/Dygma" \
".minttyrc" \
\
".config/autokey/data/Phrases" \
".config/autokey/data/Scripts" \
".config/xbindkeys" \
".config/xmodmap"
do
file_path="${HOME}/${file_name}"
if [[ ! -f "$file_path" && ! -d "$file_path" ]]; then
continue # not found
fi
if [[ -L "$file_path" ]]; then
continue # symlink
fi
echo -e "\e[1;31mDeleting $file_path\e[0m"
rm -rf --preserve-root "$file_path"
done
}
# Read command options
while getopts ":-:" opt; do
case "${OPTARG}" in
delete)
DOTFILES_BACKUP_DELETE_LOCAL=1
;;
esac
done
# Run
dotfiles_backup
if [[ "$DOTFILES_BACKUP_DELETE_LOCAL" -eq 1 ]]; then
dotfile_delete_default
fi