-
Notifications
You must be signed in to change notification settings - Fork 1
/
crab
executable file
·250 lines (216 loc) · 6.74 KB
/
crab
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
#!/bin/bash
INIT_PAR="#Specify the file extension to add to the backup
file_type='db';
#Specify the directory with the original files
origin_folder=$(pwd)
#Specify the destination folder where your backups will be stored (use abslute path)
destination_folder=$(pwd)
#Specify the maximum number of backups to store (default: 5)
number_backups=5
#Specify text-editor (default: vim)
text_editor=vim"
function help {
echo "
---------------------------------------------
| _____ ______ ___ ______ |
| / __ \| ___ \ / _ \ | ___ \ |
| | / \/| |_/ // /_\ \| |_/ / |
| | | | / | _ || ___ \ |
| | \__/\| |\ \ | | | || |_/ / |
| \____/\_| \_|\_| |_/\____/ |
| |
| CRAB - Create Regular Amiable Backup |
---------------------------------------------
usage <\$path_to_script>/crab [-v | --version]
[-h | --help]
[-i | --init]
[-m | --make]
[-c | --config]
[-e | --ecron]
[--set-text-editor] PARAM
[--set-number-backups] PARAM
[--set-file-type] PARAM
[--set-destination-folder] PARAM
---------------------------------------------
-v --version (show version of the script)
-h --help (show this tutorial)
-i --init (create crabrc file in dirrectory for backup
-m --make (create backup manually)
-c --config (open text editor to rewrite configuration file)
-e --ecron (change crontab config)
--set-text-editor (change text editor (default: vim))
--set-number-backups (change number backups (default: 5))
--set-file-type (set type of files to save in backups)
--set-destination-folder (set the path where backups will be stored)
--set-origin-folder (set the path where the original files are stored)
"
}
function check_utils {
. ~/.crabrc || text_editor='vim'
if [ -z $(which $text_editor) ]
then
echo "Please install $text_editor or specify text-editor by using 'crab --set-text-editor <editor_name>'"
exit
fi
if [ -z $(which zip) ]
then
echo "To use CRAB you should install zip util"
exit
fi
if [ -z $(which unzip) ]
then
echo "To use CRAB you shold install unzip"
exit
fi
}
function change_config {
echo "#Specify the file extension to add to the backup
file_type=$file_type
#Specify the destination folder where your backups will be stored (use abslute path)
destination_folder=$destination_folder
#Specify the directory with the original files
origin_folder=$origin_folder
#Specify the maximum number of backups to store (default: 5)
number_backups=$number_backups
#Specify text-editor (default: vim)
text_editor=$text_editor" > ~/.crabrc
exit
}
function set_text_editor {
if [ -n "$1" ]
then
. ~/.crabrc || (echo Try to use 'crab -i' && exit)
text_editor=$1
change_config
else
help
fi
}
function set_number_backups {
if [ -n "$1" ]
then
. ~/.crabrc || (echo Try to use 'crab -i' && exit)
number_backups=$1
change_config
else
help
fi
}
function set_file_type {
if [ -n "$1" ]
then
. ~/.crabrc || (echo Try to use 'crab -i' && exit)
file_type=$1
change_config
else
help
fi
}
function set_destination_folder {
if [ -n "$1" ]
then
. ~/.crabrc || (echo Try to use 'crab -i' && exit)
destination_folder=$1
change_config
else
help
fi
}
function set_origin_folder {
if [ -n "$1" ]
then
. ~/.crabrc || (echo Try to use 'crab -i' && exit)
origin_folder=$1
change_config
else
help
fi
}
function init {
echo "$INIT_PAR" > ~/.crabrc
check_utils || exit
echo -e "\e[5m Create .crabrc file... \e[0m"
sleep 3s
. ~/.crabrc
$text_editor ~/.crabrc
}
function make_backup {
if [[ -f ~/.crabrc ]]
then
. ~/.crabrc
check_utils
echo "Get to work, guys! Create a backup!"
archive="backup-$(date +%Y-%m-%d:%H:%M:%S)"
EXT=$file_type
mkdir -p "$destination_folder"
check_count_files
zip -r "$destination_folder/$archive.zip" $(ls $origin_folder | grep "$EXT$" | awk -v dir=$origin_folder/ '{ print dir $0 }') >> /dev/null
if [[ $(unzip -tq "$destination_folder/$archive.zip") == "No"* ]]
then
echo "Saving..."
else
unzip -tq "$destination_folder/$archive.zip"
echo "It seems that the backup was broken...I'll delete it now, and you try to check your files, then I'll try again."
rm "$destination_folder/$archive.zip"
fi
else
echo "You'll never learn! Maybe you can use '.crab -i'?"
fi
}
function edit_crontab {
echo 'Please edit your crontab to change periodicity of making backups'
read -r -p "Are you sure? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY]|[дД][аА]|[дД])$ ]]
then
crontab -e
else
echo "Ahh, here we go again... And when will you stop bothering me unnecessarily?"
fi
}
function version {
echo -e "\e[35;1;5;101mC\e[37;102mR\e[36;105mA\e[39;107mB\e[0m" v.1.1.2
}
function check_count_files {
. ~/.crabrc
cur_number=$(ls "$destination_folder" | grep -c 'backup-.*.zip')
(( cur_number=$cur_number-$number_backups+1 ))
for ((i=0; i<$cur_number;i++))
do
rm "$destination_folder/$(ls "$destination_folder" | grep 'backup-.*.zip' | sort | grep --max-count=1 "backup")"
echo "The oldest backup has been deleted"
done
}
function write_config {
. ~/.crabrc || text_editor='vim'
check_utils
$text_editor ~/.crabrc
}
if [ -n "$1" ]
then
while [ -n "$1" ]
do
case "$1" in
-i) init;;
--init) init;;
-c) write_config;;
--config) write_config;;
-e) edit_crontab;;
--ecron) edit_crontab;;
-h) help;;
--help) help;;
-m) make_backup;;
--make) make_backup;;
-v) version;;
--version) version;;
--set-text-editor) set_text_editor "$2";;
--set-number-backups) set_number_backups "$2";;
--set-file-type) set_file_type "$2";;
--set-destination-folder) set_destination_folder "$2";;
--set-origin-folder) set_origin_folder "$2";;
*) help;;
esac
shift
done
else
help
fi