Enhanced partition copy script (new & improved!)


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Enhanced partition copy script (new & improved!)
# 1  
Enhanced partition copy script (new & improved!)

This is now a larger script than I would customarily post. But many folks have become accustom to getting it off this forum.

Every couple of years I update my favorite scripts. This script is one that I use regularly and have posted older versions every couple of years. I noticed that it has been translated into several foreign languages too.

Two years ago I promised to revise it so that it could be more easily run using menus. I finally got around to doing that and have enhanced several other features. The last script was primarily a disk imaging script. I realized that a separate script was really needed for partition copying so I modified the original script to be more specific.

I will be upgrading and releasing the disk imaging script soon. Until then, please enjoy... and be careful! This script will wipe things out if you aren't extra cautious Smilie
Jan Zumwalt

Code:
#! /bin/bash 
# Name:  partition-copy.sh
# By:    Jan Zumwalt - www.neatinfo.com 
# Ver:   August 22th, 2010
# About: Copies partition and support files using dd command
#
# Copyright: 
#        1992-2010 by Jan Zumwalt, www.neatinfo.com
#        This software is free and open to the public. You may use, copy, 
#        modify, distribute, and sell this program (and any variants) 
#        without restriction, so long as you do not restrict others from  
#        doing the same.
#
# History: 
#        1992 - simple script for IBM AIX and Minix OS @20 lines
#        1995 - enhanced and re-designed for SUSE Linux OS @60 lines
#        1998 - enhanced and re-designed for Mandrake Linux OS @100 lines
#        2002 - log and re-designed for PcLinux OS @150 lines
#        2005 - verbose comments and info section @250 lines
#        2008 - modifiable variables to top, keybrd input, user modifiable @350 lines
#        2009 - error checks, self install and shortcut feature @450 lines
#        2010 - aesthetics, menus and aux system files capability @600 lines
#
# Future enhancements:
#       *Calculate file save elapsed time - in beta testing
#        Improve log format
#        Ability to save session log with partition file 
#        Restore feature
#        Better file compression 
#        Kill process feature
#
# code remarks:
#    -e = enable interpretation of backslash codes
#    -n = disable newline at end of line (i.e. user input prompt)
#    ${VAR} can be used to separate a variable if no space is
#           available in a string, ie "http://www.${WEB}.com"
#    exit 1 = error, exit 0 = success

# +------------------------------------------------------+
# |    These are the user modifiable variables. The      |
# |    "MENU" variables are for the partition menu.      |           
# +------------------------------------------------------+

PARTITION="/dev/sda6"               # default partition to copy and save
PD="50gb PcLinux development"       # description

MENU1="/dev/sda1"                   # menu item 1
MENU1D="75gb development XP"        # description

MENU2="/dev/sda7"                   # menu item 2
MENU2D="100gb development data"     # description

MENU3="/dev/sdb1"                   # menu item 3
MENU3D="75gb backup XP"             # description

MENU4="/dev/sdb2"                   # menu item 4
MENU4D="400gb primary data"         # description

MENU5="/dev/sdd0"                   # menu item 5
MENU5D="usb thumb drive"            # description

MENU6="/dev/sdd1"                   # menu item 6
MENU6D="usb thumb drive"            # description

MENU7="         "                   # menu item 7
MENU7D="un-assigned"                # description

MENU8="         "                   # menu item 8
MENU8D="un-assigned"                # description

MENU9="         "                   # menu item 9
MENU9D="un-assigned"                # description

FILE_ID="-`date +%Y-%m-%d_%N`"                    # unique id so we can save many partitions
FILE_PATH="/media/data-400gb/partition-images/"   # target path, needs a trailing slash
FILE_NAME="partition"

# ##########################################################
# ###          END OF USER MODIFIABLE VARIABLES          ###
# ###          DO NOT MODIFY CODE BELOW THIS !           ###
# ##########################################################

# Pretty ANSI writing
OFF="\033[0m"
BOLD="\033[1m"
DIM="\033[2m"
YELLOW="\033[1;33m"
RED="\033[1;31m"
GREEN="\033[1;32m"
BLUE="\033[1;34m"

FkeyRead ()  # read 1 character from keyboard, char is in $ANSWER
{
oldstty=$(stty -g)
stty -icanon -echo min 1 time 1
ANSWER=$(dd bs=1 count=1 2>/dev/null)
stty "${oldstty}"
}

clear

echo -en "\n\t ,--------------------------------------------------,"
echo -en "\n\t |                                                  |"
echo -en "\n\t |     $GREEN"'______          _   _ _   _             '"$OFF     |"
echo -en "\n\t |     $GREEN"'| ___ \        | | (_) | (_)            '"$OFF     |"
echo -en "\n\t |     $GREEN"'| |_/ /_ _ _ __| |_ _| |_ _  ___  _ __  '"$OFF     |"
echo -en "\n\t |     $GREEN"'|  __/ _` | `__| __| | __| |/ _ \| `_ \ '"$OFF     |"
echo -en "\n\t |     $GREEN"'| | | (_| | |  | |_| | |_| | (_) | | | |'"$OFF     |"
echo -en "\n\t |     $GREEN"'\_|  \__,_|_|   \__|_|\__|_|\___/|_| |_|'"$OFF     |"
echo -en "\n\t |     $GREEN"'       _____                            '"$OFF     |"
echo -en "\n\t |     $GREEN"'      /  __ \                           '"$OFF     |"
echo -en "\n\t |     $GREEN"'      | /  \/ ___  _ __  _   _          '"$OFF     |"
echo -en "\n\t |     $GREEN"'      | |    / _ \| `_ \| | | |         '"$OFF     |"
echo -en "\n\t |     $GREEN"'      | \__/\ (_) | |_) | |_| |         '"$OFF     |"
echo -en "\n\t |     $GREEN"'       \____/\___/| .__/ \__, |         '"$OFF     |"
echo -en "\n\t |     $GREEN"'                  | |     __/ |         '"$OFF     |"
echo -en "\n\t |     $GREEN"'                  |_|    |___/          '"$OFF     |"
echo -en "\n\t |                                                  |"
echo -en "\n\t '--------------------------------------------------'"


echo -en "$OFF"
echo -en "\n\n\t\t$BOLD$RED     by: Jan Zumwalt - NeatInfo.com"
echo -en "\n\t\t\t       Ver-8/2010 $OFF"                                                    
echo -en "\n\n\t\t     Press$YELLOW [ANY]$OFF key to continue... "
FkeyRead
clear
echo -en "\n"
echo -en "\t This program uses the dd command to copy a partition to\n" 
echo -en "\t a file. Without this script, a typing mistake using the\n"
echo -en "\t dd  command  can  cause  all your data to be destroyed!\n"
echo -en "\n"
echo -en "\t This script allows you to  carefully  plan ahead,  then\n"
echo -en "\t repeat the process fearlessly. It also makes a nice log.\n"
echo -en "\n"
echo -en "\t This script is currently set for...\n"
echo -en "\t default partition: $BOLD$PARTITION$OFF $PD\n"
echo -en "\t default file name: $BOLD$FILE_NAME$OFF\n"
echo -en "\t default file id:   $BOLD$FILE_ID$OFF\n"
echo -en "\n"
echo -en "\t You  will  be  given  an   opportunity  to  change  the\n"
echo -en "\t$BOLD partition $OFF and $BOLD file $OFF name.  Partition menu choices may\n"
echo -en "\t be changed by opening the script with a text editor and\n"
echo -en "\t editing  variables  near the  beginning of this script."                                                  
echo -en "\n\n\t\t     Press$YELLOW [ANY]$OFF key to continue... "
FkeyRead

while :
do
clear
echo -en "\n\n\n"
echo -en "\t .------------------------------------------------.\n"
echo -en "\t | $GREEN                   MAIN MENU                  $OFF |\n"
echo -en "\t |------------------------------------------------|\n"
echo -en "\t | Please select from one of these options.       |\n"
echo -en "\t |                                                |\n"
echo -en "\t |$YELLOW 1) Copy partition to file $OFF                     |\n"  
echo -en "\t |$YELLOW 2) View file system $OFF (fdisk -l)                |\n"
echo -en "\t |$YELLOW 3) Initialize logs and create a shortcut $OFF      |\n"
echo -en "\t |$YELLOW 4) Info - brief instructions $OFF                  |\n"
echo -en "\t |$YELLOW 5) Quit - also q,n, or x $OFF                      |\n"
echo -en "\t '------------------------------------------------'\n"
echo -en "\n"
echo -en "\t$BOLD    What would you like to do? 1,2,3,4,5 $YELLOW[1]$OFF "
FkeyRead
MENU_CHOICE=$ANSWER
   case $MENU_CHOICE in 
      1|"")     # The user selected "Copy partition to file"

          while :
          do
          clear
          echo -en "\n\n\n\n"
echo -en "\t .------------------------------------------------.\n"
echo -en "\t | $GREEN                PARTITION MENU                $OFF |\n"
echo -en "\t '------------------------------------------------'\n"
          echo -en "\t                               \n"          
          echo -en "\t $YELLOW 1) $MENU1 $OFF $MENU1D \n"  
          echo -en "\t $YELLOW 2) $MENU2 $OFF $MENU2D \n"
          echo -en "\t $YELLOW 3) $MENU3 $OFF $MENU3D \n"  
          echo -en "\t $YELLOW 4) $MENU4 $OFF $MENU4D \n" 
          echo -en "\t $YELLOW 5) $MENU5 $OFF $MENU5D \n"  
          echo -en "\t $YELLOW 6) $MENU6 $OFF $MENU6D \n"
          echo -en "\t $YELLOW 7) $MENU7 $OFF $MENU7D \n"  
          echo -en "\t $YELLOW 8) $MENU8 $OFF $MENU8D \n"
          echo -en "\t $YELLOW 9) $MENU9 $OFF $MENU9D \n"
          echo -en "\t $YELLOW 0) Quit   $OFF    do you really want to quit?\n"
          echo -en "\t                               \n"
          echo -en "\t .-------------------------------------------------. \n"
          echo -en "\t |$YELLOW [default] = $PARTITION $OFF $PD |\n" 
          echo -en "\t '-------------------------------------------------'\n"
          echo -en "\n"
          echo -en "\t$BOLD Partition$OFF to copy and save 1,2,3... or $YELLOW[default]$OFF "
          FkeyRead
          PART_CHOICE=$ANSWER
          case $PART_CHOICE in 
             "")  # The user selected [default] partition
                  # deafault partition is already set, so continue
               break ;;
             1)   # The user selected menu item 1
                  PARTITION=$MENU1
               break ;;
             2)   # The user selected menu item 1
                  PARTITION=$MENU2
               break ;;
             3)   # The user selected menu item 1
                  PARTITION=$MENU3
               break ;;
             4)   # The user selected menu item 1
                  PARTITION=$MENU4
               break ;;
             5)   # The user selected menu item 1
                  PARTITION=$MENU5
               break ;;
             6)   # The user selected menu item 1
                  PARTITION=$MENU6
               break ;;
             7)   # The user selected menu item 1
                  PARTITION=$MENU7
               break ;;
             8)   # The user selected menu item 1
                  PARTITION=$MENU8
               break ;;
             9)   # The user selected menu item 1
                  PARTITION=$MENU9
               ;;
             0)   # The user selected menu item 1
                  echo -en "\n\n\n\n\t$GREEN OK! I am quiting, thanks for using$YELLOW Partion-Copy!$OFF\n"
                  echo -en "\n\t Press $YELLOW [ANY]$OFF key to exit... "
                  FkeyRead
                  echo -en "\n\n"
                  exit 1
               ;;
             *)   # anything else is invalid
                  clear
                  echo -en "\n\n\n\n"
                  echo -en "\t .-------------------------------------------------. \n"
                  echo -en "\t |                                                 | \n"
                  echo -en "\t |         $BOLD$RED That was an incorrect choice! $OFF         |\n"
                  echo -en "\t |          Press $YELLOW [ANY]$OFF key to continue...        |\n"
                  echo -en "\t |                                                 | \n"
                  echo -en "\t '-------------------------------------------------'\n" 
                  echo -en "\t "
                  FkeyRead
               ;;
          esac
          done

         clear
         echo -en "\n"
         echo -en "\t OK! Here we go...\n"

            # see if target directory exist
     	    if [ ! -d $FILE_PATH ]; then
 	       echo -en "\n\t$RED *** Target file directory not found *** $OFF\n"
 	       echo -en "\n\t *** I can not go on without it!     ***\n"
               echo -en "\n\t Should I try to create the directory I need $BOLD$FILE_PATH$OFF ? "
               echo -en "\n\t (I can only create a single directory off an existing one)"
               FkeyRead
   		  case $ANSWER in 
      		     [Yy]) 
                        mkdir $FILE_PATH
 	                echo -en "\n\n\t I made the target file directory..."
                        ;;
                     *)
                        clear
                        echo -en "\n\n\t $RED I warned YOU that I needed it! $OFF"
                        echo -en "\n\t $BOLD I QUIT!... $OFF\n\n\n"
                        exit 1;;
                  esac
            fi

            # see if log directory exist
     	    if [ ! -d $HOME/logs ]; then
 	       echo -en "\n\n\t $RED *** Log directory not found *** $OFF\n"
               echo -en "\n\t Should I create the directory I need for logs ($HOME/logs)? "
               FkeyRead
   		  case $ANSWER in 
      		     [Yy]) 
                     mkdir $HOME/logs
                  esac
            fi

            # choose file name
 	       echo -en "\n\t$YELLOW Enter a file name, (no spaces or weird characters) $OFF"
               read FILE_NAME


            # see if user wants fdisk info saved
 	       echo -en "\n\t$YELLOW Would you like fdisk info saved [y/n]? $OFF"
               FkeyRead
   		DISK_INFO=$ANSWER
   		  case $DISK_INFO in 
      		     [Yy]) 
                        fdisk -l $PARTITION > $FILE_PATH$FILE_NAME$FILE_ID.fdisk.txt
 	                echo -en "\n\n\t I saved the fdisk info to $FILE_PATH$FILE_NAME$FILE_ID.fdisk.txt"
                        ;;
                     *)
                        echo -en "\n\t No fdisk info saved..."
                        ;;
                  esac

            # see if user wants mtab (fstab) data saved
 	       echo -en "\n\n\t$YELLOW Would you like mtab (fstab) data saved [y/n]? $OFF"
               FkeyRead
   		MTAB_DATA=$ANSWER
   		  case $MTAB_DATA in 
      		     [Yy]) 
                        cp /boot/grub/menu.lst $FILE_PATH$FILE_NAME$FILE_ID.mtab 
 	                echo -en "\n\n\t I saved mtab data to $FILE_PATH$FILE_NAME$FILE_ID.mtab"
                        ;;
                     *)
                        echo -en "\n\t No mtab data saved..."
                        ;;
                  esac

            # see if user wants mbr data saved
 	       echo -en "\n\n\t$YELLOW Would you like mbr image saved [y/n]? $OFF"
               FkeyRead
   		MBR_DATA=$ANSWER
   		  case $MBR_DATA in 
      		     [Yy]) 
                        dd if=$PARTITION of=$FILE_PATH$FILE_NAME$FILE_ID.mbr.img count=1 bs=512 
 	                echo -en "\n\n\t I saved the mbr image to $FILE_PATH$FILE_NAME$FILE_ID.mbr.img"
                        ;;
                     *)
                        echo -en "\n\t No mbr image saved..."
                        ;;
                  esac

            # see if user wants grub menu data saved
 	       echo -en "\n\n\t$YELLOW Would you like grub boot menu data saved [y/n]? $OFF"
               FkeyRead
   		GRUB_DATA=$ANSWER
   		  case $GRUB_DATA in 
      		     [Yy]) 
                        cp /boot/grub/menu.lst $FILE_PATH$FILE_NAME$FILE_ID.grub.menu.lst 
 	                echo -en "\n\n\t I saved grub menu data to $FILE_PATH$FILE_NAME$FILE_ID.grub.menu.lst"
                        ;;
                     *)
                        echo -en "\n\t No grub data saved..."
                        ;;
                  esac

                  clear
                  echo -en "\n\n\n"
                  echo -en "\n\t .-------------------------------------------------."
                  echo -en "\n\t |                                                 |"
                  echo -en "\n\t | $RED"'    _    _                  _             _  '"$OFF   |"
                  echo -en "\n\t | $RED"'   | |  | |                (_)           | | '"$OFF   |"
                  echo -en "\n\t | $RED"'   | |  | | __ _ _ __ _ __  _ _ __   __ _| | '"$OFF   |"
                  echo -en "\n\t | $RED"'   | |/\| |/ _` | `__| `_ \| | `_ \ / _` | | '"$OFF   |"
                  echo -en "\n\t | $RED"'   \  /\  / (_| | |  | | | | | | | | (_| |_| '"$OFF   |"
                  echo -en "\n\t | $RED"'    \/  \/ \__,_|_|  |_| |_|_|_| |_|\__, (_) '"$OFF   |"
                  echo -en "\n\t | $RED"'                                     __/ |   '"$OFF   |"
                  echo -en "\n\t | $RED"'                                    |___/    '"$OFF   |" 
                  echo -en "\n\t |                                                 |"
                  echo -en "\n\t '-------------------------------------------------'" 
                  echo -en "\n\n\t$YELLOW You are about to copy...$OFF\n"
                  echo -en "\n\t $GREEN$PARTITION $OFF"
                  echo -en "\n\t   to..."
                  echo -en "\n\t $GREEN$FILE_PATH$FILE_NAME$FILE_ID.gz $OFF\n"
                  echo -en "\n\t Do you want to continue y/n$YELLOW [Y]$OFF? "
                  FkeyRead
   		  case $ANSWER in 
                     [QqNnXx]) # we are here because the user wanted to quit
                        clear         
                        echo -en "\n\n"
                        echo -en "\t      OK! I am quiting...\n"
                        echo -en "\n\t      Press $YELLOW [ANY]$OFF key to exit... "
                        FkeyRead
                        echo -en "\n\n"
                        exit 1;;
                     *) # any other key allows us to proceed
                        ;;
                  esac
         clear
         echo -en "\n\n\t OK! We are going to continue...."
         # opportunity to add comment to log file
	 echo -en "\n\n\t$YELLOW You may now add a comment for the log file...$OFF\n"
         echo -en "\t "        
         read ANSWER
         echo -e "\t\t"$ANSWER >> $HOME/logs/partition-copy.log
         # save backup starting time      image  = /boot/vmlinuz

         echo -en "\t +-----------------------------------------------+\n"
         echo -en "\t Backup start time = " `date`"\n"| tee -a "$HOME/logs/partition-copy.log"
         echo -en "\t +-----------------------------------------------+\n"
         echo -en "\t Backing up...\n"| tee -a "$HOME/logs/partition-copy.log"
         # The actual backup is done here. Included are some possible options.
         # These options are machine specific - here is some of mine...
         # dd if=/dev/hda1 of=/dev/hdc1 # win partition
         # dd if=/dev/hda7 of=/dev/hdc7 # linux partition
         # dd if=/dev/hda  of=/dev/hdc  # entire IDE slot 1 drive to IDE slot 3 <default>
         echo -en "\t Partition=$PARTITION\n"| tee -a "$HOME/logs/partition-copy.log"
         echo -en "\t Path=$FILE_PATH\n"     | tee -a "$HOME/logs/partition-copy.log"
         echo -en "\t File name=$FILE_NAME\n"| tee -a "$HOME/logs/partition-copy.log"
         echo -en "\t File ID=$FILE_ID.gz\n" | tee -a "$HOME/logs/partition-copy.log"
         START_TIME=`date +%s`
         dd if=$PARTITION  bs=1024k | gzip > "$FILE_PATH$FILE_NAME$FILE_ID.gz"| tee -a "$HOME/logs/partition-copy.log"
         END_TIME=`date +%s`
         ELAPSED_MINUTES=`echo $((($END_TIME-$START_TIME)/60))`
         echo -en "\t Elapased minutes: $ELAPSED_MINUTES"  | tee -a "$HOME/logs/partition-copy.log"     
         # save backup ending time
         echo -en "\n\t Backup end time = " `date`"\n"| tee -a "$HOME/logs/partition-copy.log"
         # success - show log file and exit
         echo -en "\t Script completed successfully.\n\n"| tee -a "$HOME/logs/partition-copy.log"
         echo -en "\t *******************************************\n"
         echo -en "\t ***   Here is a copy of your log file   ***\n"
         echo -en "\t *******************************************\n"
         cat $HOME/logs/partition-copy.log
         echo -en "\n\n\t     Press the$YELLOW [ANY]$OFF key to exit. "
         FkeyRead
         echo -en "\n\n"  
       exit 0;;

      2) # show drive info         
           clear
           echo -en "\n\n"
           echo -en ",-------------------------------------------------------------------,\n" | pr -o5 -t
           echo -en ".                                                                   .\n" | pr -o5 -t
           echo -en ".  This is a listing of your MOUNTED media. You may have other      .\n" | pr -o5 -t
           echo -en ".  media attached and available. The file system or devices ${YELLOW}do${OFF}      .\n" | pr -o5 -t
           echo -en ".  ${YELLOW}not have to be mounted${OFF} to work.$BOLD The \"dd\" command that this       .\n" | pr -o5 -t
           echo -en ".  program uses can be VERY dangerous.$OFF There is no way of knowing   .\n" | pr -o5 -t
           echo -en ".  what the intent of the user is, and in less than 1 second        .\n" | pr -o5 -t
           echo -en ".  permanent data loss will occur! So, please be very carfull!      .\n" | pr -o5 -t
           echo -en ".                                                                   .\n" | pr -o5 -t
           echo -en "'-------------------------------------------------------------------'\n" | pr -o5 -t
           echo -en "\n" | pr -o5 -t
           df -h -T | pr -o5 -t | cut -c1-75
           echo -en "\n" | pr -o5 -t
           fdisk -l | pr -o5 -t | cut -c1-75

           echo -en "\n     Press$YELLOW [ANY]$OFF key to return to main menu." | pr -o5 -t
           FkeyRead
           clear ;;

      3) # set up program and create a shortcut         
           clear 
           echo -en "\n\t$YELLOW" `date`"$OFF" | tee -a "$HOME/logs/partition-copy.log"
           echo -en "\n"
           echo -en "\n\t$BOLD Checking for personal log directory... $OFF\n"
           if [ ! -d $HOME"/logs" ] ; then # if log dir not made, create it
              mkdir $HOME"/logs"
              echo -en "\t Missing personal log dir, I made one at $HOME/logs\n"| tee -a "$HOME/logs/partition-copy.log"
           else
              echo -en "\t Found personal log dir at $HOME/logs\n"| tee -a "$HOME/logs/partition-copy.log"
           fi

           echo -en "\n\t$BOLD Checking for personal bin directory... $OFF\n"
           if [ ! -d $HOME"/bin" ] ; then # if bin dir not made, create it
              mkdir $HOME"/bin"
              echo -en "\t Missing personal bin dir, I made one at $HOME/bin\n"| tee -a "$HOME/logs/partition-copy.log"
           else
              echo -en "\t Found personal bin dir at $HOME/bin\n"| tee -a "$HOME/logs/partition-copy.log"
           fi

           echo -en "\n\t$BOLD Checking for script in personal bin directory... $OFF\n"
           if [ ! -f $HOME"/bin/partition-copy.sh" ] ; then # if prog not in bin dir, copy it there
              cp $0 $HOME"/bin/partition-copy.sh" # copy myself to the bin dir
              echo -en "\t I copied script to bin dir at $HOME/bin\n"| tee -a "$HOME/logs/partition-copy.log"
           else
              cp $0 $HOME"/bin/partition-copy.sh" # copy myself to the bin dir
              echo -en "\t I overwrote script in bin dir at $HOME/bin\n"| tee -a "$HOME/logs/partition-copy.log"
           fi

           echo -en "\n\t$BOLD Checking script permissions, making executable... $OFF\n"
           chmod  ugo+x "$HOME/bin/partition-copy.sh" # make sure script will execute 
           echo -en "\t Confirmed script is executable at $HOME/bin\n"| tee -a "$HOME/logs/partition-copy.log"

           echo -en "\n\t$BOLD Desktop shortcut... $OFF\n"
           START_DIR=$PWD # save current dir
           echo -en "\t Going to your desktop and creating shortcut$YELLOW Partition-Copy$OFF...\n"| tee -a "$HOME/logs/partition-copy.log" 
           cd $HOME"/Desktop" # goto desktop to make shortcut
           
           # This next section of code creates the desktop shortcut
           {
           echo "[Desktop Entry]"
           echo "Comment="
           echo "Comment[en_US]="
           echo "Encoding=UTF-8"
           echo "Exec='$HOME/bin/partition-copy.sh'"
           echo "GenericName=" 
           echo "GenericName[en_US]="
           echo "Icon=cantor"
           echo "MimeType="
           echo "Name=partition-copy"
           echo "Name[en_US]=partition-copy"
           echo "Path="               
           echo "StartupNotify=true" 
           echo "Terminal=true"               
           echo "TerminalOptions=\s--noclose"   
           echo "Type=Application"            
           echo "X-DCOP-ServiceType=none"   
           echo "X-KDE-SubstituteUID=false"
           echo "X-KDE-Username=" 
           } > "partition-copy.desktop"
           chmod  ugo+x "partition-copy.desktop" # make shortcut executable
           cd $START_DIR # be nice and return to our original dir

           echo -en "\t Created desktop shortcut...\n"| tee -a "$HOME/logs/partiton-copy.log" 
           echo -en "\n\t Press$YELLOW [ANY]$OFF key to return to main menu."
           FkeyRead
           clear 
           ;;
         
      4) # The user wants some information about the script
         clear
         echo -en "\n"
         echo -en "\t .--------------------- $BOLD About This Program $OFF ---------------------.\n"
         echo -en "\t .                                                                .\n"
         echo -en "\t .  This is a very brief  explanation  of how this program works. .\n"
         echo -en "\t .  This  program  will work with all file systems.  They do not  .\n"
         echo -en "\t .  have to be mounted to work. This program's primary use is to  .\n"
         echo -en "\t .  copy a  complete  partition  to a  file (backup).  This will  .\n"
         echo -en "\t .  provide  an  exact  byte  for  byte copy (including MS Win).  .\n"
         echo -en "\t . $YELLOW This  program  can  be  VERY dangerous.$OFF There is no way of   .\n"
         echo -en "\t .  knowing what the users  intent is.  A typing error can cause  .\n"
         echo -en "\t .  permanent data loss in less than 1 second!                    .\n"
         echo -en "\t .                                                                .\n"
         echo -en "\t .  Most mother boards provide  SATA,  IDE,  SCSII and USB ports  .\n"
         echo -en "\t .  for data storage. UNIX controls  drives and partitions using  .\n"
         echo -en "\t .  alias names. Here are some examples...                        .\n"
         echo -en "\t .  IDE uses hda,hdb,hdc, and  hdd                                .\n"
         echo -en "\t .  SATA drives use sda, sdb, etc                                 .\n"
         echo -en "\t .  USB are sdd0, sdd1, sdd2 and so on                            .\n"
         echo -en "\t .                                                                .\n"
         echo -en "\t .  Each partition on a drive can be designated by adding a       .\n"
         echo -en "\t .  number. hda1 is the first partition on the first IDE drive,   .\n"
         echo -en "\t .  hda3 is the third partition of the same drive.                .\n"
         echo -en "\t .                                                                .\n"
         echo -en "\t .  more...                   pg 1 of 4                           .\n"
         echo -en "\t '----------------------------------------------------------------'\n"
         echo -en "\n\t     Press $YELLOW [ANY]$OFF key to continue... "
         FkeyRead
         clear
         echo -en "\n"
         echo -en "\t .--------------------- $BOLD About This Program $OFF ---------------------.\n"
         echo -en "\t .                                                                .\n"
         echo -en "\t . $BOLD dd$OFF is the UNIX command to make a file or partition image.     .\n"
         echo -en "\t .  The \"dd\" command uses$BOLD if$OFF (in file) and$BOLD of$OFF (out file) as file  .\n"
         echo -en "\t .  system parameters. To completely copy the 1st IDE drive to    .\n"
         echo -en "\t .  the 2nd drive, we would use this command...                   .\n"
         echo -en "\t .    $YELLOW dd if=/dev/hda of=/dev/hdb $OFF                                .\n"
         echo -en "\t .                                                                .\n"
         echo -en "\t .  Because we did not give partition numbers, the whole 1st      .\n"
         echo -en "\t .  drive will be copied to the entire 2nd drive.                 .\n"
         echo -en "\t .  To copy a specific partition we must use the partition        .\n"
         echo -en "\t .  number. Suppose we would like to copy the 1st partition of    .\n"
         echo -en "\t .  IDE-3 (the \"c\" drive) and have it go to partition 4 of the    .\n"
         echo -en "\t .  same drive. We would use this command...                      .\n"
         echo -en "\t .    $YELLOW dd if=/dev/hdc1 of=/dev/hdc4  $OFF                             .\n"
         echo -en "\t .                                                                .\n"
         echo -en "\t .  Be forwarned that if the two partitions are not the same      .\n"
         echo -en "\t .  size, bad things can happen. You usually are all right if     .\n"
         echo -en "\t .  you copy a small partition to a big one, but copying a big    .\n"
         echo -en "\t .  one to a small one usually results in data corruption.        .\n"
         echo -en "\t .                                                                .\n"
         echo -en "\t .  more...                   pg 2 of 4                           .\n"
         echo -en "\t '----------------------------------------------------------------'\n"
         echo -en "\n\t     Press $YELLOW [ANY]$OFF key to continue... "
         FkeyRead
         clear
         echo -en "\n"
         echo -en "\t .----------------------------------------------------------------.\n"
         echo -en "\t .                                                                .\n"
         echo -en "\t .  To  make  selecting  the  file  systems  easy,  I  made two   .\n"
         echo -en "\t .  variables to hold the drive  definitions.  They are located   .\n"
         echo -en "\t .  near  the  beginning  of  this  script.  Here  is  how  the   .\n"
         echo -en "\t .  actual \"dd\" command looks using variables...                  .\n"
         echo -en "\t .  $YELLOW  dd if=\$PARTITION of=\$FILE_ID  $OFF                       .\n"
         echo -en "\t .                                                                .\n"
         echo -en "\t .  UNIX  is  very  generious  in  file system support, so this   .\n"
         echo -en "\t .  script can be used for  just  about  any  type  of  storage   .\n"
         echo -en "\t .  device. I have not tried other devices such as memory cards,  .\n"
         echo -en "\t .  but I can not see any  reason they  should not work just as   .\n"
         echo -en "\t .  well.                                                         .\n"
         echo -en "\t .                                                                .\n"
         echo -en "\t .  more...                   pg 3 of 4                           .\n"
         echo -en "\t '----------------------------------------------------------------'\n"
         echo -en "\n\t     Press $YELLOW [ANY]$OFF key to continue... "
         FkeyRead
         clear
         echo -en "\n"
         echo -en "\t .----------------------------------------------------------------.\n"
         echo -en "\t .  Here are some examples I use with my 250gb development sys.   .\n"
         echo -en "\t .  To mirror my entire drive \"A\" to drive \"C\"                    .\n"
         echo -en "\t . $YELLOW PARTITION=/dev/hda $OFF                                      .\n"
         echo -en "\t . $YELLOW FILE_ID=/dev/hdc $OFF                                             .\n"
         echo -en "\t .                                                                .\n"
         echo -en "\t .  To save the Windows XP OS on drive \"A\" partition 1 to a file  .\n"
         echo -en "\t .  with the date i.e \"partition-image-2010-06-04\"                .\n"
         echo -en "\t . $YELLOW PARTITION=/dev/hda1 $OFF                                     .\n"
         echo -en "\t . $YELLOW FILE_ID=/media/data/partition-image-\`date +%Y-%m-%d\`$OFF          .\n"
         echo -en "\t .                                                                .\n"
         echo -en "\t .  At the shell prompt, type$YELLOW man fdisk$OFF for some                  .\n"
         echo -en "\t .  interesting information. I hope this is of some use :)        .\n"
         echo -en "\t .                                                                .\n"
         echo -en "\t .                              $BOLD  Jan Zumwalt - NeatInfo.com $OFF     .\n"
         echo -en "\t .                                                                .\n"
         echo -en "\t .                          pg 4 of 4                             .\n"
         echo -en "\t .----------------------------------------------------------------.\n"
         echo -en "\n\t     Press $YELLOW [ANY]$OFF key to continue... "
         FkeyRead
         clear ;;

      5|[QqNnXx]) # we are here because the user wanted to quit
         clear         
         echo -en "\n\n"
         echo -en "\t      OK! I am quiting...\n"
         echo -en "\n\t      Press $YELLOW [ANY]$OFF key to exit... "
         FkeyRead
         echo -en "\n\n"
         exit 1;;
         
      *) # the user's response does not make sense so...
         clear         
         echo -en "\n\n"
         echo -en "\t\t$BOLD   Please enter ${YELLOW}1,2,3,4,5 $OFF \n"
         echo -en "\n\t   Press $YELLOW [ANY]$OFF key to try again... "
         FkeyRead
         echo -en "\n\n"
         clear ;;
   esac
done
exit 1

# 2  
Because of the support requests, need for regular updates, and additional instructions or information, I have formally entered this as a project at source forge.

https://sourceforge.net/projects/partition-copy/
# 3  
One thing I've noticed that dd seems unable to do is properly save/restore "extended" partition information. It's not recorded in the boot sector, and dd-ing the "extended" device (not the partitions in it) doesn't seem to preserve it either. Where is it stored?
# 4  
I'm not sure I understand what you are asking but here is a bit of additional information that may or may not answer your question.

The partition information for an entire drive is kept in the MBR (master boot record). There is no separate or individual information block available. Therefore my program (and any other) that attempts to save the first 512 bytes of a partition (which is the mbr) for any partition other than the first will receive an error.

My program does not attempt to stop someone from saving an invalid MBR block on other partitions. dd is smart enough to notice that an attempt is being made to save the MBR of an invalid partition and will shoot out an error message, but it still saves the invalid 512 byte block.

This first example will work because the mbr is actualy at the start of partition 1
Code:
dd if=hda1 of=partition.mbr.img count=1 bs=512

This example will cause an error message because the 2nd partition does not have an mbr!
Code:
dd if=hda2 of=partition.mbr.img count=1 bs=512

My thinking is that there may be some special case that I am not aware of that a user may need that 512 byte block. So, I trust the users judgment.

In the future, I will place some type of error trap and give sa message to the user that explains what is going on.

I hope this information helps.

Jan Zumwalt
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #125
Difficulty: Easy
UNIX is a certification of a complete OS system, not only a UNIX kernel.
True or False?

9 More Discussions You Might Find Interesting

1. Red Hat

Shrink LVM partition & create new Linux Primary partition

Hello All, I have a Red Hat Linux 5.9 Server installed with one hard disk & 2 Partitions created on it as follows, /boot - Linux Partition & another is LVM - One VG & under that 5-6 Logical volumes(var,opt,home etc). Here my requirement is to take out 1GB of space from LVM ( Any logical... (5 Replies)
Discussion started by: gr8_usk
5 Replies

2. Shell Programming and Scripting

Script to overwrite & before that keep copy a file on many servers

I have ssh password less auth enable & script does the job well as well #/bin/bash for i in `cat ip` do scp /etc/resolv.conf root@$ip done But I need to take backup of the file i will overwrite .. is there any simple way ? Kindly respond (5 Replies)
Discussion started by: heman96
5 Replies

3. Shell Programming and Scripting

Compare & Copy Directories : Bash Script Help

Beginner/Intermediate shell; comfortable in the command line. I have been looking for a solution to a backup problem. I need to compare Directory 1 to Directory 2 and copy all modified or new files/directories from Directory 1 to Directory 3. I need the directory and file structure to be... (4 Replies)
Discussion started by: Rod
4 Replies

4. Filesystems, Disks and Memory

Using dd to copy a partition.

Here's a conundrum. I use a ThinkPad (T30) which has a slot on the side for the hard drive. It is very easy to swap this with another hard drive which I keep as a backup. Now when I copy the Linux partition from my (in use) hard drive to the backup one (in my UltraBay slot) it takes only 30... (0 Replies)
Discussion started by: newyorkpaulie
0 Replies

5. Shell Programming and Scripting

Perl Script to find & copy words from Web.

I need to write a perl script to search for a specific set of numbers that occur after a series of words but before another. Specifically, I need to locate the phrase today at the summit, then immediately prior to the words tonnes/day copy the number that will be between 100 and 9,999, for example,... (1 Reply)
Discussion started by: libertyforall
1 Replies

6. UNIX for Advanced & Expert Users

copy a whole partition excluding specified.

Scenario: I would want to copy my / to /mnt, and to avoid recursion exclude /mnt. cp -avx / /mnt If i use the above i believe it would run recursively, and end up in mess. So how to do it ?! Basically this / is sda1, and /mnt is sda2 and sda1 is where only OS is available & currently... (2 Replies)
Discussion started by: thegeek
2 Replies

7. Shell Programming and Scripting

How do I search first&second string & copy all content between them to other file?

Hi All, How do I search first string & second string and copy all content between them from one file to another file? Please help me.. Thanks In Advance. Regards, Pankaj (12 Replies)
Discussion started by: pankajp
12 Replies

8. UNIX for Dummies Questions & Answers

slice & partition???

:confused: Hello, What is the difference between slice and partition on Solaris world? Regards (4 Replies)
Discussion started by: XNOR
4 Replies

9. Gentoo

How to copy single partition?

I presently backup my multi-OS multi-paritition boot drive (fedora core 4/ext3, WinXPServer/NTFS, WinXPHome/FAT32) with the command: telinit 1; cp /dev/sda /dev/sdb And this works. Is there a command to only copy a single partition instead of an entire device? And what about the grub... (12 Replies)
Discussion started by: siegfried
12 Replies

Featured Tech Videos