forked from musq/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_backup.sh
executable file
·132 lines (82 loc) · 2.89 KB
/
create_backup.sh
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
#!/bin/bash
cd "$(dirname "${BASH_SOURCE[0]}")" \
&& . "utils.sh"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
init_backup() {
local -r BACKUP_HOME="$HOME/.backups/dotfiles-backup"
mkdir -p "$BACKUP_HOME"
# get the version
LAST_VERSION=$(
find "$BACKUP_HOME" -iname 'v[[:alnum:]]*' \
-type d | \
sed "s/.*\///" | \
cut -c 2- | \
sort -n | \
tail -1
)
CURRENT_VERSION=$((LAST_VERSION + 1))
BACKUP_DIR="$BACKUP_HOME/v$CURRENT_VERSION"
mkd "$BACKUP_DIR" \
|| ( print_error "Failed to create backup directory" && exit 1 )
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
create_backup() {
local files=$(find_files_to_backup "create_symlinks()")
for i in $files; do
sourceFile="$HOME/.$(printf "%s" "$i" \
| sed "s/\"//g" \
| sed "s/.*\/\(.*\)/\1/g")"
targetFile="$BACKUP_DIR/.$(printf "%s" "$i" \
| sed "s/\"//g" \
| sed "s/.*\/\(.*\)/\1/g")"
copy_file "$sourceFile" "$targetFile"
done
local files=$(find_files_to_backup "create_full_path_symlinks()")
for i in $files; do
sourceFile="$HOME/.$(printf "%s" "$i" \
| sed "s/\"//g")"
targetFile="$BACKUP_DIR/.$(printf "%s" "$i" \
| sed "s/\"//g")"
copy_file "$sourceFile" "$targetFile"
done
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
copy_file() {
if [[ -e "$1" && ! -L "$1" ]]; then
mkdir -p "$(dirname "$2")"
# If the source file is not a symlink, take a backup
execute \
"cp -a $1 $2" \
"$1 → $2" \
|| print_error "$1 → $2"
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
find_files_to_backup() {
local -r mark="$1"
local files=$(sed -n "/$mark/,/)$/p" create_symbolic_links.sh \
| grep "^\ *\".*\"$" \
| sed -e :a -e ';$!N;s/\n/ /;ta')
printf "%s" "$files"
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
finish_backup() {
# https://unix.stackexchange.com/questions/8430/how-to-remove-all-empty-directories-in-a-subtree
find "$BACKUP_DIR" -type d -empty -delete > /dev/null 2>&1
# The following rmdir command is added as hack to delete the topmost
# directory on MacOS. The exact bug is mentioned in the link.
# https://unix.stackexchange.com/questions/497666/relative-path-potentially-not-safe-error-with-find-delete-on-macos
rmdir "$BACKUP_DIR" > /dev/null 2>&1
if [ -d "$BACKUP_DIR" ]; then
print_success "Backup created successfully"
else
print_success "Backup is not needed, hence v$CURRENT_VERSION has been deleted"
fi
}
main() {
print_in_purple "\n ● Create backup\n\n"
init_backup
create_backup
finish_backup
}
main