Subversion Repositories livecd

Rev

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

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