-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestore.sh
More file actions
executable file
·163 lines (150 loc) · 3.13 KB
/
restore.sh
File metadata and controls
executable file
·163 lines (150 loc) · 3.13 KB
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
#!/bin/bash
#############################################################
#
# script: restore.sh
# written by: marek novotny
# version: 1.1
# date: Sun Jun 13 12:31:00 PDT 2015
# purpose: restore backup tarballs
#
# licence: GPL v2 (only)
# repository: http://www.github.com/marek-novotny
#
#############################################################
sWord="$(echo $1 | sed -e 's/\/$//')"
if (($# != 1)) ; then
echo " Usage: ${0##*/} {search word}"
exit 1
fi
restore()
{
if [[ -d "${originalPath}" ]] && [[ -w "${originalPath}" ]]
then
echo " Target: ${originalPath} exists..."
echo " Stage 1: Renaming existing directory..."
mv "${originalPath}" "${originalPath}.bak"
if (($? == 0))
then
echo " Stage 2: Extracting Tarball..."
tar -xzf "${absTarball}" -C "${HOME}"
if (($? == 0))
then
echo " Stage 3: Removing backup..."
rm -rf "${originalPath}.bak"
if (($? == 0))
then
echo " Stage 4: ${originalPath} restored..."
exit 0
fi
else
mv "${originalPath}.bak" "${originalPath}"
exit 1
fi
fi
else
echo " Restoring ${originalPath}..."
tar -xf "${absTarball}" -C "$HOME"
exit 0
fi
}
confirmRestore()
{
while true;
do
read -p " Write: ${absTarball##*/} -> $HOME (y/n): " mSelect
case ${mSelect} in
[Yy]* )
restore
;;
[Nn]* )
echo " Exiting..."
exit 0
;;
* )
echo " Input Error: Just (y/n): "
;;
esac
done
}
checkStorage()
{
srcSize=$(du -csk "${absTarball}" | tail -n1 | awk '{print $1}')
tgtSize=$(df -k "$HOME" 2> /dev/null | tail -n1 | awk '{print $4}')
if (((srcSize * 3) > tgtSize)) ; then
echo "storage is low --abort"
exit 1
fi
}
checkItemPerm()
{
if [[ ! -r "${absTarball}" ]] ; then
echo " Backup: ${absTarball} cannot be read..."
exit 1
fi
}
chooseItem()
{
PS3=$'\n Please choose an item source: '
select choice in "${items[@]}" ; do
absTarball="${sources}/${sFolder}/${choice}"
dirName="$(echo ${choice} | awk -F'_' '{print $5}' | sed -e 's/\.tar\.gz$//')"
originalPath="${HOME}/${dirName}"
echo " Original path: ${originalPath}"
return 0
done
}
getItems()
{
IFS=$'\n'
cd "${sources}/${sFolder}"
dirListing=($(ls))
items=($(printf "%s\n" "${dirListing[@]}" | grep -i ${sWord} | sort))
if (("${#items[@]}" == 0))
then
echo " Search word: ${sWord} found no hits in ${sFolder}"
exit 1
fi
}
checkPerms()
{
if [[ -d "${sFolder}" ]] && [[ -r "${sFolder}" ]] ; then
return 0
else
echo " Choice: ${sFolder} is not a folder or is unreadable..."
return 1
fi
}
chooseSource()
{
PS3=$'\n Please choose a backup source: '
select choice in "${folders[@]}" ; do
sFolder="${choice}"
return 0
done
}
getSources()
{
sources="$HOME/Backups"
if [[ -d "${sources}" ]] && [[ -r "${sources}" ]] ; then
IFS=$'\n'
cd "${sources}"
folders=($(ls))
if ((${#folders[@]} == 0)) ; then
echo " Folder: ${sources} does not contain any targets..."
return 1
else
return 0
fi
else
echo " Folder: ${sources} does not exist or is unreadable..."
return 1
fi
}
getSources
chooseSource
checkPerms
getItems
chooseItem
checkItemPerm
checkStorage
confirmRestore