You are not logged in.

#1 2008-05-10 10:58:39

sujoy
Member
From: India
Registered: 2008-02-08
Posts: 94
Website

[SOLVED]Backup the installation.

I spent a lot of time, (over a period of months actually) to set up Arch with awesomeWM just the way I want it to be. Now, incase my hard disk does get corrupt or because of any unforeseen/foreseen circumstances I have to reinstall Arch, it would break my heart literaly.

Hence, a backup solution would be good, such that I can just install Arch again from the installation CD and restore it to my present configuration with all apps installed and configured as they are, from a backup media (preferably DVD or CD).

Now will it work if I just backup all the directories in a data DVD/CD and after a clean installation, just copy them in their place? Or is there a more elegant solution to this?

Last edited by sujoy (2008-05-11 06:12:39)

Offline

#2 2008-05-10 14:43:30

bradym
Arch Linux f@h Team Member
From: /dev/null
Registered: 2008-04-13
Posts: 9

Re: [SOLVED]Backup the installation.

You could use something like clonezilla (available from http://www.clonezilla.org/) to create an image of your hard drive, then if anything happens, just restore a new hard drive from the image.

Offline

#3 2008-05-10 14:59:16

sujoy
Member
From: India
Registered: 2008-02-08
Posts: 94
Website

Re: [SOLVED]Backup the installation.

oh well, it says only used blocks, does it mean that the backup wont work if i resize my partitions?

Offline

#4 2008-05-10 15:06:53

atasmrk
Member
Registered: 2007-05-07
Posts: 23

Re: [SOLVED]Backup the installation.

One possibility is to boot from a live cd and tar up your current installation.
That is how i did it.

Offline

#5 2008-05-10 15:58:28

_Marco_
Member
Registered: 2008-04-21
Posts: 242

Re: [SOLVED]Backup the installation.

atasmrk wrote:

One possibility is to boot from a live cd and tar up your current installation.
That is how i did it.

I use this method too, just remember to use the option to preserve permissions (or you'll backup useless files)

Offline

#6 2008-05-10 16:42:51

bradym
Arch Linux f@h Team Member
From: /dev/null
Registered: 2008-04-13
Posts: 9

Re: [SOLVED]Backup the installation.

I've used clonezilla in the past, and if I remember right, there's an option to backup the partition table, so I would guess you could restore the image to a larger partition, but I'm not positive.

Offline

#7 2008-05-10 17:56:20

sujoy
Member
From: India
Registered: 2008-02-08
Posts: 94
Website

Re: [SOLVED]Backup the installation.

_Marco_ wrote:
atasmrk wrote:

One possibility is to boot from a live cd and tar up your current installation.
That is how i did it.

I use this method too, just remember to use the option to preserve permissions (or you'll backup useless files)

should i back up the proc/ sys/ also?

Offline

#8 2008-05-10 20:25:42

Leigh
Member
From: USA
Registered: 2004-06-25
Posts: 533

Re: [SOLVED]Backup the installation.

I have a usb hard drive that I use specifically for a backup. It's a clone of my main system and I can use it to rescue my main system in case something bad happens. What I did, was use the arch install cd, and partition the usb hd the same as the main system, and let it create the same file systems as I use for the main system. Then I skipped down to install boot loader to put Grub in the mbr of sdc. Then I exited the install and from a live cd I copied/cloned my main system to the usb drive. for example main system is sda, and usb is sdc, and I mount them from a live cd or secondary HD install, say mounted under /mnt as sda1, sda3, sdc1, sdc3...
cp -a -v /mnt/sda1/* /mnt/sdc1
cp -a -v /mnt/sda3/* /mnt/sdc3

The -a switch is to copy recursively to preserve permissions. -v is verbose

Afterwards, The fstab and grubs menu.lst will need edited to reflect the usb as sdc, or add a entry to boot the usb sdc disk. Then I'm able to plug in and boot the usb sdc disk and it's an exact clone of my main system.

I did it this way because my main system constantly changes, and if it's just little changes. I'll plug in and boot the usb install and incorporate those changes, update with pacman, etc... If it's totally major changes, or totally outdated, I'll just rm -r everything in each partition and cp -a all over again. This is certainly not the easiest way to maintain a backup, but for me it's really handy having a bootable clone of my system on stand by in case something bad happens smile

Last edited by Leigh (2008-05-10 20:43:04)


-- archlinux 是一个极好的 linux

Offline

#9 2008-05-10 20:38:46

SpookyET
Member
Registered: 2008-01-27
Posts: 410

Re: [SOLVED]Backup the installation.

There are a few methods to back up your installation.

tar, rsync, rsync-based scripts, duplicity, rdiff-backup, SafeKeep (pretty cool, and based on rdiff-backup), rsnapshot.

There are nice things and bad things about each one. Out of all mentioned, I would recommend Safekeep or rsnapshot.

Unfortunately, they are pull, instead of push. So, you'll need a server.
Right now, I'm evaluating rdup, which looks pretty cool. It's push-based and very flexible.

Dar is also very good, but you'll need a script:

#!/bin/sh

#Written by Manuel Iglesias. glesialo@tiscali.es
#Notes:
SystemDirectory=/sbin
# This file should be copied (by CopySystemFiles) to its corresponding Directory (see above).
# Exit codes at the end of this file.

CommandName=`basename $0`



#########################################################
# BACKUP SETUP. BEGIN. Read Dar Doc before modification.
#########################################################
# Permissions.
##################
# Allow use only in run level 1.
CheckRunLevel=false
#
# Allow use only by root (Super user).
CheckUser=true
#
#########################################################
# Paths and files.
##################
# Directories.
#########
# Backup files Directory: Absolute path (Should start with '/'!!). Don't end it with '/' unless it is '/'.
DestinationDir=/var/backup
#
# Origin of Backup/Restore Directory: Absolute path (Should start with '/'!!).
# Don't end it with '/' unless it is '/'.
OriginDir=/
#
# Directories to backup. Relative to Origin of Backup Dir! Empty means: all dirs
# (Except those in Directories to ignore. See below.). Separate with spaces.
SubDirsToBackup="/dev/console /dev/zero /dev/null"
#
# Directories to ignore. Relative to Origin of Backup Dir! Separate with spaces.
SubDirsToIgnore=" \
    **/.thumbnails \ 
    **/.mozilla/firefox/*/Cache \
    **/.Trash \
    **/.emerald/themecache \
    **/.fontconfig/*.cache* \
    **/.java/deployment/cache \
    **/amarok/albumcovers/cache \
    **/amarok/albumcovers/large \
    **/.liferea*/mozilla/liferea/Cache \
    **/.liferea*/cache \
    **/.macromedia/Flash_Player/*SharedObjects \
    **/.macromedia/Macromedia/Flash\ Player/*SharedObjects \
    **/.metacity/sessions \
    **/.nautilus/saved* \
    **/.mythtv/osdcache \
    **/.mythtv/themecache \
    **/var/cache \
    **/workspace/.metadata \
    **/.openoffice.org2/user/registry/cache \
    **/.openoffice.org2/user/uno_packages/cache \
    **/.grails/*/scriptCache \
    **/.wine/drive_c/windows/temp \
    **/lost+found \
    **/root/.synaptic/log \
    **/.Trash \
    **/Downloads \
    **/Packages/abs/*/src \
    **/Packages/abs/*/pkg \
    **/Packages/abs/*/*.gz \
    **/Packages/abs/*/*.bz2 \
    dev \
    media/* \
    mnt \
    proc \
    sys \
    tmp \
    var/log \
    var/tmp"
#
# DestinationDir will be automatically included in SubDirsToIgnore if DestinationDir is a subdirectory
# of OriginDir. If you want to include the base (IE.: Temp if DestinationDir: OriginDir/Temp/Backup) of
# DestinationDir instead, set constant IgnoreBaseOfDestinationDir to true. Value (true | false).
IgnoreBaseOfDestinationDir=false
#
# File systems that should be mounted for a correct backup. If any of them has to be mounted,
# it will be umounted before this shellscript exits. Please mind mounting order!!
# Absolute path (Should start with '/'!!). Separate with spaces.
DirsToMount="/var/backup"
#
##################
# Files.
#########
# Files to backup. Empty: all files (Except those in Files to ignore. See below.).
# No Path. Separate with spaces.
FilesToBackup=""
#
# Files that should not be included in backup. No Path. Separate with spaces.
FilesToIgnore="*~ .*~ cryptfile0.crypt cryptfile1.crypt"
#
# Files that should not to be compressed. No Path. Separate with spaces.
FilesNotToCompress="*.dar *.crypt *.arj *.bz2 *.bz *.Z *.tgz *.taz *.cpio *.deb\
 *.gtar *.gz *.lzh *.lhz *.rar *.rpm *.shar *.sv4cpi *.sv4crc *.tar *.ustar *.zoo\
 *.zip *.jar *.jpg *.gif *.mpg *.mpeg *.avi *.ram *.rm *.mp4 .mov *.wmv *.mkv"
#
#########################################################
# Parameters used to choose Differential Backup level.
##################
BlockSize=1024
#
# When Diffbackup > (MaxDiffPercentOfFullBackup% of FullBackup): New FullBackup recommended.
MaxDiffPercentOfFullBackup=30
#
# When Diffbackup < (MinDiffPercentOfFullBackup% of FullBackup): Rewrite first DiffBackup recommended.
MinDiffPercentOfFullBackup=3
#
# Max 99. If (Nr of DiffBackups) > MaxNrOfDiffBackups: Rewrite first DiffBackup recommended.
MaxNrOfDiffBackups=20
#
#########################################################
# Dar settings and options.
##################
#Used dar suite program names.
DarManagerName=dar_manager
DarName=dar
#
# Directory where dar usually resides. Absolute path (Should start with '/'!!). Don't end it with '/'.
DarDir=/usr/bin
#
# Create empty sub-directories in backup instead of those not saved. Value (true | false).
BackupIgnoredDirsEmpty=true
#
# CompressWithBZip2=false -> no compression. Value (true | false).
CompressWithBZip2=true
#
# Compress Files > 100Mb. Only valid if CompressWithBZip2=true. Value (true | false).
CompressBigFiles=true
#
# Value (true | false).
VerboseMode=true
#
# Value (true | false).
MakeSlices=true
#
# StopAfterSlices: Only valid if MakeSlices=true. Value (true | false).
StopAfterSlices=false
#
# SizeOfDarStatic: dar_static + DocFiles + Restore shell + etc (To calculate first slize size).
SizeOfDarStatic=4
#
SliceSize=650
#
#########################################################
# BACKUP SETUP. END.  Read Dar Doc before modification.
#########################################################



#########################################################
# SUBROUTINES. BEGIN.
#########################################################

echoE()
{
# echo to standard error. Remove leading/trailing blanks and double spaces.
echo $* 1>&2
    return 0
}

Usage()
{
echoE "$CommandName creates (Using '$DarName'), in directory"
echoE "'$DestinationDir',"
echoE "a backup of all files and directories in"
echoE "'$OriginDir'. It analyzes current backup files and recommends the most suitable new backup level to the user. It also creates/updates a database with backup information for future Backup management (Using '$DarManagerName')." 
echoE "The backup will be split in files of $SliceSize Mb to fit in removable media."
echoE
echoE "Usage: $CommandName. (User can choose backup level)."
echoE "or"
echoE "Usage: $CommandName -auto. ($CommandName selects backup level automatically)."
echoE 
    return 0
}

UmountDirs ()
{
if [ "$DirsToUMount" != "" ]
  then
    echo "############"
    echo "$CommandName: Unmounting file systems:"
    for i in $DirsToUMount
      do
        mount | grep -w $i &> /dev/null
        if [ $? -eq 0 ]
          then
            if (umount $i &> /dev/null)
              then
                echo "$CommandName: $i unmounted."
              else
                echoE "$CommandName: $i could not be unmounted."
            fi
          else
            echo "$CommandName: $i was already unmounted."
        fi
      done
fi
echo "############"
    return 0
}

TwoDigits ()
{
#Add leftmost 0
if [ $1 -lt 10 ]
  then
    echo 0$1
  else
    echo $1
fi
    return 0
}

Stream()
{
# Output String(s) without letting the Shell interpret metacharacters.
# Remove leading/trailing blanks and double spaces.
# Enclose arguments in "" when calling. I.E.: Stream "$Var1 $Var2"
TempStr=$@
Length=${#TempStr}
  if [ $Length -eq 0 ]
    then
      return
    else
      CharNum=0
      while [ $CharNum -lt $Length ]
        do
          echo -n "${TempStr:$CharNum:1}"
          let CharNum++
        done
      echo
  fi
return
}

#########################################################
# SUBROUTINES. END.
#########################################################



NoUserChoice=false
if [ $# -ne 0 ]
  then
    if [ "$1" == "-auto" ]
      then
        NoUserChoice=true
      else
        Usage
        exit 1
    fi
fi

if $CheckRunLevel
  then
    RunLevel=`runlevel | sed 's/.* //'`
    if [ $RunLevel != S ]
      then
        echoE "$CommandName: RunLevel: $RunLevel. Please change to RunLevel 1 (init 1) and try again."
        exit 1
    fi
fi

if $CheckUser
  then
    CurrentUser=`whoami`
    if [ "$CurrentUser" != "root" ]
      then
        echoE "$CommandName: User: '$CurrentUser'. Please login as 'root' and try again."
        exit 1
    fi
fi
echo "############"

DirsToUMount=""
if [ "$DirsToMount" != "" ]
  then
    echo "$CommandName: Mounting file systems:"
    for i in $DirsToMount
      do
        mount | grep -w $i &> /dev/null
        if [ $? -ne 0 ]
          then
            if (mount $i &> /dev/null)
              then
                echo "$CommandName: $i mounted."
                DirsToUMount=" $i"$DirsToUMount
              else
                echoE "$CommandName: $i could not be mounted. Aborting."
                UmountDirs
                exit 2
            fi
          else
            echo "$CommandName: $i was already mounted."
        fi
      done
    echo "############"
fi

if [ "$OriginDir" != "/" ]
  then
#   if first character is not '/'.
    if [ "${OriginDir:0:1}" != "/" ]
      then
        echoE "$CommandName: 'Origin' directory:"
        echoE "$CommandName: $OriginDir."
        echoE "$CommandName: Must be an absolute path (Should start with '/'!)."
        echoE "$CommandName: Please edit '$CommandName' and try again."
        UmountDirs
        exit 3
      else
#       if last character is '/'.
        if [ "${OriginDir:${#OriginDir}-1:1}" == "/" ]
          then
            echoE "$CommandName: 'Origin' directory:"
            echoE "$CommandName: $OriginDir."
            echoE "$CommandName: Should not end with '/'!."
            echoE "$CommandName: Please edit '$CommandName' and try again."
            UmountDirs
            exit 3
          else
            if test ! -d $OriginDir
              then
                echoE "$CommandName: 'Origin' directory:"
                echoE "$CommandName: $OriginDir."
                echoE "$CommandName: Does not exist. Please edit '$CommandName' and try again."
                UmountDirs
                exit 3
            fi
        fi
    fi
fi

if [ "$DestinationDir" != "/" ]
  then
#   if first character is not '/'.
    if [ "${DestinationDir:0:1}" != "/" ]
      then
        echoE "$CommandName: 'DestinationDir' directory:"
        echoE "$CommandName: $DestinationDir."
        echoE "$CommandName: Must be an absolute path (Should start with '/'!)."
        echoE "$CommandName: Please edit '$CommandName' and try again."
        UmountDirs
        exit 3
      else
#       if last character is '/'.
        if [ "${DestinationDir:${#DestinationDir}-1:1}" == "/" ]
          then
            echoE "$CommandName: 'DestinationDir' directory:"
            echoE "$CommandName: $DestinationDir."
            echoE "$CommandName: Should not end with '/'!."
            echoE "$CommandName: Please edit '$CommandName' and try again."
            UmountDirs
            exit 3
          else
            if test ! -d $DestinationDir
              then
                echoE "$CommandName: 'DestinationDir' directory:"
                echoE "$CommandName: $DestinationDir."
                echoE "$CommandName: Does not exist. Please edit '$CommandName' and try again."
                UmountDirs
                exit 3
            fi
        fi
    fi
fi

if [ $OriginDir == $DestinationDir ]
  then
    echoE "$CommandName: 'DestinationDir' and 'OriginDir' can not be the same directory!"
    echoE "$CommandName: Please edit '$CommandName' and try again."
    UmountDirs
    exit 3
fi

# Find dar & dar_manager
if type >/dev/null 2>&1 $DarName
  then
    DarFound=true
  else
    DarFound=false
fi
if type >/dev/null 2>&1 $DarManagerName
  then
    DarManagerFound=true
  else
    DarManagerFound=false
fi
if ! ($DarFound && $DarManagerFound)
  then
    if [ "$DarDir" != "/" ]
      then
#       if first character is not '/'.
        if [ "${DarDir:0:1}" != "/" ]
          then
            echoE "$CommandName: 'DarDir' directory:"
            echoE "$CommandName: $DarDir."
            echoE "$CommandName: Must be an absolute path (Should start with '/'!)."
            echoE "$CommandName: Please edit '$CommandName' and try again."
            UmountDirs
            exit 3
          else
#           if last character is '/'.
            if [ "${DarDir:${#DarDir}-1:1}" == "/" ]
              then
                echoE "$CommandName: 'DarDir' directory:"
                echoE "$CommandName: $DarDir."
                echoE "$CommandName: Should not end with '/'!."
                echoE "$CommandName: Please edit '$CommandName' and try again."
                UmountDirs
                exit 3
              else
                if test ! -d $DarDir
                  then
                    echoE "$CommandName: 'DarDir' directory:"
                    echoE "$CommandName: $DarDir."
                    echoE "$CommandName: Does not exist. Please edit '$CommandName' and try again."
                    UmountDirs
                    exit 3
                fi
            fi
        fi
    fi
#   Include directory, where dar usually resides, in PATH."
#   DarDir not in PATH?
    echo $PATH | grep $DarDir &> /dev/null
    if [ $? -ne 0 ]
      then
        PATH=$DarDir":"$PATH
    fi
fi
if ! type >/dev/null 2>&1 $DarName
  then
    echoE "$CommandName: $DarName neither in PATH nor in $DarDir. Aborting."
    UmountDirs
    exit 3
fi
if ! type >/dev/null 2>&1 $DarManagerName
  then
    echoE "$CommandName: $DarManagerName neither in PATH nor in $DarDir. Aborting."
    UmountDirs
    exit 3
fi

#########################################################
# VARIABLES INITIALIZATION. BEGIN.
#########################################################
# Backup Paths.
###############
#Backup base names & DataBase name.
FullBackupBaseName=$CommandName"Full"
DiffBackupBaseName=$CommandName"Diff"
DataBaseName=$CommandName"DataBase"
#
FullBackupPath=$DestinationDir/$FullBackupBaseName
DiffBackupPath=$DestinationDir/$DiffBackupBaseName
DataBasePath=$DestinationDir/$DataBaseName
#
#########################################################
# Set dar options.
###############
# Backup base name (Will be set later): -c PathBackUpBaseName
BackupNameOption="-c "
#
# Reference backup (Will be set later) for differential backups: -A PathBackUpBaseName
ReferenceBackupOption="-A "
#
# Origin of Backup: -R /.
DarOptions="-R "$OriginDir
#
# Compress data inside the backup using bzip2: -y[CompressLevel].
# CompressLevel: 0 minimum; 9 maximun. Compress Files > 100Mb: -m 0.
if $CompressWithBZip2
  then
    DarOptions=$DarOptions" -y9"
    if $CompressBigFiles
      then
        DarOptions=$DarOptions" -m 0"
    fi
fi
#
# Verbose mode: -v
if $VerboseMode
  then
    DarOptions=$DarOptions" -v"
fi
#
# Create empty sub-directories in backup instead of those not saved: -D
if $BackupIgnoredDirsEmpty
  then
    DarOptions=$DarOptions" -D"
fi
#
# Do not read ~/.darrc nor /etc/darrc configuration file: -N
DarOptions=$DarOptions" -N"
#
#########################################################
#Set Slice options.
###############
if [ $SliceSize -gt $SizeOfDarStatic ]
  then
    let FirstSliceSize=$SliceSize-$SizeOfDarStatic
  else
    FirstSliceSize=$SliceSize
fi
#
# All sizes in Mb; Stop after each slize.
if $MakeSlices
  then
    FirstSliceSizeOption="-S "$FirstSliceSize"M"
    SliceSizeOption="-s "$SliceSize"M"
#   Pause between slices to change removable media. Ring bell: -p -b
    if $StopAfterSlices
      then
        DarOptions=$DarOptions" -p -b"
    fi
  else
    FirstSliceSizeOption=""
    SliceSizeOption=""
fi
#
#########################################################
#Set Include/Exclude Files Options.
###############
# Files you don't want to backup: -X "*~" -X ".*~"
if [ "$FilesToIgnore" != "" ]
  then
    InclExclFilesOption='-X "'`Stream "$FilesToIgnore" |  sed 's/ /" -X "/g'`'"'
  else
    InclExclFilesOption=""
fi
#
# Files you want to backup without compression: -Z "*.zip"
if $CompressWithBZip2
  then
    if [ "$FilesNotToCompress" != "" ]
      then
        InclExclFilesOption=$InclExclFilesOption' -Z "'`Stream "$FilesNotToCompress" |  sed 's/ /" -Z "/g'`'"'
    fi
fi
#
#  Files to include in backup: -I "*.html".
if [ "$FilesToBackup" != "" ]
  then
    InclExclFilesOption=' -I "'`Stream "$FilesToBackup" |  sed 's/ /" -I "/g'`'" '$InclExclFilesOption
fi
#
#########################################################
#Set Include/Exclude directories Options.
###############
# $OriginDir in $DestinationDir?
echo $DestinationDir | grep $OriginDir &> /dev/null
if [ $? -eq 0 ]
  then
#   TempDir= $DestinationDir-$OriginDir
    TempDir=`echo $DestinationDir | sed s%$OriginDir%%`
    if $IgnoreBaseOfDestinationDir
      then
#       Include BaseDir of DestinationDir (Without first '/') in SubDirsToIgnore.
#       if first character, in TempDir, is not '/'.
        if [ "${DestinationDir:0:1}" != "/" ]
          then
#           Add '/' in front.
            TempDir="/"$TempDir
        fi
        TempPath=$TempDir
        while [ $TempPath != `dirname $TempPath` ]
          do
            BasePath=$TempPath
            TempPath=`dirname $TempPath`
          done
        BasePath=`basename $BasePath`
        if [ "$SubDirsToIgnore" != "" ]
          then
            SubDirsToIgnore=$SubDirsToIgnore" $BasePath"
          else
            SubDirsToIgnore=$BasePath
        fi
      else
#       Include DestinationDir (Without first '/') in SubDirsToIgnore.
#       if first character, in TempDir, is '/'.
        if [ "${TempDir:0:1}" == "/" ]
          then
#           Remove first '/'.
            TempDir=${TempDir:1:${#TempDir}-1}
        fi
        if [ "$SubDirsToIgnore" != "" ]
          then
            SubDirsToIgnore=$SubDirsToIgnore" $TempDir"
          else
            SubDirsToIgnore=$TempDir
        fi
    fi
fi
#
# Sub-trees you must not save: -P dev/pts -P proc. Path must be relative to -R option
# Enclose each directory in "" just in case there are metacharacters in the name.
if [ "$SubDirsToIgnore" != "" ]
  then
    IncludeExclDirsOption='-P "'`Stream "$SubDirsToIgnore" |  sed 's/ /" -P "/g'`'"'
  else
    IncludeExclDirsOption=""
fi
#
#  Sub-trees you must save: Add without any option in front.
# Enclose each directory in "" just in case there are metacharacters in the name.
if [ "$SubDirsToBackup" != "" ]
  then
    IncludeExclDirsOption='-g"'`Stream "$SubDirsToBackup" | sed 's/ /" -g "/g'`'" '$IncludeExclDirsOption
fi
#
#########################################################
# Set dar_manager options.
###############
# Create DataBase: -C PathBaseName
CreateDataBaseOption="-C "$DataBasePath
#
# DataBase used as reference: -B PathBaseName
DataBaseNameOption="-B "$DataBasePath
#
# Add Archive to DataBase (Will be set later): -A PathArchiveName
AddToDataBaseOption="-A "
#
#########################################################
# VARIABLES INITIALIZATION. END.
#########################################################



FullDiffBackupSize=`ls -1 -s --block-size=$BlockSize $FullBackupPath.* 2> /dev/null | awk '{s = s + $1}
                                                                                               END {print s}'`
if [ "$FullDiffBackupSize" == "" ]
  then
    FullDiffBackupSize=0
fi
TotalDiffBackupSize=`ls -1 -s --block-size=$BlockSize $DiffBackupPath??.* 2> /dev/null | awk '{s = s + $1}
                                                                                                  END {print s}'`
if [ "$TotalDiffBackupSize" == "" ]
  then
    TotalDiffBackupSize=0
fi

echo "$CommandName: ### `date --rfc-822` ###"
echo "$CommandName: Current backup information (Size in $BlockSize bytes blocks.):"
if [ $FullDiffBackupSize -eq 0 ]
  then
    echo "$CommandName: No $FullBackupBaseName files found!"
    echo "############"
    echo "$CommandName: Preparing to Create $FullBackupBaseName."
    DiffBackupNr=0
    LastDiffBackup=$DiffBackupNr
  else
    echo "$CommandName: ..$FullBackupBaseName: $FullDiffBackupSize."
    if [ $TotalDiffBackupSize -eq 0 ]
      then
        DiffBackupNr=1
        LastDiffBackup=0
        BaseName=$DiffBackupBaseName`TwoDigits $DiffBackupNr`
        echo "############"
        echo "$CommandName: Preparing to Create $BaseName."
      else
        echo "$CommandName: ..$DiffBackupBaseName: $TotalDiffBackupSize:"
        DiffBackupNr=0
        LastDiffBackup=$DiffBackupNr
        BestChoiceDiffLevel=""
        RemainingDiffSize=$TotalDiffBackupSize
        CurrentSize=1
        while [ $CurrentSize -ne 0 ]
          do
            let DiffBackupNr++
            BaseName=$DiffBackupPath`TwoDigits $DiffBackupNr`
            CurrentSize=`ls -1 -s --block-size=$BlockSize $BaseName.* 2> /dev/null | awk '{s = s + $1}
                                                                                         END {print s}'`
            if [ "$CurrentSize" == "" ]
             then
               CurrentSize=0
            fi
            if [ $CurrentSize -ne 0 ]
              then
                LastDiffBackup=$DiffBackupNr
                let RemainingDiffSize=$RemainingDiffSize-$CurrentSize
                if [ "$BestChoiceDiffLevel" == "" ] && [ $CurrentSize -lt $RemainingDiffSize ]
                  then
                    BestChoiceDiffLevel=$DiffBackupNr
                fi
                BaseName=$DiffBackupBaseName`TwoDigits $DiffBackupNr`
                echo "$CommandName: ....$BaseName: $CurrentSize."
            fi
          done
        echo "############"
        let NextDiffBackup=$LastDiffBackup+1
        if [ "$BestChoiceDiffLevel" == "" ]
          then
            BestChoiceDiffLevel=$NextDiffBackup
        fi
         Choice[4]="Exit $CommandName."
        let MinDiffBackupSize=$FullDiffBackupSize*$MinDiffPercentOfFullBackup/100
        if [ $TotalDiffBackupSize -lt $MinDiffBackupSize ]
          then
            BestChoiceDiffLevel=1
            Choice[1]=" ($DiffBackupBaseName<$MinDiffPercentOfFullBackup%$FullBackupBaseName)."
        fi
        if [ $LastDiffBackup -gt $MaxNrOfDiffBackups ]
          then
            BestChoiceDiffLevel=1
            Choice[1]=${Choice[1]}" (NrOfDiffBackups>$MaxNrOfDiffBackups)."
        fi
        BaseName=$DiffBackupBaseName`TwoDigits $BestChoiceDiffLevel`
        Choice[1]=" $BaseName."${Choice[1]}
        BaseName=$DiffBackupBaseName`TwoDigits $NextDiffBackup`
        Choice[2]="Create $BaseName. Faster."
        Choice[3]="Rewrite $FullBackupBaseName ($DiffBackupBaseName>$MaxDiffPercentOfFullBackup%$FullBackupBaseName). Recommended!"
        let MaxDiffBackupSize=$FullDiffBackupSize*$MaxDiffPercentOfFullBackup/100
        if [ $NextDiffBackup -eq $BestChoiceDiffLevel ]
          then
            if [ $TotalDiffBackupSize -gt $MaxDiffBackupSize ]
              then
                Choices="1 3"
                CreateRewriteMode="Create"
                Choice[1]=${Choice[1]}" Faster."
              else
                Choices=""
            fi
          else
            CreateRewriteMode="Rewrite"
            if [ $TotalDiffBackupSize -gt $MaxDiffBackupSize ]
              then
                Choices="1 2 3"
              else
                Choices="1 2"
                Choice[1]=${Choice[1]}" Recommended!"
            fi
        fi
        Choice[1]=$CreateRewriteMode${Choice[1]}
        if [ "$Choices" == "" ]
          then
            DiffBackupNr=$BestChoiceDiffLevel
            BaseName=$DiffBackupBaseName`TwoDigits $DiffBackupNr`
            echo "$CommandName: Preparing to Create $BaseName."
          else
            Choices=$Choices" 4"
            echo "$CommandName: Options:"
            ChoiceNr=1
            for i in $Choices
              do
                echo "$CommandName: $ChoiceNr.${Choice[$i]}"
                let ChoiceNr++
              done
            echo "############"
            if $NoUserChoice
              then
                echo $Choices | grep "3" &> /dev/null
                if [ $? -eq 0 ]
                  then
                    Choice=3
                  else
                    Choice=1
                fi
              else
                let ChoiceNr--
                ValidNumber=false
                until $ValidNumber
                  do
                    read -p "$CommandName: Please choose a number: " UserChoice
                    case $UserChoice in
                      [a-zA-Z-_.,]* | *[a-zA-Z-_.,] | *[a-zA-Z-_.,]*)
                        echoE "$CommandName: No alpha characters allowed. Please try again.";;
                      "") ;;
                      *)
                        ValidNumber=true;;
                    esac
                    if $ValidNumber
                     then
                       if [ $UserChoice -lt 1 ] || [ $UserChoice -gt $ChoiceNr ]
                         then
                           echoE "$CommandName: Allowed number range: 1..$ChoiceNr. Please try again."
                           ValidNumber=false
                       fi
                    fi
                  done
                ChoiceNr=0
                for i in $Choices
                  do
                    let ChoiceNr++
                    if [ $ChoiceNr -eq $UserChoice ]
                     then
                       Choice=$i
                    fi
                  done
                echo "############"
            fi
            case $Choice in
              1)
                DiffBackupNr=$BestChoiceDiffLevel
                BaseName=$DiffBackupBaseName`TwoDigits $DiffBackupNr`
                echo "$CommandName: Preparing to $CreateRewriteMode $BaseName.";;
              2)
                DiffBackupNr=$NextDiffBackup
                BaseName=$DiffBackupBaseName`TwoDigits $DiffBackupNr`
                echo "$CommandName: Preparing to Create $BaseName.";;
              3)
                echo "$CommandName: Preparing to Rewrite $FullBackupBaseName."
                DiffBackupNr=0;;
              4)
                echoE "$CommandName: Program exits at user request."
                UmountDirs
                exit 4;;
              *)
                echoE "$CommandName:Warning: Incorrect choice: $Choice. Aborting."
                UmountDirs
                exit 5;;
            esac
        fi
    fi
fi

if [ $DiffBackupNr -eq 0 ]
  then
    FullBackup=true
  else
    FullBackup=false
fi

if $FullBackup
  then
    BackupNameOption=$BackupNameOption$FullBackupPath
    if [ $FullDiffBackupSize -ne 0 ]
      then
        echo "############"
        echo "$CommandName: Removing previous $FullBackupBaseName files."
        for i in $FullBackupPath.*
          do
            if test -f $i
              then
                if (rm $i)
                  then
                    echo "$CommandName: $i removed."
                  else
                    echoE "$CommandName:Warning: Failure to remove $i."
                fi
            fi
          done
    fi
    if [ $TotalDiffBackupSize -ne 0 ]
      then
        echo "############"
        echo "$CommandName: Removing previous $DiffBackupBaseName files."
        for i in $DiffBackupPath??.*
          do
            if test -f $i
              then
                if (rm $i)
                  then
                    echo "$CommandName: $i removed."
                  else
                    echoE "$CommandName:Warning: Failure to remove $i."
                fi
            fi
          done
    fi
    if test -f $DataBasePath
      then
        echo "############"
        echo "$CommandName: Removing previous $DataBaseName file."
        if (rm $DataBasePath)
          then
            echo "$CommandName: $DataBasePath removed."
          else
            echoE "$CommandName:Warning: Failure to remove $DataBasePath."
        fi
    fi
    echo "############"
    echo "$CommandName: creating $FullBackupBaseName. Please wait."
    echo "###"
    sh <<End
    $DarName $FirstSliceSizeOption $SliceSizeOption $BackupNameOption $DarOptions $IncludeExclDirsOption $InclExclFilesOption
End
    ExitCode=$?
    if [ $ExitCode -eq 0 ]
      then
        echo "###"
        echo "$CommandName: $FullBackupBaseName created successfully."
        echo "############"
        echo "$CommandName: Creating $DataBaseName."
        AddToDataBaseOption=$AddToDataBaseOption$FullBackupPath
        if ($DarManagerName $CreateDataBaseOption)
          then
            echo "$CommandName: Adding $FullBackupBaseName to $DataBaseName. Please wait."
            if ($DarManagerName $DataBaseNameOption $AddToDataBaseOption)
              then
                echo "$CommandName: $FullBackupBaseName added to $DataBaseName successfully."
              else
                echoE "$CommandName: Warning: Adding $FullBackupBaseName to $DataBaseName failed."
            fi
          else
            echoE "$CommandName: Warning: Creation of $DataBaseName failed."
        fi
      else
        echoE "$CommandName: Sorry. '$DarName' failed with exit code $ExitCode. $FullBackupBaseName backup aborted."
        UmountDirs
    fi
  else
    BackupName=$DiffBackupBaseName`TwoDigits $DiffBackupNr`
    BackupPath=$DiffBackupPath`TwoDigits $DiffBackupNr`
    BackupNameOption=$BackupNameOption$BackupPath
    if [ $DiffBackupNr -gt 1 ]
      then
        PrevDiffBackup=$DiffBackupNr
        let PrevDiffBackup--
        ReferenceBackupOption=$ReferenceBackupOption$DiffBackupPath`TwoDigits $PrevDiffBackup`
      else
        ReferenceBackupOption=$ReferenceBackupOption$FullBackupPath
    fi
#   In fact $DiffBackupNr+1 because FullBackup=1. Set to $DiffBackupNr for use in 'Erase .. from DataBase' loop below.
    DataBaseLastValidArchive=$DiffBackupNr
    if [ $LastDiffBackup -ne 0 ]
      then
        if [ $DiffBackupNr -le $LastDiffBackup ]
          then
            echo "############"
            echo "$CommandName: Removing previous $DiffBackupBaseName files."
        fi
        let DiffBackupNr--
        while [ $DiffBackupNr -lt $LastDiffBackup ]
          do
            let DiffBackupNr++
            BaseName=$DiffBackupPath`TwoDigits $DiffBackupNr`
            for i in $BaseName.*
              do
                if test -f $i
                  then
                    if (rm $i)
                      then
                        echo "$CommandName: $i removed."
                      else
                        echoE "$CommandName:Warning: Failure to remove $i."
                    fi
                fi
              done
          done
    fi
    if test -f $DataBasePath
      then
#       $DarManagerName -l outputs to standard error.
        LastArchiveInDB=`$DarManagerName $DataBaseNameOption -l 2>&1 | awk 'END {print $1}'`
        case $LastArchiveInDB in
          [a-zA-Z-_.,]* | *[a-zA-Z-_.,] | *[a-zA-Z-_.,]*)
#           If DataBase empty last line produced by 'dar_manager -l' is full of '--'
            echoE "$CommandName: Warning: $DataBaseName is empty. Aborting."
            UmountDirs
            exit 6;;
          *)
            if [ $LastArchiveInDB -gt $DataBaseLastValidArchive ]
              then
                echo "############"
                echo "$CommandName: Erasing previous Differential backups from $DataBaseName."
                while [ $LastArchiveInDB -gt $DataBaseLastValidArchive ]
                  do
                    let ArchiveBaseName=$LastArchiveInDB-1
                    BaseName=`TwoDigits $ArchiveBaseName`
                    ArchiveBaseName=$DiffBackupBaseName$BaseName
                    if ($DarManagerName $DataBaseNameOption -D $LastArchiveInDB)
                      then
                        echo "$CommandName: Archive $LastArchiveInDB ($ArchiveBaseName) erased from $DataBaseName."
                      else
                        echoE "$CommandName: Warning: Erasing of Archive $LastArchiveInDB ($ArchiveBaseName)\
                                                                                  from $DataBaseName failed."
                    fi
                    let LastArchiveInDB--
                  done
            fi;;
        esac
      else
        echoE "$CommandName: Warning! $DataBaseName does not exist. Aborting."
        UmountDirs
        exit 7
    fi
    echo "############"
    echo "$CommandName: creating $BackupName. Please wait."
    echo "###"
    sh <<End
    $DarName $SliceSizeOption $BackupNameOption $ReferenceBackupOption $DarOptions $IncludeExclDirsOption $InclExclFilesOption
End
    ExitCode=$?
    if [ $ExitCode -eq 0 ]
      then
        echo "###"
        echo "$CommandName: $BackupName created successfully."
        echo "############"
        echo "$CommandName: Updating $DataBaseName. Please wait."
        AddToDataBaseOption=$AddToDataBaseOption$BackupPath
        if ($DarManagerName $DataBaseNameOption $AddToDataBaseOption)
          then
            echo "$CommandName: $BackupName added to $DataBaseName successfully."
          else
            echoE "$CommandName: Warning: Adding $BackupName to $DataBaseName failed."
        fi
      else
        echoE "$CommandName: Sorry. '$DarName' failed with exit code $ExitCode. $BackupName backup aborted."
    fi
fi

UmountDirs

exit 0

Exit codes:
0 Sucessful run.
1 Wrong arguments;
  || Not root;
  || Wrong runlevel.
2 File system could not be mounted.
3 Incorrect Origin directory or it does not exist;
  || Incorrect Destination directory or it does not exist;
  || $DarName or $DarManagerName not in $PATH or $DarDir.
4 User request exit.
5 Wrong user choice.
6 Database empty.
7 Database does not exist.

Last edited by SpookyET (2008-05-10 20:40:04)

Offline

#10 2008-05-11 06:11:30

sujoy
Member
From: India
Registered: 2008-02-08
Posts: 94
Website

Re: [SOLVED]Backup the installation.

wow, so many options. thanks guys, i guess my problem is solved. smile

Offline

Board footer

Powered by FluxBB