-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbackrest
227 lines (186 loc) · 7.81 KB
/
backrest
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
#!/bin/bash
####################################################
# Backup & Restore
# by s3n0, 2019-2024
####################################################
# shell script for backing up and restoring user-defined files and folders in the Enigma2
####################################################
#BACKUP_DIR="" # leave blank, i.e. empty folder -> means the current directory (specified by Linux-Shell / terminal)
#BACKUP_DIR="/media/hdd/backrest_folder/"
#BACKUP_DIR="/home/root/backrest_folder/"
#BACKUP_DIR="/media/mmc/backrest_folder/"
#BACKUP_DIR="/hdd/backrest_folder/"
BACKUP_DIR="/tmp/backrest_folder/"
##### * WARNING !!! if you use the "/tmp" folder to backup, keep in mind that this is a TEMPORARY folder, created in the RAM-disk,
##### * so after restarting Linux, its contents will be deleted... therefore after backup, please temporarily copy the archive-file to your PC,
##### * and before restoring the data, copy the archive-file back to the backup subfolder in "/tmp"
FILE_PREFIX="e2-backup" # Note: The timestamp is automatically added at the end of the file name (as a file sufix) + the file extension will be set to ".tar.gz"
LIST="
/etc/enigma2/
/usr/share/enigma2/picon/
/media/hdd/picon/
/media/usb/picon/
/media/mmc/picon/
/media/mmc/picon-tpl/
/usr/share/enigma2/MetrixHD/skinparts/MetrixHD-infobar-mod-by-s3n0/
/etc/tuxbox/*.xml
/etc/auto.network
/etc/cron/crontabs/root
/usr/script/
/usr/bin/enigma2_pre_start.sh
/usr/bin/*cam*
/etc/init.d/softcam*
/etc/rc?.d/*softcam*
/etc/tuxbox/config/
/etc/CCcam.cfg
/usr/keys/
/usr/bin/udpxy
/etc/init.d/udpxy
/etc/rc?.d/*udpxy
/etc/init.d/iptv_*
/etc/rc?.d/*iptv_*
"
PLUGINS_TO_RESTORE="
enigma2-plugin-extensions-chocholousek-picons
enigma2-plugin-extensions-youtube
enigma2-plugin-skins-metrix-atv-fhd-icons
enigma2-plugin-systemplugins-fastscan
enigma2-plugin-systemplugins-serviceapp
exteplayer3
zerotier
curl
curlftpfs
htop
"
LIST=$(echo $LIST | tr "\n" " ")
PLUGINS_TO_RESTORE=$(echo $PLUGINS_TO_RESTORE | tr "\n" " ")
####################################################
####################################################
init_4_with_feedback() {
echo "Stopping the Enigma..."
init 4
x=0
while [ "$x" -le 15 ] && [ $(pidof enigma2) ] ; do
x=$(( $x + 1 ))
sleep 1
echo "...$x seconds waiting"
#echo "...Enigma state = "`ps --no-headers -o stat -C enigma2` # debugging output - for testing purpose only
done
if pidof enigma2 > /dev/null 2>&1 ; then
echo "...stopping failed ! the 'backrest' script was aborted !"
exit 1
else
echo "...successfully stopped after $x seconds."
fi
}
init_3_with_feedback() {
echo "Starting the Enigma..."
init 3
x=0
while [ "$x" -le 120 ] && [ -z "$(timeout 1 wget -q -O - http://127.0.0.1/web/powerstate)" ] ; do
x=$(( $x + 1 ))
sleep 1
echo "...$x seconds waiting"
done
echo "...+5 seconds of additional waiting (other Enigma2 modules such as OpenWebif take a long time to start)"
sleep 5; x=$(( $x + 5 ))
echo "...successfully started after $x seconds."
}
do_backup() {
echo "-----------------------"
if [ "$1" != "quickly" ]; then # ignoring the init command to start or stop the Enigma2, if the user choice was '--backup-quickly' argument
# store the Standby power state when the script starts: 5 = to turn the Enigma2 into Standby , 4 = to wake-up the Enigma2 from Standby
wget -q -O - http://127.0.0.1/api/powerstate | grep -iqE '"instandby"\s*:\s*true' && previous_power_state=5 || previous_power_state=4
init_4_with_feedback
echo "-----------------------"
fi
mkdir -p /tmp/backrest_temp
BCKP_FILE_TMP="/tmp/backrest_temp/${FILE_PREFIX}_`date '+%Y-%m-%d_%H-%M-%S'`.tar.gz"
BCKP_FILE_TARGET="${BACKUP_DIR}${FILE_PREFIX}_`date '+%Y-%m-%d_%H-%M-%S'`.tar.gz"
echo "Output archive file name: "
echo " TMP: ${BCKP_FILE_TMP} "
echo " FINAL: ${BCKP_FILE_TARGET}"
echo "-----------------------"
echo "Files and folders to compress:"
echo " LIST: ${LIST}"
echo "-----------------------"
echo "Starting compression..."
[ -z "$BACKUP_DIR" ] || mkdir -p $BACKUP_DIR
#tar --ignore-failed-read --exclude='/etc/enigma2/epg.dat' -czpf $BACKUP_FILENAME $LIST # --ignore-failed-read ...... is unfortunately a unknown argument in `tar` under OpenPLi 7.1
tar --exclude='/etc/enigma2/epg.dat' --exclude='/hdd/epg.dat' --exclude='/media/hdd/epg.dat' -czpf $BCKP_FILE_TMP $LIST
mv -f $BCKP_FILE_TMP $BCKP_FILE_TARGET
echo "...compression done." # [ $? -eq 0 ] && echo "...done." || echo "...failed ! (exit code = $?)"
echo "-----------------------"
echo "List of files in the backup directory:"
ls -l "${BACKUP_DIR}${FILE_PREFIX}"*
echo "-----------------------"
if [ "$1" != "quickly" ]; then # ignoring the init command to start or stop the Enigma2, if the user choice was '--backup-quickly' argument
init_3_with_feedback
echo "Force the Enigma power to its original state : $([ $previous_power_state = 5 ] && echo 'turn the Enigma2 into Standby' || echo 'wake-up the Enigma2 from Standby')"
wget -q -O - http://127.0.0.1/api/powerstate?newstate=$previous_power_state > /dev/null 2>&1
echo "-----------------------"
fi
rm -rf /tmp/backrest_temp
}
do_restore() {
echo "-----------------------"
if ls "${BACKUP_DIR}${FILE_PREFIX}"*.tar.gz > /dev/null 2>&1 ; then
LATEST_FILE=$(ls -1 "${BACKUP_DIR}${FILE_PREFIX}"*.tar.gz | tail -n 1)
init_4_with_feedback
echo "-----------------------"
echo "The latest archive file found in the directory:"
echo " ${LATEST_FILE}"
echo "-----------------------"
echo "Installing required plugins..."
opkg update > /dev/null 2>&1
opkg install $PLUGINS_TO_RESTORE
echo "...done."
echo "-----------------------"
echo "Starting decompression..."
tar -C / --overwrite -xzphf $LATEST_FILE
echo "...done."
echo "-----------------------"
if [ -d "/usr/script" ]; then
echo "Setting the execution rights (attributes) on all files from the '/usr/script' folder."
chmod a+x /usr/script/*
echo "...done."
echo "-----------------------"
fi
echo "Restarting the set-top box..."
echo "(sending the 'init 6' command)"
init 6
echo "...done."
echo "-----------------------"
else
echo "ERROR! No archive files ${FILE_PREFIX}*.tar.gz found in the ${BACKUP_DIR} directory!"
echo "-----------------------"
exit 1
fi
}
####################################################
####################################################
case "$1" in
b|backup|-b|--backup)
do_backup "normal"
;;
bq|backup-quickly|-bq|--backup-quickly)
do_backup "quickly"
;;
r|restore|-r|--restore)
do_restore
;;
*)
echo -e "\nUSAGE:"
echo -e "\t $0 <ARGUMENT>"
echo -e "ARGUMENT:"
echo -e "\t b|backup|-b|--backup"
echo -e "\t\t ...backup user-defined files & folders (Enigma will be stopped during the backup process)"
echo -e "\t bq|backup-quickly|-bq|--backup-quickly"
echo -e "\t\t ...backup user-defined files & folders (Enigma will not be stopped during the backup process)"
echo -e "\t r|restore|-r|--restore"
echo -e "\t\t ...restore user-defined files & folders - latest archive found in directory (set-top-box will restarted !!!)"
echo -e "BACKUP DIR:"
[ -z "$BACKUP_DIR" ] && echo -e "\t $(pwd) (as the current directory)" || echo -e "\t ${BACKUP_DIR}"
;;
esac
exit 0