summaryrefslogtreecommitdiff
path: root/incremental-complete-backup/backup-tool.sh
blob: e86c5b23cfcad153b2bbd3b5338ebdf1208a0baf (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
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
#!/bin/bash
#***********************************************#
#  Gui tool to create backups of FOSSEE laptop  #
#	to external storage media.              #
#                                               #
#***********************************************#

# functions()
# ###########
# ------------------------------------------------------------#
# selection_menu()                                            #
# shows the menu with two options.                            #
# Parameter: $1(title) $2(option1) $3(option2)                #
# change the value of $result to 1 or 2 according to option   #
# selected.                                                   #
# ------------------------------------------------------------#
selection_menu() {
choice="$(zenity --width=600 --height=200 --list --radiolist --title="$1" --text "<b>Choose :</b> " --hide-header --column "selection"  --column "options" FALSE "$2" FALSE "$3")"
case "${choice}" in
    $2 )
        result="1" # if option1 is selected.
        ;;
    $3 )
        result="2" # if option2 is selected.
        ;;
esac
}
# ------------------------------------------------------------#
# sudoAccess() get the sudo password from user via zenity     #
# window and stores in a global variable ($password).         #
# ------------------------------------------------------------#
sudoAccess() {
# Remove any previous sudo passwords
sudo -K
# In case of wrong password, else condition will fail(return 1)
# and executes from beginning
while true
do
# Get password from user
password=$(zenity --title "Enter your password to continue" --password)
# zenity dialog button 'Cancel' returns 1, and 'Yes' returns 0.
# Check for zenity 'Cancel' option
if [ $? -eq 1 ]
then
exit 0
else
# sending user entered password to 'echo' command to verify
# password, if wrong it will repeat from beginning
echo $password | sudo -S echo "test">/dev/null
if [ $? -eq 0 ]
then
break
fi
fi
done
}
# ------------------------------------------------------------#
# removeSDCARD() Prompt a dialog box asking user to remove    #
# all the external media and after that stores the size of    #
# disk without media on $sizeofDiskBeforeSDCARD               #
# ------------------------------------------------------------#
removeSDCARD() {
# The dialog box below will ask user to remove drive(sdcard)
zenity --question --title "Remove media" \
--text "Please remove your drive(sdcard) if connected,
then press YES to continue"
# Checking the return value of zenity dialog, same as previous function
if [ $? -eq 1 ]
then
exit 0
else
# This will return size of disk without our media
umount /media/$USER/*  # to unmount all external media.
sizeofDiskBeforeSDCARD=$(echo $password | sudo -S sfdisk -s\
| tail -1 | awk '{print $2}')
fi
}
# ------------------------------------------------------------#
# insertSDCARD() Prompt a dialog box asking user to connect   #
# the required media & stores the size of disk after inserting#
# the media on $sizeofDiskAfterSDCARD.                        #
# ------------------------------------------------------------#
insertSDCARD() {
# The dialog box below will ask user to insert sdcard
zenity --question --title "Insert media" --text "Now please insert your drive(sdcard) back,\
then press YES to continue"
# Checking the button selected in zenity dialog
if [ $? -eq 1 ]
then
exit 0
else
# sfdisk prints the total disk space in KB
sizeofDiskAfterSDCARD=$(echo $password | sudo -S sfdisk -s\
| tail -1 | awk '{print $2}')
fi
}
# ------------------------------------------------------------#
# SizeofSDCARD() Prompt a dialog box showing the size of      #
# detected media in GB. After calculating difference of       #
# sizeofDiskBeforeSDCARD and sizeofDiskAfterSDCARD            #
# ------------------------------------------------------------#
SizeofSDCARD() {
# verifying new device by finding difference in size
# before and after insertion
sizeSDCARDKbytes=$(($sizeofDiskAfterSDCARD - $sizeofDiskBeforeSDCARD))
# Converting into GB first
sizeSDCARD=$(echo "scale=2;$sizeSDCARDKbytes/1048576" | bc)
# Converting sizeSDCARD to integer, so as to use in conditional statement,
# if any card is detected it will go inside 'else' statement
if [ $(echo $sizeSDCARD |cut -f 1 -d '.') -le 0 ]
then
zenity --info --title "Info" --text "No media found, please check and restart application"
exit 0
else
zenity --question --title "Info" --text "A device of $sizeSDCARD GB is detected, the size will be less \
than actual size of your device. Would you like to continue? \
Press 'YES' to continue or 'NO' to quit !"
# If 'NO' is selected in zenity dialog then exit
if [ $? -eq 1 ]
then
exit 0
fi
fi
}
# ------------------------------------------------------------#
# formatforIncremental() unmount the mounted partitions,      #
# create new partition table format 1st partition to vfat,    #
# 2nd to ext4 and mount under /mnt                            #
# ------------------------------------------------------------#
formatforIncremental() {
umount /media/$USER/*
echo $password |sudo -S mkdir -p /mnt/boot /mnt/rootfs
echo -e "o\nn\np\n1\n\n+100M\nn\np\n2\n\n\nw"|sudo fdisk /dev/$dev_name  # delete old partition table and creating new
sudo mkfs.vfat /dev/$dev_name*1
sudo mkfs -t ext4 /dev/$dev_name*2
sudo mount -t vfat /dev/$dev_name*1 /mnt/boot -o rw,uid=1000,gid=1000
sudo mount /dev/$dev_name*2 /mnt/rootfs
}
# ------------------------------------------------------------#
# formatforComplete() unmount the mounted partitions, create  #
# new partition table format 1st partition to vfat,           #
#                                                             #
# ------------------------------------------------------------#
formatforComplete() {
umount /media/$USER/*
echo $password |sudo -S mkdir -p /mnt/boot
echo -e "o\nn\np\n1\n\n\nw"|sudo fdisk /dev/$dev_name  # delete old partition table and creating new
sudo mkfs.vfat /dev/$dev_name*1
sudo mount -t vfat /dev/$dev_name*1 /mnt/boot -o rw,uid=1000,gid=1000
}

###################################################################################
# Execution starts here.

zenity --width=600 --height=200 --info --text "You need an 8GB or above external storage device (sdcard /pendrive) to continue"
sudoAccess
removeSDCARD
insertSDCARD
SizeofSDCARD
dev_name=`lsblk |head -2|tail -1|cut -d" " -f1` # tracking device name 
selection_menu "Select Backup mode" "Incremental Backup" "Complete Backup"
case "${result}" in
    "1" ) # Incremental
        selection_menu "Incremental Backup options" "Continue with previous backup storage[if you have a previous incremental backup] " "Create a new backup by formating the storage"
        case "${result}" in
                "1" ) # "Continue with previous backup": check for mac_id matching & proceed to rsync
                        rootfs_path=`mount | grep ext|cut -d" " -f3` # tracking the rootfs mount path
                        mac_id=`cat /sys/class/net/eth0/address`     # macid of the machine
                        if [ "$rootfs_path" == "" ] || [ ! -e $rootfs_path/opt/.Hw_addr.txt  ];
                        then # (no rootfs) or ( rootfs doesn't contain Hw_addr.txt )
                            zenity --width=600 --height=100 --info --text "Your storage media doesnot contain matching backup from this machine"
                            exit
                        elif [ "$mac_id" == "$(cat $rootfs_path/opt/.Hw_addr.txt)" ]; # if macids are matching
                        then
                            echo "match found"
                            sudo rsync -latgrzpo --exclude='/tmp' --exclude='/dev' --exclude='/proc' --exclude='/sys' / $rootfs_path
                        else
                            zenity --width=600 --height=100 --info --text "Your storage media doesnot contain matching backup from this machine"
                            exit
			fi
                        ;;
                "2" ) # "new incremental backup" start new rsync
                        cat /sys/class/net/eth0/address > /opt/.Hw_addr.txt # storing mac_id b4 copy
                        formatforIncremental
                        sudo rsync -latgrzpo /opt/fossee-os/* /mnt/boot/
                        sudo rsync -latgrzpo --exclude='/tmp/*' --exclude='/dev/*' --exclude='/proc/*' --exclude='/sys/*' / /mnt/rootfs/
                        sync
                        echo $password |sudo umount /mnt/* # refresh sudo access
                        sudo rm -rf /mnt/*
                        ;;
        esac
        ;;
    "2" ) # Complete 
        formatforComplete
        sudo rsync -latgrzpo /opt/fossee-os/* /mnt/boot/
        rm -f /etc/udev/rules.d/70-persistent-net.rules
        sudo tar -cpzf /mnt/boot/fossee-os.tar.gz --one-file-system /
        sync
        echo $password |sudo umount /mnt/* # refresh sudo access
        sudo rm -rf /mnt/*
        ;;
esac
zenity --width=600 --height=100 --info --text "Done ! "