This repository has been archived by the owner on Sep 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
restore_config.sh
executable file
·83 lines (72 loc) · 1.85 KB
/
restore_config.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
#!/usr/bin/env bash
. build_env.sh
MNT_ROOT=$(mktemp -d)
help() {
usage
echo
echo " -i IMAGE The image file where the configuration should be applied."
echo " -c CONFIG The *.tar.gz file that contains the OpenWRT configuration."
echo
exit 0
}
usage() {
echo "Usage: $0 [ -i IMAGE ] [ -c CONFIG ]" 1>&2
}
while getopts ":i:c:h" options; do
echo "$options"
case "${options}" in
i)
IMAGE=${OPTARG}
if [ ! -f "$IMAGE" ]; then
echo "ERROR: The file '$IMAGE' does not exist!" 1>&2
exit 1
fi
pcapdir_set=true
;;
c)
CONFIG=($OPTARG)
if [ ! -f "$CONFIG" ]; then
echo "ERROR: The file '$CONFIG' does not exist!" 1>&2
exit 1
fi
;;
:)
echo "Error: -${OPTARG} requires an argument."
exit 1
;;
h)
help
;;
*)
usage
exit 1
;;
esac
done
if [ $OPTIND -ne 5 ]; then
usage
exit 1
fi
LOOP_DEV=$(get_loopdev "$IMAGE")
# mount the root file system
sudo mount -t ext4 ${LOOP_DEV}p2 "$MNT_ROOT"
# prepare chroot
sudo mount -o bind /dev "$MNT_ROOT"/dev/
sudo mount -t proc proc "$MNT_ROOT"/proc/
sudo mkdir -p "$MNT_ROOT"/tmp/lock
sudo cp /etc/resolv.conf "$MNT_ROOT"/tmp/
sudo cp "$CONFIG" "$MNT_ROOT"/tmp/backup.tar.gz
# execute chroot commands
! sudo chroot "$MNT_ROOT" /bin/sh << "EOT"
/sbin/sysupgrade -r /tmp/backup.tar.gz
/bin/opkg update
/bin/opkg install $(/bin/cat /etc/backup/installed_packages.txt | /bin/sed -E 's/\tunknown//')
/bin/rm -rf /tmp/*
EOT
# unmount all again
sudo umount "$MNT_ROOT"/dev
sudo umount "$MNT_ROOT"/proc
sudo umount "$MNT_ROOT"
rmdir "$MNT_ROOT"
# close the loop device again
sudo losetup -d $LOOP_DEV