Subversion Repositories livecd

Rev

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