Subversion Repositories livecd

Rev

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

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