Subversion Repositories livecd

Rev

Rev 1 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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