forked from mathiasbynens/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestore_and_backup.sh
executable file
·255 lines (213 loc) · 6.21 KB
/
restore_and_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
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
#!/usr/bin/bash
# This is a shell script that provides functionality to backup, update, and restore dotfiles using
# rsync. It also includes options for installing zsh and Oh My Zsh. The script uses command-line
# options to determine which subcommand to execute and includes functions for pre-processing and
# post-processing logic.
# Default values
help=false
quiet=false
install_omz=false
dry_run=false
# get the current directory
# DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
rsync_default_options="avhC"
#------------------------------------------------------------------------------
function pre_process() {
# Script logic based on the options
if $help; then
exit 0
fi
if $quiet; then
echo "Running in quiet mode"
fi
# cd "$(dirname "${BASH_SOURCE}")";
# git pull origin main;
#------------------------------------------------------------------------------
# if havent install zsh, install it first
if ! command -v zsh &> /dev/null
then
echo "zsh could not be found"
echo "installing zsh"
sudo apt -y install zsh
echo "zsh installed please run this script again"
exit 1
fi
}
# Function to back up dotfiles using rsync
backup_dotfiles() {
# Accept source directory and backup directory as parameters
source_dir="$1"
dest_dir="$2"
# Ensure the backup directory exists
mkdir -p "$dest_dir"
echo $ignore_single_file_cmd
echo "backup is depreciated, please manual edit"
exit 1
# Perform the backup using rsync
rsync \
--files-from="./sources/.file_list" \
--exclude-from="./sources/.ex_list" \
-$rsync_default_options \
--no-perms \
"$source_dir" "$dest_dir"
echo $rsync_cmd
echo "Dotfiles backup complete!"
}
# Function to restore dotfiles backup
restore_dotfiles() {
# Accept backup directory and destination directory as parameters
backup_dir="$1"
restore_dir="$2"
backup_dotfiles "$restore_dir" "./bkp/$restore_dir.bkp"
# Perform the restore using rsync
rsync \
--files-from="./sources/.file_list" \
--exclude-from="./sources/.ex_list" \
-$rsync_default_options \
--no-perms \
"$backup_dir" "$restore_dir"
echo "Dotfiles restore complete!"
}
# Function for backup subcommand
do_backup() {
echo "Performing backup..."
echo "opts $rsync_default_options"
if [ ! $quiet ];
then
echo "continue? y/n"
read -n 1 -r
if [[ $REPLY == [nN]$ ]]
then
exit 1
fi
fi
# Add backup logic here
pre_process;
backup_dotfiles $HOME/ ./sources/root/ ;
post_process;
}
# Function for update subcommand
do_update() {
echo "deperectated, use backup..."
# Add update logic here
# pre_process;
# backup_dotfiles ./sources/root $HOME;
# post_process ;
}
# Function for restore subcommand
do_restore() {
echo "Performing restore..."
# Add restore logic here
echo "opts $rsync_default_options"
pre_process;
restore_dotfiles ./sources/root/ $HOME ;
post_process;
}
post_process(){
if $install_omz; then
echo "Installing Oh My Zsh"
# Add your installation logic here
zsh -c "./config-ohmyzsh.sh"
fi
}
# function for help
usage(){
echo "usage havent implement yet"
}
#------------------------------------------------------------------------------
# Default values for options
quiet=false
verbose=false
dry_run=false
phase_args(){
# Args handding
while getopts ":hvqd-" opt; do # Go through the options
case $opt in
h ) # Help
usage
exit 0 # Exit correctly
;;
v ) # Debug
echo "Read verbose flag"
verbose=true
;;
q | -quiet )
quiet=true
;;
d | -dry )
dry_run=true
;;
- )
case "${OPTARG}" in
help )
usage
exit 0
;;
quiet )
quiet=true
;;
install-omz )
install_omz=true
;;
* )
echo "Invalid option: --${OPTARG}"
usage
exit 1
;;
esac
;;
: )
echo "Option requires an argument: -$OPTARG"
usage
exit 1
;;
? ) # Invalid option
echo "[ERROR]: Invalid option: -${OPTARG}"
usage
exit 1 # Exit with erro
;;
esac
done
echo "dry_run: $dry_run"
if $dry_run; then
rsync_default_options+="n"
fi
}
if [ ! 0 == $# ] # If options provided then
then
subcommand=$1; shift # Get subcommand and shift to next option
# subcmd handling
case "$subcommand" in
backup )
unset OPTIND # in order to make -v pow -a <arg> -f <arg> work -> https://stackoverflow.com/questions/2189281/how-to-call-getopts-in-bash-multiple-times
if [ ! 0 == $# ] # if options provided
then
if [ $verbose == true ]; then echo "Remaining args are: <${@}>"; fi
phase_args "$@"
fi
do_backup
;;
update )
do_update
;;
restore )
unset OPTIND # in order to make -v pow -a <arg> -f <arg> work -> https://stackoverflow.com/questions/2189281/how-to-call-getopts-in-bash-multiple-times
if [ ! 0 == $# ] # if options provided
then
if [ $verbose == true ]; then echo "Remaining args are: <${@}>"; fi
phase_args "$@"
fi
do_restore
;;
* ) # Invalid subcommand
if [ ! -z $subcommand ]; then # Don't show if no subcommand provided
echo "Invalid subcommand: $subcommand"
fi
usage
exit 1 # Exit with error
;;
esac
else # else if no options provided throw error
usage
exit 1
fi