Subversion Repositories livecd

Rev

Blame | Last modification | View Log | Download | RSS feed

#!/bin/bash
#
# Saves data to a usbstick (default) 
# or a partition (user input)
# Scientific Linux Live CD
#
# Urs Beyerle, PSI
#

### definitions
CONFIG_FOLDER=""
MOUNTDIR=livecd
. /$MOUNTDIR/live/liblinuxlive

# PSI setup?
PSI=$( cmdline_parameter psi )

if [ $PSI ]; then
    SAVEFOLDER=PSI_LIVECD
else
    SAVEFOLDER=SL_LIVECD
fi

echo 

### this script should be run as root 
#   (if not, somethings will not be stored)
if [ "$( whoami )" != "root" ]; then
    echo "********************************************"
    echo "WARNING: Run this script as root or sudo !!"
    echo "As normal user not everything will be saved."
    echo "Run instead: # sudo $0"
    echo "********************************************"
    echo
    echo "Press a key to continue (CTRL-C to exit)"
    read -n 1
fi

### search usb sticks
echo "Search for usbstick..."
USBMOUNT=$( grep -i /media/usb /etc/fstab | tail -n 1 | awk '{ print $2 }' )
if [ $USBMOUNT ]; then
    # mount usbstick, if not already mounted
    mount | grep -q " $USBMOUNT " 
    if [ "$?" != "0" ]; then
        echo "Mount $USBMOUNT"
        mount $USBMOUNT 2>/dev/null
    fi
fi

### if no usbstick, ask the user for a partition
if [ ! $USBMOUNT ]; then
    echo "No usbstick found."
    echo "Type in a partition you want to store the data to."
    echo -n "(e.g. /dev/hda1, /dev/sda1, ...): "
    read MOUNT_DEV
    # search $MOUNT_DEV in fstab
    USBMOUNT=$( grep "^$MOUNT_DEV " /etc/fstab | tail -n 1 | awk '{ print $2 }' )
    if [ ! $USBMOUNT ]; then
        mkdir -p /mnt/$MOUNT_DEV
        USBMOUNT=/mnt/$MOUNT_DEV
    fi
    # mount read write 
    mount -rw $MOUNT_DEV $USBMOUNT 2>/dev/null
    if [ "$?" != "0" ]; then
        echo "ERROR: Can not mount $MOUNT_DEV to $USBMOUNT"
        sleep 5; exit 1
    else
        echo "Mounted $MOUNT_DEV to $USBMOUNT."
    fi
fi

### search for $SAVEFOLDER on $USBMOUNT
if [ -d $USBMOUNT/$SAVEFOLDER ]; then
    CONFIG_FOLDER=$USBMOUNT/$SAVEFOLDER
    echo "Found folder '$SAVEFOLDER' on $USBMOUNT."
else
    mkdir -p $USBMOUNT/$SAVEFOLDER
    if [ "$?" = "0" ]; then
        echo "Folder $SAVEFOLDER created on $USBMOUNT."
        CONFIG_FOLDER=$USBMOUNT/$SAVEFOLDER
    else
        echo "ERROR: Can not write to $USBMOUNT."
        CONFIG_FOLDER=""
    fi
fi

### Create README.txt in $CONFIG_FOLDER
echo "Do not delete this folder!" > $CONFIG_FOLDER/README.txt
echo "It is used to store your data from LiveCD." >> $CONFIG_FOLDER/README.txt
if [ "$?" = "0" ]; then
    echo "Saved $CONFIG_FOLDER/README.txt"
else
    echo "ERROR: Could not save $CONFIG_FOLDER/README.txt !"
fi

### put parameter "keyboard" to config (if not yet there)
CONFIG_FILE=$CONFIG_FOLDER/config
grep -q "KEYBOARD=" $CONFIG_FILE 2>/dev/null
if [ "$?" != "0" ]; then
    # what keyborad do I have?
    . /etc/sysconfig/keyboard
    echo KEYBOARD=$KEYTABLE >> $CONFIG_FILE
    echo "Saved $CONFIG_FILE"
fi

### put restore script to setup (if setup does not yet exists)
SETUP_FILE=$CONFIG_FOLDER/setup
if [ ! -e $SETUP_FILE ]; then
    cat > $SETUP_FILE <<'EOF'
#!/bin/bash
#
# restore tar.gz files, if found on usbstick
files=$( ls $CONFIG_FOLDER/*.tar.gz )
cd /
for file in $files; do
    tar xfzP $file 2>/dev/null
    if [ "$?" = "0" ]; then
            echo "$file restored."
    fi
done
EOF
    echo "Saved $SETUP_FILE"
fi

### save /home, /etc/passwd /etc/shadow, ...
#   as tar.gz. files
echo "Start saving tar.gz files to $CONFIG_FOLDER:"
SAVES="/home \
       /root \
       /etc/passwd \
       /etc/shadow \
       /etc/group"

for save in $SAVES; do
    echo -n "Save $save ... "
    tar cfzP $CONFIG_FOLDER/$( basename $save ).tar.gz $save
    if [ "$?" = "0" ]; then
        echo " done."
    else
        echo "ERROR: Could not save $save to $USBMOUNT !"
    fi
done

### save /etc/X11/xorg.conf
cp -a /etc/X11/xorg.conf $CONFIG_FOLDER/
echo "Xserver configuration file 'xorg.conf' saved to $CONFIG_FOLDER." 

### inform user
echo "If you don't want to keep one of these config files, just delete it."
echo

### umount $USBMOUNT
echo "Unmount $USBMOUNT"
umount $USBMOUNT 2>/dev/null
echo "$0 finished."
echo
echo "Please <press a key> ..."
read -n 1