Subversion Repositories livecd

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 beyerle@PS 1
#!/bin/bash
2
#
3
# Saves data to a usbstick (default) 
4
# or a partition (user input)
5
# Scientific Linux Live CD
6
#
7
# Urs Beyerle, PSI
8
#
9
 
10
### definitions
11
CONFIG_FOLDER=""
12
MOUNTDIR=livecd
13
. /$MOUNTDIR/live/liblinuxlive
14
 
15
# PSI setup?
16
PSI=$( cmdline_parameter psi )
17
 
18
if [ $PSI ]; then
19
    SAVEFOLDER=PSI_LIVECD
20
else
21
    SAVEFOLDER=SL_LIVECD
22
fi
23
 
24
echo 
25
 
26
### this script should be run as root 
27
#   (if not, somethings will not be stored)
28
if [ "$( whoami )" != "root" ]; then
29
    echo "********************************************"
30
    echo "WARNING: Run this script as root or sudo !!"
31
    echo "As normal user not everything will be saved."
32
    echo "Run instead: # sudo $0"
33
    echo "********************************************"
34
    echo
35
    echo "Press a key to continue (CTRL-C to exit)"
36
    read -n 1
37
fi
38
 
39
### search usb sticks
40
echo "Search for usbstick..."
41
USBMOUNT=$( grep -i /media/usb /etc/fstab | tail -n 1 | awk '{ print $2 }' )
42
if [ $USBMOUNT ]; then
43
    # mount usbstick, if not already mounted
44
    mount | grep -q " $USBMOUNT " 
45
    if [ "$?" != "0" ]; then
46
	echo "Mount $USBMOUNT"
47
	mount $USBMOUNT 2>/dev/null
48
    fi
49
fi
50
 
51
### if no usbstick, ask the user for a partition
52
if [ ! $USBMOUNT ]; then
53
    echo "No usbstick found."
54
    echo "Type in a partition you want to store the data to."
55
    echo -n "(e.g. /dev/hda1, /dev/sda1, ...): "
56
    read MOUNT_DEV
57
    # search $MOUNT_DEV in fstab
58
    USBMOUNT=$( grep "^$MOUNT_DEV " /etc/fstab | tail -n 1 | awk '{ print $2 }' )
59
    if [ ! $USBMOUNT ]; then
60
	mkdir -p /mnt/$MOUNT_DEV
61
	USBMOUNT=/mnt/$MOUNT_DEV
62
    fi
63
    # mount read write 
64
    mount -rw $MOUNT_DEV $USBMOUNT 2>/dev/null
65
    if [ "$?" != "0" ]; then
66
	echo "ERROR: Can not mount $MOUNT_DEV to $USBMOUNT"
67
	sleep 5; exit 1
68
    else
69
	echo "Mounted $MOUNT_DEV to $USBMOUNT."
70
    fi
71
fi
72
 
73
### search for $SAVEFOLDER on $USBMOUNT
74
if [ -d $USBMOUNT/$SAVEFOLDER ]; then
75
    CONFIG_FOLDER=$USBMOUNT/$SAVEFOLDER
76
    echo "Found folder '$SAVEFOLDER' on $USBMOUNT."
77
else
78
    mkdir -p $USBMOUNT/$SAVEFOLDER
79
    if [ "$?" = "0" ]; then
80
	echo "Folder $SAVEFOLDER created on $USBMOUNT."
81
	CONFIG_FOLDER=$USBMOUNT/$SAVEFOLDER
82
    else
83
	echo "ERROR: Can not write to $USBMOUNT."
84
	CONFIG_FOLDER=""
85
    fi
86
fi
87
 
88
### Create README.txt in $CONFIG_FOLDER
89
echo "Do not delete this folder!" > $CONFIG_FOLDER/README.txt
90
echo "It is used to store your data from LiveCD." >> $CONFIG_FOLDER/README.txt
91
if [ "$?" = "0" ]; then
92
    echo "Saved $CONFIG_FOLDER/README.txt"
93
else
94
    echo "ERROR: Could not save $CONFIG_FOLDER/README.txt !"
95
fi
96
 
97
### put parameter "keyboard" to config (if not yet there)
98
CONFIG_FILE=$CONFIG_FOLDER/config
99
grep -q "KEYBOARD=" $CONFIG_FILE 2>/dev/null
100
if [ "$?" != "0" ]; then
101
    # what keyborad do I have?
102
    . /etc/sysconfig/keyboard
103
    echo KEYBOARD=$KEYTABLE >> $CONFIG_FILE
104
    echo "Saved $CONFIG_FILE"
105
fi
106
 
107
### put restore script to setup (if setup does not yet exists)
108
SETUP_FILE=$CONFIG_FOLDER/setup
109
if [ ! -e $SETUP_FILE ]; then
110
    cat > $SETUP_FILE <<'EOF'
111
#!/bin/bash
112
#
113
# restore tar.gz files, if found on usbstick
114
files=$( ls $CONFIG_FOLDER/*.tar.gz )
115
cd /
116
for file in $files; do
117
    tar xfzP $file 2>/dev/null
118
    if [ "$?" = "0" ]; then
119
	    echo "$file restored."
120
    fi
121
done
122
EOF
123
    echo "Saved $SETUP_FILE"
124
fi
125
 
126
### save /home, /etc/passwd /etc/shadow, ...
127
#   as tar.gz. files
128
echo "Start saving tar.gz files to $CONFIG_FOLDER:"
129
SAVES="/home \
130
       /root \
131
       /etc/passwd \
132
       /etc/shadow \
133
       /etc/group"
134
 
135
for save in $SAVES; do
136
    echo -n "Save $save ... "
137
    tar cfzP $CONFIG_FOLDER/$( basename $save ).tar.gz $save
138
    if [ "$?" = "0" ]; then
139
	echo " done."
140
    else
141
	echo "ERROR: Could not save $save to $USBMOUNT !"
142
    fi
143
done
144
 
145
### save /etc/X11/xorg.conf
146
cp -a /etc/X11/xorg.conf $CONFIG_FOLDER/
147
echo "Xserver configuration file 'xorg.conf' saved to $CONFIG_FOLDER." 
148
 
149
### inform user
150
echo "If you don't want to keep one of these config files, just delete it."
151
echo
152
 
153
### umount $USBMOUNT
154
echo "Unmount $USBMOUNT"
155
umount $USBMOUNT 2>/dev/null
156
echo "$0 finished."
157
echo
158
echo "Please <press a key> ..."
159
read -n 1