-
Notifications
You must be signed in to change notification settings - Fork 0
/
czip.lib
executable file
·173 lines (153 loc) · 4.23 KB
/
czip.lib
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
#!/usr/bin/env bash
#
# ############################################################################
# Project: scripts (none)
# File...: czip.lib
# Created: Saturday, 2023/09/09 - 01:13:02
# Author.: @fbnmtz, (fabiano.matoz@gmail.com)
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# Last Modified: Saturday, 2023/09/09 - 02:34:13
# Modified By..: @fbnmtz, (fabiano.matoz@gmail.com)
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# Version: 0.0.1.47
# ~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~·~·~·~·~·~·~·~~·~·~·~·~·~~·~·~·~·~·~·~·~
# Description:
# >
# ############################################################################
# HISTORY:
#
# shift
# s=${1:-'10'}
_xLIB_CZIP_=true
xrequirements datesh puts pwdgen sed grep cat du awk ls mkdir hostname tr
xsetHome
# set up DIRs
BACKUPS=$APP_HOME/backups/
RESTORES=$APP_HOME/recovery
KEYS=$APP_HOME/.zkey
DEVICE=$(hostname)
for directory in $BACKUPS $RESTORES $KEYS; do
if [ ! -d $directory ]; then
mkdir -p $directory
fi
done
prompt(){
printf "$APP>>[ "; puts r "x" -n; puts "]" -n; read choice
}
file?(){
if [ -f $1 ]; then
echo "ok"
else
echo "x"
fi
}
_create(){
local file
file=$1
name=$(echo $file | tr -s '/' '-')
# generate key
pw=$(pwdgen 30)
filedate="$(datesh | tr -d '/' | tr -d ':')"
filename="$filedate@$DEVICE:$name"
puts "debug"
puts "$file, $pw, $filename"
# compact this file/folder
if zip -P $pw -9 $BACKUPS/$filename.zip -r $file; then
# save key only if zip process was done
# file1
echo "# $filename" > /media/xdb/$filename.zkey
echo $pw >> /media/xdb/$filename.zkey
# file2
echo "# $filename" > $KEYS/$filename.zkey
echo $pw >> $KEYS/$filename.zkey
fi
}
_extract(){
files=($(ls $BACKUPS | tr -s '\n' ' '))
if [ "$1" == "" ]; then
echo "choose file to extract:"
i=-1
for d in ${files[@]}; do
i=$(($i+1))
if [ $(($i%2)) -eq 1 ]; then
current_color=c
else
current_color=B
fi
size=$(du -h $BACKUPS/$d | awk '{print $1}')
puts $current_color " [$i] => $d ($size)"
done
prompt
while [ ! $choice -le $i ] || [ "$choice" == "" ] ; do
echo "invalid number! choose between [0..$i]"
prompt
done
# remove FILE and KEY
pw=$( cat $KEYS$(echo ${files[$choice]} | sed 's/\.zip/\.zkey/g') | grep -v '#' )
echo $pwd
cmd="unzip -P $pw $BACKUPS/${files[$choice]} -d $RESTORES"
echo "$cmd"
$cmd
# rm $KEYS$(echo ${files[$choice]} | sed 's/\.zip/\.zkey/g')
# usage
exit 0
fi
file=$1
# name=$(basename $1)
name=$(echo $file | tr -s '/' '-')
pw="$(cat $KEYS$(echo "$name" | cut -d '.' -f 1 ).zkey | grep -v '#')"
echo "$pw"
unzip -P $pw $1 -d $RESTORES
}
_list(){
i=-1
colors=(c B)
files=($(ls $BACKUPS))
puts "$APP list:"
for f in ${files[@]}; do
i=$(($i+1))
if [ $(($i%2)) -eq 1 ]; then
current_color=c
else
current_color=B
fi
size=$(du -h $BACKUPS/$f | awk '{print $1}')
# echo -e "$f ($size) $s"
puts $current_color " -> $f ($size) [$(file? $KEYS$(echo ${files[$i]} | sed 's/\.zip/\.zkey/g') )]"
done
puts "files count: ${#files[@]} - total size: $(du -h $BACKUPS | awk '{print $1}')"
}
_remove(){
files=($(ls $BACKUPS | tr -s '\n' ' '))
if [ "$1" == "" ]; then
echo "choose file to exclude:"
i=-1
for d in ${files[@]}; do
i=$(($i+1))
if [ $(($i%2)) -eq 1 ]; then
current_color=c
else
current_color=B
fi
size=$(du -h $BACKUPS/$d | awk '{print $1}')
puts $current_color " [$i] => $d ($size)"
done
prompt
while [ ! $choice -le $i ] || [ "$choice" == "" ] ; do
echo "invalid number! choose between [0..$i]"
prompt
done
# remove FILE and KEY
echo "rm $BACKUPS/${files[$choice]}"
rm $BACKUPS/${files[$choice]}
rm $KEYS$(echo ${files[$choice]} | sed 's/\.zip/\.zkey/g')
else
# if para exit check if it's in valid options
if [[ "${files[@]}" =~ "$1" ]]; then
echo "file in"
# command="$command $1"
else
echo "file out"
fi
fi
}