-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeeper.sh
executable file
·310 lines (291 loc) · 8.02 KB
/
keeper.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
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
#!/usr/bin/env -S bash -e
# --- Do not touch section
config_backup_profile='default'
config_no_confirm=0
# put the script directory in 'SCRIPT_DIR' (symlink safe way)
SOURCE=${BASH_SOURCE[0]}
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )
SOURCE=$(readlink "$SOURCE")
[[ $SOURCE != /* ]] && SOURCE=$DIR/$SOURCE # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
export SCRIPT_DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )
# source lib
source $SCRIPT_DIR/.lib.sh
# --- Functions and global variables section
TMP_DIR=""
# @brief adds a path to the $TMP_DIR
# @param $1 path to add
# @param $2 place to add the path at
# @needed '$TMP_DIR'
add_path_to_tmp_dir ()
{
local pathFrom="$1"
local pathTo="$2"
add_path_to_dest_at "$pathFrom" "$TMP_DIR/$pathTo"
}
# @brief adds a list of paths to the $TMP_DIR
# @param $1 path to take the list from
# @param $2 place to add the paths at
# @param $@ list of paths to add
# @needed 'add_path_to_tmp_dir'
add_entries_to_archive()
{
local dirFrom=$1; shift
local dirTo=$1; shift
local paths=("$@")
for entrie in "${paths[@]}"; do
add_path_to_tmp_dir "$dirFrom/$entrie" "$dirTo/$entrie"
done
}
# @brief a tmp function that will be replaced by the real one from the backup profile
add_profile_backup ()
{
echo_error_msg "No profile was loaded, exisiting"
exit
}
# @brief genereate a info file about the archive and echo the path to it
# @param $1 message to include in the info file
generate_info_file ()
{
local message="$1"
local dirForInfo=$(generate_tmp_dir)
touch "$dirForInfo/archive.info"
local archiveinfoPath="$dirForInfo/archive.info"
cat > $archiveinfoPath <<EOF
# default archive.info file
creator='$USER'
date_archive_created='$(date +%Y-%m-%d)'
doc='backup made by $USER on $HOSTNAME'
# Preview message
$message
EOF
echo $archiveinfoPath
}
# @brief initializes archive with the archive.info file
# @param $1 path to the wanted archive
# @param $2 message to include in the info file
create_archive ()
{
local archivePath="$1"
local archive_info_message="$2"
local infoFile=$(generate_info_file "$archive_info_message")
make_archive_with_first_file "$archivePath" "$infoFile"
rm -rf $(dirname $infoFile)
}
# @brief echos the archive.info file from the archive
# @param $1 path to the archive
preview_archive_details ()
{
source_archive_info "$1"
echo -e "
$doc
on $date_archive_created
"
display_file_from_archive "$1" 'archive.info' | sed -n '5,$p'
}
run_backup ()
{
local pathToStartFrom="$1"
local archivePath="$2"
local info_message="$3"
archivePath=$(get_full_path "$archivePath")
local currentDir=$(pwd)
cd $pathToStartFrom
TMP_DIR=$(generate_tmp_dir)
create_archive $archivePath "$info_message"
# backup
backup_profile $config_backup_profile
# add all the files in $TMP_DIR to the archive
add_path_to_archive "$archivePath" "$TMP_DIR"
rm -rf $TMP_DIR
cd $currentDir
}
run_restore ()
{
local archivePath=$1
local destPath=$2
archivePath=$(get_full_path "$archivePath")
# restore
restore_archive "$archivePath" "$destPath"
source_archive_info "$archivePath"
# --sed-home-path
if [[ "$sed_home_path" -eq "1" ]]; then
echo_msg "Sed home path:"
if [[ -z "$creator" ]]; then
echo_error_msg "No creator was found in the archive.info file"
elif [[ "$creator" == "$USER" ]]; then
echo_warning_msg "The archive was made by a creator named $creator, so no changes will be made"
else
echo_warning_msg "replacing $creator with $USER on all archive files in $destPath"
list_archive_contents "$archivePath" | while read file; do
if [[ -f "$destPath"/"$file" ]]; then
sed -i "s|/home/$creator|/home/$USER|g" "$destPath"/"$file"
fi
done
fi
fi
}
# --- Main section
ARGC=$#
choose="" # can be either 'backup' or 'restore'
pathFrom=""
pathTo=""
archive_info_msg="No message to display"
sed_home_path=0
to_preview=0
dont_run=0
while [ $# -gt 0 ]; do
case $1 in
-h | --help)
help_message
exit
;;
--profiles)
list_profiles
exit
;;
--no-confirm)
config_no_confirm=1
shift
;;
--no-color)
NO_COLOR=1
bash_lib_define_colors
shift
;;
-b | --backup)
if [[ $choose == "restore" ]]; then
echo_error_msg "Can't do a backup and a restore at the same time"
exit
fi
choose="backup"
shift
;;
-r | --restore)
if [[ $choose == "backup" ]]; then
echo_error_msg "Can't do a backup and a restore at the same time"
exit
fi
choose="restore"
shift
;;
--sed-home-path)
sed_home_path=1
shift
;;
--dont-run)
dont_run=1
shift
;;
--preview)
to_preview=1
shift
;;
--only-preview)
to_preview=1
dont_run=1
shift
;;
--profile)
config_backup_profile="$2"
shift 2 # shift 2 times to get rid of the option's value
;;
-f | --from)
pathFrom="$2"
shift 2 # shift 2 times to get rid of the option's value
;;
-t | --to)
pathTo="$2"
shift 2 # shift 2 times to get rid of the option's value
;;
-m | --message)
archive_info_message="$2"
shift 2 # shift 2 times to get rid of the option's value
;;
--message-file)
archive_info_message="$(cat $2)" 1> /dev/null && cat_failed=0 || cat_failed=1
[[ $cat_failed -eq 1 ]] && echo_error_msg "Somthing went wrong when getting the file for the message, check the output above"
[[ $cat_failed -eq 1 ]] && exit
shift 2 # shift 2 times to get rid of the option's value
;;
-- )
echo "reached end of options"
shift
break
;;
*)
echo_error_msg "Unknown option [$1]"
help_message
exit
;;
esac
done
case $choose in
backup)
[ -z "$pathTo" ] && echo_error_msg "No path to backup to was given" && help_message && exit
[[ ! $pathTo =~ \.(zip|tar.gz) ]] && echo_error_msg "Archive type not supported, must be .zip or .tar.gz" && help_message && exit
[[ -d "$(dirname $pathTo)" ]] || mkdir -p "$(dirname $pathTo)"
# 'from' is optional
if [ -z "$pathFrom" ]; then
echo_warning_msg "Note: No path to start from was given, starting from current directory [$PWD]"
pathFrom="$PWD"
else
if [[ ! -d $pathFrom ]]; then
echo_error_msg "$pathFrom is not a directory"
help_message
exit
fi
fi
echo_ok_msg "Backup detected and all options are valid"
;;
restore)
[ -z "$pathFrom" ] && echo_error_msg "No path to restore from was given" && help_message && exit
[ ! -f "$pathFrom" ] && echo_error_msg "$pathFrom is not a file [required by restore option]" && help_message && exit
[ $(check_if_archive_is_valid "$pathFrom") ] || ( echo_error_msg "$pathFrom is not a valid archive to restore from [Note: an archive must contain certain things, look at the README.md]" && exit )
# 'to' is optional
if [ -z "$pathTo" ]; then
echo_warning_msg "Note: No path to restore to was given, restoring to current directory [$PWD]"
pathTo="$PWD"
else
if [[ ! -d $pathTo ]]; then
echo_error_msg "$pathTo is not a directory"
help_message
exit
fi
fi
echo_ok_msg "Restore detected and all options are valid"
;;
*)
help_message
exit
;;
esac
if [[ ! -f "$SCRIPT_DIR"/profiles/"$config_backup_profile" ]]; then
echo_error_msg "$config_backup_profile does not exist inside $SCRIPT_DIR/profiles/"
list_profiles
exit
fi
pathFrom=$(get_full_path "$pathFrom")
pathTo=$(get_full_path "$pathTo")
if [[ $choose == "backup" ]]; then
echo_msg "will backup to: $pathTo from $pathFrom"
elif [[ $choose == "restore" ]]; then
echo_msg "will restore from: $pathFrom to $pathTo"
if [[ $sed_home_path == 1 ]]; then
echo_msg "will replace the old home path with the new one (if they are different)"
fi
if [[ $to_preview == 1 ]]; then
echo_msg "Preview details:"
printf "${BLUE}"
preview_archive_details "$pathFrom"
printf "${NC}"
fi
fi
[[ $dont_run == 1 ]] && exit
[ $config_no_confirm == 0 ] && wait_for_any_key_press "press [ANY KEY] to continue.. "
if [[ $choose == "backup" ]]; then
run_backup "$pathFrom" "$pathTo" "$archive_info_message"
elif [[ $choose == "restore" ]]; then
run_restore "$pathFrom" "$pathTo"
fi