LINUX.ORG.RU

История изменений

Исправление zzdnx, (текущая версия) :

Я могу привести сюда хоть весь код скрипта. Просто не считал нужным делать это без реальной необходимости.

Только подсветка синтаксиса уехала... Отключил.

#!/bin/bash

# This script backup VirtualBox guest(s)

#######################
###    VARIABLES    ###
#######################

sudo_user=rem				# User for manage virtual machines
VBD=/home/vbox				# VirtualBox directory
share=/mnt/archive2			# Mount share for backup
target=/mnt/archive2/vbox		# Target directory for backup

#######################
###    FUNCTIONS    ###
#######################

function getvar () { 
  case "$1" in
# Set group of virtual machines:
    all | ALL ) array=(AP LA PB RD KS) ;;

# Set virtual machine:
#    LAMP )
#          folder="$VBD/$1" ;
#          config_file="$1.vbox" ;
#          hdd_file="$1.vdi" ;;

# Automatically get parameters for virtual machine: 
    * )   su $sudo_user -c "VBoxManage showvminfo \"$1\" 2>>/dev/null >>/dev/null" && \
            folder="`su $sudo_user -c "VBoxManage showvminfo \"$1\"      2>>/dev/null" | grep 'Log'    | sed 's/.* \//\//g;s/\/Logs$//g'`" && \
            config_file="`su $sudo_user -c "VBoxManage showvminfo \"$1\" 2>>/dev/null" | grep ' file:' | sed 's/.*\/\(.*\).*/\1/g'`" && \
            hdd_file="`su $sudo_user -c "VBoxManage showvminfo \"$1\"    2>>/dev/null" | grep "(UUID"  | sed 's/.*\/\(.*\) (.*/\1/g'`" || \
            vm_not_exist=1 && if [[ ! -z "$vm_not_exist" ]] ; then echo -e "Not found VM with name $1." ; fi ;;
  esac
}

function backup () { 
# echo "Backup Virtual Machine $1..."
  if [ "$folder" != "/" ] ; then
    if [ ! -d "$folder/backup" ] ; then
      mkdir "$folder/backup" && chmod -R 777 "$folder/backup"
    fi ; 
  fi
  if [ ! -d "$folder" ] ;      then   echo -e "Error: Target 'folder' is not exist. Exit.\n"       && exit 0 ; else cd "$folder" ; fi
  if [ ! -f "$config_file" ] ; then   echo -e "Error: Target 'config_file' is not exist. Exit.\n"  && exit 0 ; fi
  if [ ! -f "$hdd_file" ] ;    then   echo -e "Error: Target 'hdd_file' is not exist. Exit.\n"     && exit 0 ; fi

  logging2file "Stop service $1" && logging "VirtualBox service script returned: `service "$1" stop`" && \
  ( 
    cp "$config_file" backup/"$config_file" 2>>/dev/null
    logging "Start FastCopy (bigsync)..."                 && bigsync -b 1 -s ./"$hdd_file" -d ./backup/"$hdd_file" 2>>/dev/null && \
    logging2file "Start service $1"                       && logging "VirtualBox service script returned: `service "$1" start`" && \
    logging "Make tgz archive from $folder/backup/..."    && cd "$folder"/backup/ && tar -czpf "$target"/"$1"_`date  +%F`.tgz ./ && \
    logging "tgz archive DONE!" && cd ..
  ) || logging2file "debug: \$1=$1, folder=$folder, config_file=$config_file, hdd_file=$hdd_file"
  unset folder config_file hdd_file
}

function logging2file () {
  if [ ! -z "$folder" ] ; then 
    echo "`date "+%F %r "`$@" >>"$folder/backup/backup.log"
  else
    echo "ERROR logging2file"
  fi 
}

function logging2console () { 
  echo "$@"
}

function logging () { 
  logging2file "$@"
  logging2console "$@"
}


#######################
###      CODE       ###
#######################

if [ -z "$1" ] ; then 
  echo -e "Usage:
  # $0 [all | VM1 VM2 … VMn]"
  exit 0
fi

logging2console "Mount share $share ..."
if [ ! -z $share ] ; then mount $share ; fi

while (( "${#}" >= "1" )) ; do
  getvar ${@} >>/dev/null # getvar have to be here for getting virtual machines list
  if [ -z "$array" ] ; then
    getvar "$1" ; if [ -z "$vm_not_exist" ] ; then backup "$1" ; else unset vm_not_exist ; fi
  else 
    for i in ${array[@]} ; do
      getvar "$i" ; if [ -z "$vm_not_exist" ] ; then backup "$i" ; else unset vm_not_exist ; fi
    done
    unset array
  fi
  shift 1 
done

logging2console "Unmount share $share ..."
if [ ! -z $share ] ; then umount $share ; fi

exit 0

О bigsync - это утилита ускоряет копирование больших файлов (читает куски по мегабайту и выполняет запись только когда куски отличаются). Распространяется в виде исходников.

Исправление zzdnx, :

Я могу привести сюда хоть весь код скрипта. Просто не считал нужным делать это без реальной необходимости.

Только подсветка синтаксиса уехала... Отключил.

#!/bin/bash

# This script backup VirtualBox guest(s)

#######################
###    VARIABLES    ###
#######################

sudo_user=rem				# User for manage virtual machines
VBD=/home/vbox				# VirtualBox directory
share=/mnt/archive2			# Mount share for backup
target=/mnt/archive2/vbox		# Target directory for backup

#######################
###    FUNCTIONS    ###
#######################

function getvar () { 
  case "$1" in
# Set group of virtual machines:
    all | ALL ) array=(AP LA PB RD KS) ;;

# Set virtual machine:
#    LAMP )
#          folder="$VBD/$1" ;
#          config_file="$1.vbox" ;
#          hdd_file="$1.vdi" ;;

# Automatically get parameters for virtual machine: 
    * )   su $sudo_user -c "VBoxManage showvminfo \"$1\" 2>>/dev/null >>/dev/null" && \
            folder="`su $sudo_user -c "VBoxManage showvminfo \"$1\"      2>>/dev/null" | grep 'Log'    | sed 's/.* \//\//g;s/\/Logs$//g'`" && \
            config_file="`su $sudo_user -c "VBoxManage showvminfo \"$1\" 2>>/dev/null" | grep ' file:' | sed 's/.*\/\(.*\).*/\1/g'`" && \
            hdd_file="`su $sudo_user -c "VBoxManage showvminfo \"$1\"    2>>/dev/null" | grep "(UUID"  | sed 's/.*\/\(.*\) (.*/\1/g'`" || \
            vm_not_exist=1 && if [[ ! -z "$vm_not_exist" ]] ; then echo -e "Not found VM with name $1." ; fi ;;
  esac
}

function backup () { 
# echo "Backup Virtual Machine $1..."
  if [ "$folder" != "/" ] ; then
    if [ ! -d "$folder/backup" ] ; then
      mkdir "$folder/backup" && chmod -R 777 "$folder/backup"
    fi ; 
  fi
  if [ ! -d "$folder" ] ;      then   echo -e "Error: Target 'folder' is not exist. Exit.\n"       && exit 0 ; else cd "$folder" ; fi
  if [ ! -f "$config_file" ] ; then   echo -e "Error: Target 'config_file' is not exist. Exit.\n"  && exit 0 ; fi
  if [ ! -f "$hdd_file" ] ;    then   echo -e "Error: Target 'hdd_file' is not exist. Exit.\n"     && exit 0 ; fi

  logging2file "Stop service $1" && logging "VirtualBox service script returned: `service "$1" stop`" && \
  ( 
    cp "$config_file" backup/"$config_file" 2>>/dev/null
    logging "Start FastCopy (bigsync)..."                 && bigsync -b 1 -s ./"$hdd_file" -d ./backup/"$hdd_file" 2>>/dev/null && \
    logging2file "Start service $1"                       && logging "VirtualBox service script returned: `service "$1" start`" && \
    logging "Make tgz archive from $folder/backup/..."    && cd "$folder"/backup/ && tar -czpf "$target"/"$1"_`date  +%F`.tgz ./ && \
    logging "tgz archive DONE!" && cd ..
  ) || logging2file "debug: \$1=$1, folder=$folder, config_file=$config_file, hdd_file=$hdd_file"
  unset folder config_file hdd_file
}

function logging2file () {
  if [ ! -z "$folder" ] ; then 
    echo "`date "+%F %r "`$@" >>"$folder/backup/backup.log"
  else
    echo "ERROR logging2file"
  fi 
}

function logging2console () { 
  echo "$@"
}

function logging () { 
  logging2file "$@"
  logging2console "$@"
}


#######################
###      CODE       ###
#######################

if [ -z "$1" ] ; then 
  echo -e "Usage:
  # $0 [all | VM1 VM2 … VMn]"
  exit 0
fi

logging2console "Mount share $share ..."
if [ ! -z $share ] ; then mount $share ; fi

while (( "${#}" >= "1" )) ; do
  getvar ${@} >>/dev/null # getvar have to be here for getting virtual machines list
  if [ -z "$array" ] ; then
    getvar "$1" ; if [ -z "$vm_not_exist" ] ; then backup "$1" ; else unset vm_not_exist ; fi
  else 
    for i in ${array[@]} ; do
      getvar "$i" ; if [ -z "$vm_not_exist" ] ; then backup "$i" ; else unset vm_not_exist ; fi
    done
    unset array
  fi
  shift 1 
done

logging2console "Unmount share $share ..."
if [ ! -z $share ] ; then umount $share ; fi

exit 0

Исходная версия zzdnx, :

Я могу привести сюда хоть весь код скрипта. Просто не считал нужным делать это без реальной необходимости.

Только подсветка синтаксиса уехала... Отключил.

#!/bin/bash

# This script backup VirtualBox guest(s)

#######################
###    VARIABLES    ###
#######################

sudo_user=me				# User for manage virtual machines
VBD=/home/vbox				# VirtualBox directory
share=/mnt/archive2			# Mount share for backup
target=/mnt/archive2/vbox		# Target directory for backup

#######################
###    FUNCTIONS    ###
#######################

function getvar () { 
  case "$1" in
# Set group of virtual machines:
    all | ALL ) array=(AP LA PB RD KS) ;;

# Set virtual machine:
#    LAMP )
#          folder="$VBD/$1" ;
#          config_file="$1.vbox" ;
#          hdd_file="$1.vdi" ;;

# Automatically get parameters for virtual machine: 
    * )   su $sudo_user -c "VBoxManage showvminfo \"$1\" 2>>/dev/null >>/dev/null" && \
            folder="`su $sudo_user -c "VBoxManage showvminfo \"$1\"      2>>/dev/null" | grep 'Log'    | sed 's/.* \//\//g;s/\/Logs$//g'`" && \
            config_file="`su $sudo_user -c "VBoxManage showvminfo \"$1\" 2>>/dev/null" | grep ' file:' | sed 's/.*\/\(.*\).*/\1/g'`" && \
            hdd_file="`su $sudo_user -c "VBoxManage showvminfo \"$1\"    2>>/dev/null" | grep "(UUID"  | sed 's/.*\/\(.*\) (.*/\1/g'`" || \
            vm_not_exist=1 && if [[ ! -z "$vm_not_exist" ]] ; then echo -e "Not found VM with name $1." ; fi ;;
  esac
}

function backup () { 
# echo "Backup Virtual Machine $1..."
  if [ "$folder" != "/" ] ; then
    if [ ! -d "$folder/backup" ] ; then
      mkdir "$folder/backup" && chmod -R 777 "$folder/backup"
    fi ; 
  fi  && logging2file "PREdebug: \$1=$1, folder=$folder, config_file=$config_file, hdd_file=$hdd_file"
  if [ ! -d "$folder" ] ;      then   echo -e "Error: Target 'folder' is not exist. Exit.\n"       && exit 0 ; else cd "$folder" ; fi
  if [ ! -f "$config_file" ] ; then   echo -e "Error: Target 'config_file' is not exist. Exit.\n"  && exit 0 ; fi
  if [ ! -f "$hdd_file" ] ;    then   echo -e "Error: Target 'hdd_file' is not exist. Exit.\n"     && exit 0 ; fi
  logging2file "checkpoint_0"
  logging2file "Stop service $1" && logging "VirtualBox service script returned: `service "$1" stop`" && logging2file "checkpoint_1" && \
  ( 
    cp "$config_file" backup/"$config_file" 2>>/dev/null && logging2file "checkpoint_2"
#   logging "Start FastCopy (bigsync)..."                 && bigsync -b 1 -s ./"$hdd_file" -d ./backup/"$hdd_file" 2>>/dev/null && \
    logging "Start FastCopy (bigsync)..."                 && bigsync -b 1 -s ./"$hdd_file" -d ./backup/"$hdd_file" >$folder/backup/BigSync.log 2>&1 && logging2file "checkpoint_3" && \
    logging2file "Start service $1"                       && logging "VirtualBox service script returned: `service "$1" start`" && logging2file "checkpoint_4" && \
    logging "Make tgz archive from $folder/backup/..."    && cd "$folder"/backup/ && tar -czpf "$target"/"$1"_`date  +%F`.tgz ./ && logging2file "checkpoint_5" && \
    logging "tgz archive DONE!" && logging2file "checkpoint_FINAL" && cd ..
  ) || logging2file "debug: \$1=$1, folder=$folder, config_file=$config_file, hdd_file=$hdd_file"
  unset folder config_file hdd_file
}

function logging2file () {
  if [ ! -z "$folder" ] ; then 
    echo "`date "+%F %r "`$@" >>"$folder/backup/backup.log"
  else
    echo "ERROR logging2file"
  fi 
}

function logging2console () { 
  echo "$@"
}

function logging () { 
  logging2file "$@"
  logging2console "$@"
}


#######################
###      CODE       ###
#######################

if [ -z "$1" ] ; then 
  echo -e "Usage:
  # $0 [all | VM1 VM2 … VMn]"
  exit 0
fi

logging2console "Mount share $share ..."
if [ ! -z $share ] ; then mount $share ; fi

while (( "${#}" >= "1" )) ; do
  getvar ${@} >>/dev/null # getvar have to be here for getting virtual machines list
  if [ -z "$array" ] ; then
#    backup_status=
    getvar "$1" ; if [ -z "$vm_not_exist" ] ; then backup "$1" ; else unset vm_not_exist ; fi
  else 
    for i in ${array[@]} ; do
      getvar "$i" ; if [ -z "$vm_not_exist" ] ; then backup "$i" ; else unset vm_not_exist ; fi
    done
    unset array
  fi
  shift 1 
done

logging2console "Unmount share $share ..."
if [ ! -z $share ] ; then umount $share ; fi

exit 0