-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup_sdcard
executable file
·171 lines (145 loc) · 4.48 KB
/
backup_sdcard
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
#!/bin/bash
if [ "$(id -u)" != "0" ]; then
echo "Script must be run as root !"
exit 0
fi
echo ""
date
echo "***************************************************"
echo "* Backup Odroid C1 Linux from sdcard to directory *"
echo "***************************************************"
echo ""
if [ "${1}" = "" ]; then
echo "sdcard not specified."
echo "USAGE: backup_sdcard </dev/sdX> <backup_directory>"
exit 0
fi
if [ "${2}" = "" ]; then
echo "Directory not specified."
echo "USAGE: backup_sdcard </dev/sdX> <backup_directory>"
exit 0
fi
sdcard=$1
bkpdir=$2
if [ -b $sdcard ]; then
# we are working with block device
# Test if requested drive is removable
ISREMOVABLE=`udevadm info -a -n ${sdcard} | grep -o "ATTR{removable}==\"1\""`
if [ ! "${ISREMOVABLE}" = "ATTR{removable}==\"1\"" ] ; then
echo "${sdcard} IS NOT REMOVABLE DRIVE !, Exiting."
exit 1
fi
_sdok=`fdisk -l $sdcard 2> /dev/null | grep Disk`
if [ "$_sdok" = "" ]; then
echo "${sdcard} NOT FOUND !, Exiting."
exit 1
fi
else
echo "${sdcard} NOT FOUND !, Exiting."
exit 1
fi
umount ${sdcard}* > /dev/null 2>&1
sleep 1
mkdir -p ${bkpdir} > /dev/null 2>&1
#=======================================================================
# Calculate partitions offsets and sizes
echo ""
echo "Analyzing sd card ..."
sdcard_part=`fdisk -l $sdcard | grep Linux | awk '{print $1}'`
sdcard_sect=`fdisk -l $sdcard | grep "Disk $sdcard" | awk '{print $7}'`
if [ "${sdcard_sect}" = "" ]; then
sdcard_sect=`fdisk -l $sdcard | grep total | awk '{print $8}'`
fi
sdcard_end=$(expr $sdcard_sect - 1024)
fat=`fdisk -l $sdcard | grep FAT`
if [ "${fat}" = "" ]; then
linuxpart="1"
else
linuxpart="2"
fi
if [ "${linuxpart}" = "2" ]; then
fat_start=`fdisk -l $sdcard | grep ${sdcard}1 | awk '{print $2}'`
fat_end=`fdisk -l $sdcard | grep ${sdcard}1 | awk '{print $3}'`
fat_size=$(( ($fat_end - $fat_start + 1) / 2048 ))
linux_start=`fdisk -l $sdcard | grep ${sdcard}2 | awk '{print $2}'`
linux_end=`fdisk -l $sdcard | grep ${sdcard}2 | awk '{print $3}'`
linux_size=$(( ($linux_end - $linux_start + 1) / 2048 ))
else
linux_start=`fdisk -l $sdcard | grep ${sdcard}1 | awk '{print $2}'`
linux_end=`fdisk -l $sdcard | grep ${sdcard}1 | awk '{print $3}'`
linux_size=$(( ($linux_end - $linux_start + 1) / 2048 ))
fi
echo ""
echo " SDCard size: $sdcard_sect blocks, $(expr $sdcard_sect / 2048 ) M"
echo "------------------------------------------------"
if [ "${linuxpart}" = "2" ]; then
printf "%14s" "FAT part:"; printf "%12s" $fat_start; printf "%12s" $fat_end; printf "%10s\n" "$fat_size M"
fi
printf "%14s" "linux part:"; printf "%12s" $linux_start; printf "%12s" $linux_end; printf "%10s\n" "$linux_size M"
echo "------------------------------------------------"
echo ""
if [ "${linuxpart}" = "2" ]; then
if [ "${fat_start}" = "" ] || [ "${linux_start}" = "" ]; then
echo "Bad SDCard partition structure !"
exit 1
fi
if [ ! "${fat_start}" = "3072" ]; then
echo "Bad SDCard partition structure (start) !"
exit 1
fi
else
if [ "${linux_start}" = "" ]; then
echo "Bad SDCard partition structure !"
exit 1
fi
if [ ! "${linux_start}" = "3072" ]; then
echo "Bad SDCard partition structure (start)!"
exit 1
fi
echo "SDCard is ext4 only card!"
fi
mkdir _mnt > /dev/null 2>&1
umount _mnt > /dev/null 2>&1
mkdir _mnt1 > /dev/null 2>&1
umount _mnt1 > /dev/null 2>&1
echo ""
echo -n "WARNING: Linux files in $bkpdir WILL BE UPDATED !, Continue (y/N)? "
read -n 1 ANSWER
if [ ! "${ANSWER}" = "y" ] ; then
echo "."
echo "Canceled.."
exit 0
fi
echo ""
# EXTRACTING FILES
echo ""
if [ "${linuxpart}" = "2" ]; then
mkdir ${bkpdir}-fat > /dev/null 2>&1
mount ${sdcard}1 _mnt > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR mounting ${sdcard}2"
exit 1
fi
echo "Copying FAT partition ..."
rsync -r -t -p -o -g -x --delete -l -H -D --numeric-ids -s --stats _mnt/ ${bkpdir}-fat/ > /dev/null 2>&1
sync
fs_size=`du -s -h ${bkpdir}-fat | awk '{print $1}'`
echo "File system size: $fs_size"
umount _mnt
fi
mkdir ${bkpdir} > /dev/null 2>&1
mount ${sdcard}${linuxpart} _mnt > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "ERROR mounting ${sdcard}${linuxpart}"
exit 1
fi
echo ""
echo "Copying linux partition ..."
rsync -r -t -p -o -g -x --delete -l -H -D --numeric-ids -s --stats _mnt/ ${bkpdir}/ > /dev/null 2>&1
sync
fs_size=`du -s -h ${bkpdir} | awk '{print $1}'`
echo "File system size: $fs_size"
umount _mnt
sync
echo "OK."
# -------------------------------------------------------------------