summaryrefslogtreecommitdiffstats
path: root/rescue-Flash/scripts/restore_systemloop
blob: 39c56e03080ec4691997d538c320196fedc4799f (plain)
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
#!/bin/bash

# import functions library
source rescue_common

tmpdir="/tmp/flash-rescue-root"
rootdir="$tmpdir/pen"

function prepare() {

    mkdir -p $rootdir

    if ! mount_usbroot $rootdir; then
        return 1
    fi

    if [ x"$version" = x"1.0" ]; then
        mkdir -p $tmpdir/shared
        if ! mount_sharedroot $tmpdir/shared; then
            return 1
        fi
    fi

    return 0
}

function doit() {

    if [ ! -d $dir ]; then
        echo
        echo "Can't find a backup directory in the Share partition"
        echo
        return 0
    fi

    cd $dir
    i=1
    for file in $(ls -c1 backup-*.bz2 2> /dev/null | sort -r | head -10); do
            files[$i]="$file"
            let i++
    done
    cd - > /dev/null

    if [ $i -eq 1 ]; then
        echo
        echo 'No backup files found!'
        echo
        return 0
    fi

    while [ true ]; do

        clear
        echo
        echo "The following backup files were found:"
        echo

        i=1
        while [ -n "${files[$i]}" ]; do
            echo "[$i] ${files[$i]}"
            let i++
        done
        let i--
        echo
        echo '[0] Cancel / Return to main menu'

        echo
        echo 'Select from the list above, the file that you want to restore.'
        echo 'The most recent files come first in the list.'
        echo
        echo -n "What backup file do you want to restore? [1] "
        read option

        if [ -z "$option" ]; then
            option=1
        fi
        if [ x"$option" = x"0" ]; then
            return 0
        fi

        if [ ! -f $dir/${files[$option]} ]; then
            echo
            echo "Error reading backup file ${files[$option]}!"
            echo
            echo -n 'Do you want to restore another backup file? [N/y] '
            read confirm

            if [ x"$confirm" = x"y" -o x"$confirm" = x"Y" ]; then
                continue
            else
                return 0
            fi
        else

            echo
            echo -n 'Restoring backup file. This can take some time:  '

            bunzip2 -c $dir/${files[$option]} > $loop \
                2> /tmp/rescue-backup.err &

            sleep 2

            while ps | grep -q bunzip2; do
                progress
            done

            sync
            echo
            echo

            errsize=$(ls -la /tmp/rescue-backup.err | cut -d' ' -f5)
            if [ $errsize -ne 0 ]; then
                echo 'Error restoring user files!'
                echo
                return 1
            else
                echo 'Backup file restored!'
                echo
                break
            fi
        fi
    done

    return 0
}

function cleanup() {

    if [ x"$version" = x"1.0" ]; then
        umount $tmpdir/shared > /dev/null 2>&1
        rmdir $tmpdir/shared 2> /dev/null
    fi

    umount $rootdir > /dev/null 2>&1
    rmdir $rootdir $tmpdir 2> /dev/null

    return 0
}

clear
trap cleanup SIGINT

version=""
if ! insert_pendrive; then
    exit 1
fi

if [ x"$version" = x"1.0" ]; then
    dir="$tmpdir/shared/backup"
    loop="$rootdir/loopbacks/system.loop"
else
    dir="$rootdir/backup"
    loop="$rootdir/.loopbacks/system.loop"
fi

if ! prepare; then
    cleanup
    exit 1
fi

if ! doit; then
    cleanup
    exit 1
fi

cleanup
exit 0