The UNIX and Linux Forums  

Go Back   В UNIX и Linux Форумы > Топ форумы > Shell программирование и сценарии
.
Google unix.com



Shell программирование и сценарии Почтовые вопросы о KSH, CSH, SH, BASH, Perl, PHP, SED, AWK и скрипты оболочки и оболочки скриптовых языков здесь.

Подробнее UNIX и Linux Темы форума можно найти полезные
Нить Резьба для начинающих Форум Ответы Последнее сообщение
Apple продуктов и хранения дисков производители доклад диск потенциала разному iBot OS X Поддержка RSS 0 11-24-2008 07:40 PM
монтаж USB дисковод / Flash Drive в ЛАРН 6.0 sureshdrajan ШОС 5 02-29-2008 12:36 PM
Создать разжечь изображения на ленту из Интернета IgniteUX изображение Andrek UNIX перспективных И опытных пользователей 3 02-28-2007 07:14 PM
Моя Drive Image ErikTheHAck HP-UX 0 06-06-2006 03:47 PM
rdsk VS ДСК для образа диска aicjofs UNIX для чайников Вопросы И Ответы 3 03-23-2006 10:34 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered By Powered by Google
 
LinkBack Резьба Инструменты Искать в этом Thread Оценить Thread Режимы дисплея
Old 01-01-2009
jwzumwalt jwzumwalt is offline
Зарегистрированный пользователь
  
 

Регистрация: август 2007
Сообщений: 15
Обновленный образ диска утилитой


Код:
#! /bin/bash 
# name: drive-image.sh, disk image copy script
# by:   Jan Zumwalt - www.neatinfo.com 
# ver:  Jan 4, 2009
# remarks:
# -e = enable interpretation of backslash codes
# -n = disable newline at end of line (i.e. user input prompt)
# exit 1 = error, exit 0 = success

IMAGE_FROM=hda  # the drive or partition we will copy (save). 
IMAGE_TO=hdc    # the target (were it will be saved)

# Pretty ANSI writing

OFF="\033[0m"
BOLD="\033[1m"
DIM="\033[2m"
YELLOW="\033[1;33m"

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

while :
do
clear
echo -en "\n"
echo -en "\t\t\t$BOLD          Drive Image Copy\n"
echo -en "\t\t\t           by:Jan Zumwalt\n"
echo -en "\t\t\t$YELLOW     NeatInfo.com - Ver Jan 04, 2009$OFF\n\n"
echo -en "\t\t This program uses the dd command to copy an entire drive,\n" 
echo -en "\t\t or partition.$YELLOW If a single mistake is made while manually\n"
echo -en "\t\t typeing the dd command, all your data may be destroyed.$OFF \n"
echo -en "\t\t This script allows you to carefully plan ahead, then \n"
echo -en "\t\t repeat the process fearlessly. It also makes a nice\n"
echo -en "\t\t log for future reference.\n"
echo -en "\n"
echo -en "\t\t This program is set to copy from [$IMAGE_FROM] to [$IMAGE_TO].\n"
echo -en "\t\t You can change what is copied by opening the script\n"
echo -en "\t\t with a text editor and changing two variables near the\n"
echo -en "\t\t beginning of the script.\n"
echo -en "\n\t\t\t\t   $YELLOW($IMAGE_FROM --> $IMAGE_TO)$OFF\n"
echo -en "\n$BOLD"
echo -en "\t\t.------------------------------------------------. \n"
echo -en "\t\t| Please select from one of these options.       |\n"
echo -en "\t\t|                                                |\n"
echo -en "\t\t|$YELLOW 1) Make the drive image $OFF$BOLD                       |\n"  
echo -en "\t\t|$YELLOW 2) View file systems list $OFF$BOLD                     |\n"
echo -en "\t\t|$YELLOW 3) Set up program and create a shortcut $OFF$BOLD       |\n"
echo -en "\t\t|$YELLOW 4) About this program (brief instructions)    $OFF$BOLD |\n"
echo -en "\t\t|$YELLOW 5) Quit $OFF$BOLD                                       |\n"
echo -en "\t\t'------------------------------------------------'\n"
echo -en "\n"
echo -en "\t\t$BOLD    What would you like to do? 1,2,3,4,5 $YELLOW[1]$OFF "
FkeyRead

   case $ANSWER in 
      1|"") 
         clear
         echo -en "\n"
         echo -en "\t\t OK! Here we go...\n"
            # see if log directory exist
     	    if [ ! -d $HOME/logs ]; then
 	       echo -en "\n\n\t\t*** Log directory not found ***\n"
               echo -en "\n\t\t Should I create the directory I need for logs ($HOME/logs)? "
               FkeyRead
   		  case $ANSWER2 in 
      		     [Yy]) 
                     mkdir $HOME/logs
                  esac
            fi
         # opportunity to add comment to log file
	 echo -en "\n\t\t You may now add a comment for the log file...\n"
         echo -en "\t\t "        
         read ANSWER
         echo -e "\t\t"$ANSWER >> $HOME/logs/drive-image.log
         # save backup starting time      image  = /boot/vmlinuz
         echo -en "\t\t Backup start time = " `date`"\n"| tee -a "$HOME/logs/drive-image.log"
         echo -en "\t\t Backing up...\n"| tee -a "$HOME/logs/drive-image.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>
         dd if=/dev/$IMAGE_FROM of=/dev/$IMAGE_TO bs=1024k | tee -a "$HOME/logs/drive-image.log"
         # save backup ending time
         echo -en "\t\t Backup end time = " `date`"\n"| tee -a "$HOME/logs/drive-image.log"
         # success - show log file and exit
         echo -en "\t\t Script completed successfully.\n\n"| tee -a "$HOME/logs/drive-image.log"
         echo -en "\t\t *******************************************\n"
         echo -en "\t\t ***   Here is a copy of your log file   ***\n"
         echo -en "\t\t *******************************************\n"
         cat $HOME/logs/drive-image.log
         echo -en "\n\n\t\t     Press the ${YELLOW}[ANY]${OFF} key to exit. "
         FkeyRead
         echo -en "\n\n"  
       exit 0;;

      2) # set up program and create a shortcut         
           clear
           echo -en "\n\n"
           echo -en "\t.-------------------  Here is your mounted media  ------------------.\n"
           echo -en "\t.                                                                   .\n"
           fdisk -l | pr -o10 -t | cut -c1-75
           echo -en "\t.                                                                   .\n"
           echo -en "\t'-------------------------------------------------------------------'\n"
           echo -en "\t.                                                                   .\n"
           echo -en "\t.  This is a listing of your MOUNTED media. You may have other      .\n"
           echo -en "\t.  media attached and available. The file system or devices ${YELLOW}do${OFF}      .\n"
           echo -en "\t.  ${YELLOW}not have to be mounted${OFF} to work.$BOLD The \"dd\" command that this       .\n"
           echo -en "\t.  program uses can be VERY dangerous.$OFF There is no way of knowing   .\n"
           echo -en "\t.  what the intent of the user is, and in less than 1 second        .\n"
           echo -en "\t.  permanent data loss will occur! So, please be very carfull!      .\n"
           echo -en "\t.                                                                   .\n"
           echo -en "\t'-------------------------------------------------------------------'\n"
           echo -en "\n\t\t     Press ${YELLOW}[ANY]${OFF} key to return to main menu."
           FkeyRead
           clear ;;

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

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

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

           chmod  ugo+x "$HOME/bin/drive-image.sh" # make sure script will execute 
           echo -en "\t\t Confirmed script is executable at $HOME/bin\n"| tee -a "$HOME/logs/drive-image.log"
           echo -en "\n"

           START_DIR=$PWD # save current dir
           echo -en "\t\t Going to your desktop and creating shortcut...\n"| tee -a "$HOME/logs/drive-image.log" 
           cd $HOME"/Desktop" # goto desktop to make shortcut
           
           {
           echo "[Desktop Entry]"
           echo "Comment="
           echo "Comment[en_US]="
           echo "Encoding=UTF-8"
           echo "Exec='$HOME/bin/drive-image.sh'"
           echo "GenericName=" 
           echo "GenericName[en_US]="
           echo "Icon=exec"
           echo "MimeType="
           echo "Name=drive-image"
           echo "Name[en_US]=drive-image"
           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=" 
           } > "drive-image.desktop"
           chmod  ugo+x "drive-image.desktop" # make shortcut executable
           cd $START_DIR # be nice and return to our original dir

           echo -en "\t\t Created desktop shortcut...\n"| tee -a "$HOME/logs/drive-image.log" 
           echo -en "\n\t\t Press ${YELLOW}[ANY]${OFF} key to return to main menu."
           FkeyRead
           clear ;;
         
      4) # we are here because the user wanted to quit
         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 complete drive images to identical drives. This will     .\n"
         echo -en "\t .  provide an exact backup that is bootable (including MS Win).  .\n"
         echo -en "\t .  If you are not interested in a bootable backup, it may be     .\n"
         echo -en "\t .  better to use the tar command which offers compact file size. .\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 typo error can cause      .\n"
         echo -en "\t .  permanent data loss in less than 1 second!                    .\n"
         echo -en "\t .                                                                .\n"
         echo -en "\t .  IDE drives are the most common, so I'll use it as an example. .\n"
         echo -en "\t .  Most mother boards provide 4 IDE drive ports attached by 2    .\n"
         echo -en "\t .  cables. Each cable can have two drives plugged on to them.    .\n"
         echo -en "\t .  UNIX controls them with alias names hda,hdb,hdc, and  hdd.    .\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 drive, hda3  .\n"
         echo -en "\t .  is the third partition of the same drive.                     .\n"
         echo -en "\t .                                                                .\n"
         echo -en "\t .  more...                   pg 1 of 3                           .\n"
         echo -en "\t .----------------------------------------------------------------.\n"
         echo -en "\n\t\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 . \"dd\" is the UNIX command to make a file or partition image.    .\n"
         echo -en "\t .  The \"dd\" command uses IF (in file) and OF (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 .  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 partion 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 .  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 3                           .\n"
         echo -en "\t .----------------------------------------------------------------.\n"
         echo -en "\n\t\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 the program listing. Here is how the    .\n"
         echo -en "\t .  actual \"dd\" command looks using variables...                  .\n"
         echo -en "\t .  $YELLOW  dd if=/dev/\$IMAGE_FROM of=/dev/\$IMAGE_TO  $OFF                  .\n"
         echo -en "\t .  UNIX is very generious in file system support, so this        .\n"
         echo -en "\t .  script can be used for SATA, SCSII, and USB drives too. I     .\n"
         echo -en "\t .  have not tried other devices such as memory cards, but I      .\n"
         echo -en "\t .  can not see any reason they should not work just as well.     .\n"
         echo -en "\t .                                                                .\n"
         echo -en "\t .  Here are some examples I use with my 250gb development sys.   .\n"
         echo -en "\t .  IMAGE_FROM=hda,  IMAGE_TO=hdc    # entire \"A\" to \"C\" drive    .\n"
         echo -en "\t .  IMAGE_FROM=hda1, IMAGE_TO=hdc1   # MS Windows partition       .\n"
         echo -en "\t .  IMAGE_FROM=hda7, IMAGE_TO=hdc7   # linux partition            .\n"
         echo -en "\t .                                                                .\n"
         echo -en "\t .  Future enhancements will probably include a modest menu       .\n"
         echo -en "\t .  so that manual text editing is not needed to select the       .\n"
         echo -en "\t .  file systems. 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 3 of 3                             .\n"
         echo -en "\t .----------------------------------------------------------------.\n"
         echo -en "\n\t\t     Press $YELLOW [ANY]$OFF key to continue... "
         FkeyRead
         clear ;;

      5|[QqXx]) # we are here because the user wanted to quit
         clear         
         echo -en "\n\n"
         echo -en "\t\t      OK! I am quiting...\n"
         echo -en "\n\t\t      Press $YELLOW [ANY]$OFF key to exit... "
         FkeyRead
         echo -en "\n\n"
         exit 1;;
         
      *) # the users 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\t   Press $YELLOW [ANY]$OFF key to try again... "
         FkeyRead
         echo -en "\n\n"
         clear ;;
   esac
done
exit 1


Последний раз редактировалось jwzumwalt; на 01-05-2009 03:53 PM..
Old 01-03-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell программист, автор
  
 

Регистрация: Mar 2007
Место проведения: Торонто, Канада
Сообщений: 2374
Цитата:
Первоначально Послано jwzumwalt View Post
Я полный привод копию моего диска 250GB примерно в 2-1/2hrs с этой программой.

Я могу сделать полную резервную копию моего диска 250GB (около 230GB занят) примерно в 2-1/4hrs с этой командой:


Код:
tar czf music.tgz /public/music

Он имеет преимущества по сравнению с вашего скрипта в том, что она не требует второе одинаково размера раздела, чтобы скопировать его.

Еще одно преимущество заключается в том, что она будет использовать меньше пространства, чем при использовании дд.
Цитата:
Не стесняйтесь предлагать предложения
@ мозгаудаляются reborg


Код:
#! /bin/bash 
# name: drive-image.sh, disk image copy script
# by:   Jan Zumwalt - www.neatinfo.com 
# ver:  01/01/09
# remarks:
# -e = enable interpretation of backslash codes
# -n = disable newline at end of line (i.e. user input prompt)

IMAGE_FROM=hda  # the drive or partition we will copy (save). 
IMAGE_TO=hdc    # the target (were it will be saved)

Я считаю, это очень странно, что вы жесткий код разделов в интерактивном сценарии.
Цитата:
# Pretty ANSI письменной

ВЫКЛ \u003d "\ 033 [0m"


Код:
OFF=$'\e[0m'

Цитата:
Bold \u003d "\ 033 [1М"
DIM \u003d "\ 033 [2m"
Желтый \u003d "\ 033 [1; 33m"

Желтый текст неразборчив в моем окне терминала.
Цитата:
в то время как:
делать
ясный
эхо-эн "\ N"

Необычное!

Почему вы подавить строки, а затем добавить один?

Что случилось с простым эхо?


Код:
echo

Цитата:
эхо-эн "\ T \ T \ T $ смелые Drive Image Копировать \ N"
эхо-эн "\ т \ т \ т по: Январь Zumwalt \ N"
эхо-эн "\ T \ T \ T $ ЖЕЛТАЯ NeatInfo.com - Версия 01/01/09 $ ВЫКЛ \ N \ N"
эхо-ан ".----------------- Здесь вы смонтирован СМИ -----------------. \ N "
эхо-эн ".. \ N"
DF [/ код]

Зачем беспокоиться, отображающий все разделы, если вы не предлагаете пользователю способ выбрать любой из них?
Цитата:
Код:
####snip####
read ANSWER
   case $ANSWER in 
      [1])

Вам не нужны скобки.

Код:
   case $ANSWER in 
      1)

И не нужно перенаправлять каждой строке. Любое использование printf:

Код:
 
           printf "%s\n" \
              "[Desktop Entry]"                 \
              "Comment="                        \
       #### snip ####
              "X-KDE-SubstituteUID=false"       \
              "X-KDE-Username="                 > "dr-image.desktop"
           chmod  ugo+x "dr-image.desktop" # make shortcut executable

Или скобки:


Код:
           {
           echo "[Desktop Entry]"
           echo "Comment="
           echo "Comment[en_US]="
       #### snip ####
           echo "X-KDE-SubstituteUID=false"
           echo "X-KDE-Username="
           }  > "dr-image.desktop"

Closed Thread

Закладки

Резьба Инструменты Искать в этом Thread
Искать в этом Thread:

Расширенный поиск
Режимы дисплея Оценить эту ветку
Оценить эту ветку:

Отправка Правила
Вы не может Начать новую нитей
Вы не может Почтовые ответы
Вы не может Почтовые вложения
Вы не может Редактировать Ваши сообщения

BB код быть На
Смайлики являемся На
[IMG] код На
HTML-код Вне
Trackbacks являемся На
Pingbacks являемся На
Refbacks являемся На




Часовой пояс GMT -4. Текущее время 03:57 AM.


Powered By: Справка, Copyright © 2000 - 2006, Jelsoft Enterprises Limited. Язык Переводы на питание от .
vBCredits v1.4 Copyright © 2007 - 2008, PixelFX Studios
В UNIX и Linux форумы Содержание Copyright © 1993-2009. Все права Reserved.Ad управления по RedTyger

Содержание соответствующие URL-адреса в vBSEO 3.2.0