Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate

Advertising

belt16ag
Tuesday, November 18th, 2008 at 6:27:31pm UTC 

  1. #Written by Manuel Iglesias. [email protected]
  2. #Notes:
  3. SystemDirectory=/sbin
  4. # This file should be copied (by CopySystemFiles) to its corresponding Directory (see above).
  5. # Exit codes at the end of this file.
  6.  
  7. CommandName=`basename $0`
  8.  
  9.  
  10.  
  11. #########################################################
  12. # BACKUP SETUP. BEGIN. Read Dar Doc before modification.
  13. #########################################################
  14. # Permissions.
  15. ##################
  16. # Allow use only in run level 1.
  17. CheckRunLevel=false
  18. #
  19. # Allow use only by root (Super user).
  20. CheckUser=true
  21. #
  22. #########################################################
  23. # Paths and files.
  24. ##################
  25. # Directories.
  26. #########
  27. # Backup files Directory: Absolute path (Should start with '/'!!). Don't end it with '/' unless it is '/'.
  28. DestinationDir=/media/Backup/gabriel/daily
  29. #
  30. # Origin of Backup/Restore Directory: Absolute path (Should start with '/'!!).
  31. # Don't end it with '/' unless it is '/'.
  32. OriginDir=/
  33. #
  34. # Directories to backup. Relative to Origin of Backup Dir! Empty means: all dirs
  35. # (Except those in Directories to ignore. See below.). Separate with spaces.
  36. SubDirsToBackup="home"
  37. #
  38. # Directories to ignore. Relative to Origin of Backup Dir! Separate with spaces.
  39. SubDirsToIgnore=" */.Trash* .Trash*\
  40.  */.mozilla/*/[Cc]ache */.opera/[Cc]ache* */.pan/*/[Cc]ache */.thumbnails */.VirtualBox */vbox */.sheep */.mcop */backgrounds"
  41. #
  42. # DestinationDir will be automatically included in SubDirsToIgnore if DestinationDir is a subdirectory
  43. # of OriginDir. If you want to include the base (IE.: Temp if DestinationDir: OriginDir/Temp/Backup) of
  44. # DestinationDir instead, set constant IgnoreBaseOfDestinationDir to true. Value (true | false).
  45. IgnoreBaseOfDestinationDir=true
  46. #
  47. # File systems that should be mounted for a correct backup. If any of them has to be mounted,
  48. # it will be umounted before this shellscript exits. Please mind mounting order!!
  49. # Absolute path (Should start with '/'!!). Separate with spaces.
  50. DirsToMount=""
  51. #
  52. ##################
  53. # Files.
  54. #########
  55. # Files to backup. Empty: all files (Except those in Files to ignore. See below.).
  56. # No Path. Separate with spaces.
  57. FilesToBackup=""
  58. #
  59. # Files that should not be included in backup. No Path. Separate with spaces.
  60. FilesToIgnore="*~ .*~ cryptfile0.crypt cryptfile1.crypt"
  61. #
  62. # Files that should not to be compressed. No Path. Separate with spaces.
  63. FilesNotToCompress="*.dar *.crypt *.arj *.bz2 *.bz *.Z *.tgz *.taz *.cpio *.deb\
  64.  *.gtar *.gz *.lzh *.lhz *.rar *.rpm *.shar *.sv4cpi *.sv4crc *.tar *.ustar *.zoo\
  65.  *.zip *.jar *.jpg *.gif *.mpg *.mpeg *.avi *.ram *.rm"
  66. #
  67. #########################################################
  68. # Parameters used to choose Differential Backup level.
  69. ##################
  70. BlockSize=1024
  71. #
  72. # When Diffbackup > (MaxDiffPercentOfFullBackup% of FullBackup): New FullBackup recommended.
  73. MaxDiffPercentOfFullBackup=30
  74. #
  75. # When Diffbackup < (MinDiffPercentOfFullBackup% of FullBackup): Rewrite first DiffBackup recommended.
  76. MinDiffPercentOfFullBackup=3
  77. #
  78. # Max 99. If (Nr of DiffBackups) > MaxNrOfDiffBackups: Rewrite first DiffBackup recommended.
  79. MaxNrOfDiffBackups=20
  80. #
  81. #########################################################
  82. # Dar settings and options.
  83. ##################
  84. #Used dar suite program names.
  85. DarManagerName=dar_manager
  86. DarName=dar
  87. #
  88. # Directory where dar usually resides. Absolute path (Should start with '/'!!). Don't end it with '/'.
  89. DarDir=/usr/local/bin
  90. #
  91. # Create empty sub-directories in backup instead of those not saved. Value (true | false).
  92. BackupIgnoredDirsEmpty=true
  93. #
  94. # CompressWithBZip2=false -> no compression. Value (true | false).
  95. CompressWithBZip2=true
  96. #
  97. # Compress Files > 100Mb. Only valid if CompressWithBZip2=true. Value (true | false).
  98. CompressBigFiles=true
  99. #
  100. # Value (true | false).
  101. VerboseMode=true
  102. #
  103. # Value (true | false).
  104. MakeSlices=true
  105. #
  106. # StopAfterSlices: Only valid if MakeSlices=true. Value (true | false).
  107. StopAfterSlices=false
  108. #
  109. # SizeOfDarStatic: dar_static + DocFiles + Restore shell + etc (To calculate first slize size).
  110. SizeOfDarStatic=4
  111. #
  112. SliceSize=4000
  113. #
  114. #########################################################
  115. # BACKUP SETUP. END.  Read Dar Doc before modification.
  116. #########################################################
  117.  
  118.  
  119.  
  120. #########################################################
  121. # SUBROUTINES. BEGIN.
  122. #########################################################
  123.  
  124. echoE()
  125. {
  126. # echo to standard error. Remove leading/trailing blanks and double spaces.
  127. echo $* 1>&2
  128.         return 0
  129. }
  130.  
  131. Usage()
  132. {
  133. echoE "$CommandName creates (Using '$DarName'), in directory"
  134. echoE "'$DestinationDir',"
  135. echoE "a backup of all files and directories in"
  136. echoE "'$OriginDir'."
  137. echoE "It analyzes current backup files and recommends the most suitable new"
  138. echoE "backup level to the user. It also creates/updates a database with backup"
  139. echoE "information for future Backup management (Using '$DarManagerName')."
  140. echoE
  141. echoE "The backup will be split in files of $SliceSize Mb to fit in removable media."
  142. echoE
  143. echoE "Usage: $CommandName. (User can choose backup level)."
  144. echoE "or"
  145. echoE "Usage: $CommandName -auto. ($CommandName selects backup level automatically)."
  146. echoE
  147.         return 0
  148. }
  149.  
  150. UmountDirs ()
  151. {
  152. if [ "$DirsToUMount" != "" ]
  153.   then
  154.     echo "############"
  155.     echo "$CommandName: Unmounting file systems:"
  156.     for i in $DirsToUMount
  157.       do
  158.         mount | grep -w $i &> /dev/null
  159.         if [ $? -eq 0 ]
  160.           then
  161.             if (umount $i &> /dev/null)
  162.               then
  163.                 echo "$CommandName: $i unmounted."
  164.               else
  165.                 echoE "$CommandName: $i could not be unmounted."
  166.             fi
  167.           else
  168.             echo "$CommandName: $i was already unmounted."
  169.         fi
  170.       done
  171. fi
  172. echo "############"
  173.         return 0
  174. }
  175.  
  176. TwoDigits ()
  177. {
  178. #Add leftmost 0
  179. if [ $1 -lt 10 ]
  180.   then
  181.     echo 0$1
  182.   else
  183.     echo $1
  184. fi
  185.         return 0
  186. }
  187.  
  188. Stream()
  189. {
  190. # Output String(s) without letting the Shell interpret metacharacters.
  191. # Remove leading/trailing blanks and double spaces.
  192. # Enclose arguments in "" when calling. I.E.: Stream "$Var1 $Var2"
  193. Length=${#TempStr}
  194.   if [ $Length -eq 0 ]
  195.     then
  196.       return
  197.     else
  198.       CharNum=0
  199.       while [ $CharNum -lt $Length ]
  200.         do
  201.           echo -n "${TempStr:$CharNum:1}"
  202.           let CharNum++
  203.         done
  204.       echo
  205.   fi
  206. return
  207. }
  208.  
  209. #########################################################
  210. # SUBROUTINES. END.
  211. #########################################################
  212.  
  213.  
  214.  
  215. NoUserChoice=false
  216. if [ $# -ne 0 ]
  217.   then
  218.     if [ "$1" == "-auto" ]
  219.       then
  220.         NoUserChoice=true
  221.       else
  222.         Usage
  223.         exit 1
  224.     fi
  225. fi
  226.  
  227. if $CheckRunLevel
  228.   then
  229.     RunLevel=`runlevel | sed 's/.* //'`
  230.     if [ $RunLevel != S ]
  231.       then
  232.         echoE "$CommandName: RunLevel: $RunLevel. Please change to RunLevel 1 (init 1) and try again."
  233.         exit 1
  234.     fi
  235. fi
  236.  
  237. if $CheckUser
  238.   then
  239.     CurrentUser=`whoami`
  240.     if [ "$CurrentUser" != "root" ]
  241.       then
  242.         echoE "$CommandName: User: '$CurrentUser'. Please login as 'root' and try again."
  243.         exit 1
  244.     fi
  245. fi
  246. echo "############"
  247.  
  248. DirsToUMount=""
  249. if [ "$DirsToMount" != "" ]
  250.   then
  251.     echo "$CommandName: Mounting file systems:"
  252.     for i in $DirsToMount
  253.       do
  254.         mount | grep -w $i &> /dev/null
  255.         if [ $? -ne 0 ]
  256.           then
  257.             if (mount $i &> /dev/null)
  258.               then
  259.                 echo "$CommandName: $i mounted."
  260.                 DirsToUMount=" $i"$DirsToUMount
  261.               else
  262.                 echoE "$CommandName: $i could not be mounted. Aborting."
  263.                 UmountDirs
  264.                 exit 2
  265.             fi
  266.           else
  267.             echo "$CommandName: $i was already mounted."
  268.         fi
  269.       done
  270.     echo "############"
  271. fi
  272.  
  273. if [ "$OriginDir" != "/" ]
  274.   then
  275. #   if first character is not '/'.
  276.     if [ "${OriginDir:0:1}" != "/" ]
  277.       then
  278.         echoE "$CommandName: 'Origin' directory:"
  279.         echoE "$CommandName: $OriginDir."
  280.         echoE "$CommandName: Must be an absolute path (Should start with '/'!)."
  281.         echoE "$CommandName: Please edit '$CommandName' and try again."
  282.         UmountDirs
  283.         exit 3
  284.       else
  285. #       if last character is '/'.
  286.         if [ "${OriginDir:${#OriginDir}-1:1}" == "/" ]
  287.           then
  288.             echoE "$CommandName: 'Origin' directory:"
  289.             echoE "$CommandName: $OriginDir."
  290.             echoE "$CommandName: Should not end with '/'!."
  291.             echoE "$CommandName: Please edit '$CommandName' and try again."
  292.             UmountDirs
  293.             exit 3
  294.           else
  295.             if test ! -d $OriginDir
  296.               then
  297.                 echoE "$CommandName: 'Origin' directory:"
  298.                 echoE "$CommandName: $OriginDir."
  299.                 echoE "$CommandName: Does not exist. Please edit '$CommandName' and try again."
  300.                 UmountDirs
  301.                 exit 3
  302.             fi
  303.         fi
  304.     fi
  305. fi
  306.  
  307. if [ "$DestinationDir" != "/" ]
  308.   then
  309. #   if first character is not '/'.
  310.     if [ "${DestinationDir:0:1}" != "/" ]
  311.       then
  312.         echoE "$CommandName: 'DestinationDir' directory:"
  313.         echoE "$CommandName: $DestinationDir."
  314.         echoE "$CommandName: Must be an absolute path (Should start with '/'!)."
  315.         echoE "$CommandName: Please edit '$CommandName' and try again."
  316.         UmountDirs
  317.         exit 3
  318.       else
  319. #       if last character is '/'.
  320.         if [ "${DestinationDir:${#DestinationDir}-1:1}" == "/" ]
  321.           then
  322.             echoE "$CommandName: 'DestinationDir' directory:"
  323.             echoE "$CommandName: $DestinationDir."
  324.             echoE "$CommandName: Should not end with '/'!."
  325.             echoE "$CommandName: Please edit '$CommandName' and try again."
  326.             UmountDirs
  327.             exit 3
  328.           else
  329.             if test ! -d $DestinationDir
  330.               then
  331.                 echoE "$CommandName: 'DestinationDir' directory:"
  332.                 echoE "$CommandName: $DestinationDir."
  333.                 echoE "$CommandName: Does not exist. Please edit '$CommandName' and try again."
  334.                 UmountDirs
  335.                 exit 3
  336.             fi
  337.         fi
  338.     fi
  339. fi
  340.  
  341. if [ $OriginDir == $DestinationDir ]
  342.   then
  343.     echoE "$CommandName: 'DestinationDir' and 'OriginDir' can not be the same directory!"
  344.     echoE "$CommandName: Please edit '$CommandName' and try again."
  345.     UmountDirs
  346.     exit 3
  347. fi
  348.  
  349. # Find dar & dar_manager
  350. if type >/dev/null 2>&1 $DarName
  351.   then
  352.     DarFound=true
  353.   else
  354.     DarFound=false
  355. fi
  356. if type >/dev/null 2>&1 $DarManagerName
  357.   then
  358.     DarManagerFound=true
  359.   else
  360.     DarManagerFound=false
  361. fi
  362. if ! ($DarFound && $DarManagerFound)
  363.   then
  364.     if [ "$DarDir" != "/" ]
  365.       then
  366. #       if first character is not '/'.
  367.         if [ "${DarDir:0:1}" != "/" ]
  368.           then
  369.             echoE "$CommandName: 'DarDir' directory:"
  370.             echoE "$CommandName: $DarDir."
  371.             echoE "$CommandName: Must be an absolute path (Should start with '/'!)."
  372.             echoE "$CommandName: Please edit '$CommandName' and try again."
  373.             UmountDirs
  374.             exit 3
  375.           else
  376. #           if last character is '/'.
  377.             if [ "${DarDir:${#DarDir}-1:1}" == "/" ]
  378.               then
  379.                 echoE "$CommandName: 'DarDir' directory:"
  380.                 echoE "$CommandName: $DarDir."
  381.                 echoE "$CommandName: Should not end with '/'!."
  382.                 echoE "$CommandName: Please edit '$CommandName' and try again."
  383.                 UmountDirs
  384.                 exit 3
  385.               else
  386.                 if test ! -d $DarDir
  387.                   then
  388.                     echoE "$CommandName: 'DarDir' directory:"
  389.                     echoE "$CommandName: $DarDir."
  390.                     echoE "$CommandName: Does not exist. Please edit '$CommandName' and try again."
  391.                     UmountDirs
  392.                     exit 3
  393.                 fi
  394.             fi
  395.         fi
  396.     fi
  397. #   Include directory, where dar usually resides, in PATH."
  398. #   DarDir not in PATH?
  399.     echo $PATH | grep $DarDir &> /dev/null
  400.     if [ $? -ne 0 ]
  401.       then
  402.         PATH=$DarDir":"$PATH
  403.     fi
  404. fi
  405. if ! type >/dev/null 2>&1 $DarName
  406.   then
  407.     echoE "$CommandName: $DarName neither in PATH nor in $DarDir. Aborting."
  408.     UmountDirs
  409.     exit 3
  410. fi
  411. if ! type >/dev/null 2>&1 $DarManagerName
  412.   then
  413.     echoE "$CommandName: $DarManagerName neither in PATH nor in $DarDir. Aborting."
  414.     UmountDirs
  415.     exit 3
  416. fi
  417.  
  418. #########################################################
  419. # VARIABLES INITIALIZATION. BEGIN.
  420. #########################################################
  421. # Backup Paths.
  422. ###############
  423. #Backup base names & DataBase name.
  424. FullBackupBaseName=$CommandName"Full"
  425. DiffBackupBaseName=$CommandName"Diff"
  426. DataBaseName=$CommandName"DataBase"
  427. #
  428. FullBackupPath=$DestinationDir/$FullBackupBaseName
  429. DiffBackupPath=$DestinationDir/$DiffBackupBaseName
  430. DataBasePath=$DestinationDir/$DataBaseName
  431. #
  432. #########################################################
  433. # Set dar options.
  434. ###############
  435. # Backup base name (Will be set later): -c PathBackUpBaseName
  436. BackupNameOption="-c "
  437. #
  438. # Reference backup (Will be set later) for differential backups: -A PathBackUpBaseName
  439. ReferenceBackupOption="-A "
  440. #
  441. # Origin of Backup: -R /.
  442. DarOptions="-R "$OriginDir
  443. #
  444. # Compress data inside the backup using bzip2: -y[CompressLevel].
  445. # CompressLevel: 0 minimum; 9 maximun. Compress Files > 100Mb: -m 0.
  446. if $CompressWithBZip2
  447.   then
  448.     DarOptions=$DarOptions" -y9"
  449.     if $CompressBigFiles
  450.       then
  451.         DarOptions=$DarOptions" -m 0"
  452.     fi
  453. fi
  454. #
  455. # Verbose mode: -v
  456. if $VerboseMode
  457.   then
  458.     DarOptions=$DarOptions" -v"
  459. fi
  460. #
  461. # Create empty sub-directories in backup instead of those not saved: -D
  462. if $BackupIgnoredDirsEmpty
  463.   then
  464.     DarOptions=$DarOptions" -D"
  465. fi
  466. #
  467. # Do not read ~/.darrc nor /etc/darrc configuration file: -N
  468. DarOptions=$DarOptions" -N"
  469. #
  470. #########################################################
  471. #Set Slice options.
  472. ###############
  473. if [ $SliceSize -gt $SizeOfDarStatic ]
  474.   then
  475.     let FirstSliceSize=$SliceSize-$SizeOfDarStatic
  476.   else
  477.     FirstSliceSize=$SliceSize
  478. fi
  479. #
  480. # All sizes in Mb; Stop after each slize.
  481. if $MakeSlices
  482.   then
  483.     FirstSliceSizeOption="-S "$FirstSliceSize"M"
  484.     SliceSizeOption="-s "$SliceSize"M"
  485. #   Pause between slices to change removable media. Ring bell: -p -b
  486.     if $StopAfterSlices
  487.       then
  488.         DarOptions=$DarOptions" -p -b"
  489.     fi
  490.   else
  491.     FirstSliceSizeOption=""
  492.     SliceSizeOption=""
  493. fi
  494. #
  495. #########################################################
  496. #Set Include/Exclude Files Options.
  497. ###############
  498. # Files you don't want to backup: -X "*~" -X ".*~"
  499. if [ "$FilesToIgnore" != "" ]
  500.   then
  501.     InclExclFilesOption='-X "'`Stream "$FilesToIgnore" |  sed 's/ /" -X "/g'`'"'
  502.   else
  503.     InclExclFilesOption=""
  504. fi
  505. #
  506. # Files you want to backup without compression: -Z "*.zip"
  507. if $CompressWithBZip2
  508.   then
  509.     if [ "$FilesNotToCompress" != "" ]
  510.       then
  511.         InclExclFilesOption=$InclExclFilesOption' -Z "'`Stream "$FilesNotToCompress" |  sed 's/ /" -Z "/g'`'"'
  512.     fi
  513. fi
  514. #
  515. #  Files to include in backup: -I "*.html".
  516. if [ "$FilesToBackup" != "" ]
  517.   then
  518.     InclExclFilesOption=' -I "'`Stream "$FilesToBackup" |  sed 's/ /" -I "/g'`'" '$InclExclFilesOption
  519. fi
  520. #
  521. #########################################################
  522. #Set Include/Exclude directories Options.
  523. ###############
  524. # $OriginDir in $DestinationDir?
  525. echo $DestinationDir | grep $OriginDir &> /dev/null
  526. if [ $? -eq 0 ]
  527.   then
  528. #   TempDir= $DestinationDir-$OriginDir
  529.     TempDir=`echo $DestinationDir | sed s%$OriginDir%%`
  530.     if $IgnoreBaseOfDestinationDir
  531.       then
  532. #       Include BaseDir of DestinationDir (Without first '/') in SubDirsToIgnore.
  533. #       if first character, in TempDir, is not '/'.
  534.         if [ "${DestinationDir:0:1}" != "/" ]
  535.           then
  536. #           Add '/' in front.
  537.             TempDir="/"$TempDir
  538.         fi
  539.         TempPath=$TempDir
  540.         while [ $TempPath != `dirname $TempPath` ]
  541.           do
  542.             BasePath=$TempPath
  543.             TempPath=`dirname $TempPath`
  544.           done
  545.         BasePath=`basename $BasePath`
  546.         if [ "$SubDirsToIgnore" != "" ]
  547.           then
  548.             SubDirsToIgnore=$SubDirsToIgnore" $BasePath"
  549.           else
  550.             SubDirsToIgnore=$BasePath
  551.         fi
  552.       else
  553. #       Include DestinationDir (Without first '/') in SubDirsToIgnore.
  554. #       if first character, in TempDir, is '/'.
  555.         if [ "${TempDir:0:1}" == "/" ]
  556.           then
  557. #           Remove first '/'.
  558.             TempDir=${TempDir:1:${#TempDir}-1}
  559.         fi
  560.         if [ "$SubDirsToIgnore" != "" ]
  561.           then
  562.             SubDirsToIgnore=$SubDirsToIgnore" $TempDir"
  563.           else
  564.             SubDirsToIgnore=$TempDir
  565.         fi
  566.     fi
  567. fi
  568. #
  569. # Sub-trees you must not save: -P dev/pts -P proc. Path must be relative to -R option
  570. # Enclose each directory in "" just in case there are metacharacters in the name.
  571. if [ "$SubDirsToIgnore" != "" ]
  572.   then
  573.     IncludeExclDirsOption='-P "'`Stream "$SubDirsToIgnore" |  sed 's/ /" -P "/g'`'"'
  574.   else
  575.     IncludeExclDirsOption=""
  576. fi
  577. #
  578. #  Sub-trees you must save: Add without any option in front.
  579. # Enclose each directory in "" just in case there are metacharacters in the name.
  580. if [ "$SubDirsToBackup" != "" ]
  581.   then
  582.     IncludeExclDirsOption='-g"'`Stream "$SubDirsToBackup" | sed 's/ /" -g "/g'`'" '$IncludeExclDirsOption
  583. fi
  584. #
  585. #########################################################
  586. # Set dar_manager options.
  587. ###############
  588. # Create DataBase: -C PathBaseName
  589. CreateDataBaseOption="-C "$DataBasePath
  590. #
  591. # DataBase used as reference: -B PathBaseName
  592. DataBaseNameOption="-B "$DataBasePath
  593. #
  594. # Add Archive to DataBase (Will be set later): -A PathArchiveName
  595. AddToDataBaseOption="-A "
  596. #
  597. #########################################################
  598. # VARIABLES INITIALIZATION. END.
  599. #########################################################
  600.  
  601.  
  602.  
  603. FullDiffBackupSize=`ls -1 -s --block-size=$BlockSize $FullBackupPath.* 2> /dev/null | awk '{s = s + $1}
  604.                                                                                                END {print s}'`
  605. if [ "$FullDiffBackupSize" == "" ]
  606.   then
  607.     FullDiffBackupSize=0
  608. fi
  609. TotalDiffBackupSize=`ls -1 -s --block-size=$BlockSize $DiffBackupPath??.* 2> /dev/null | awk '{s = s + $1}
  610.                                                                                                   END {print s}'`
  611. if [ "$TotalDiffBackupSize" == "" ]
  612.   then
  613.     TotalDiffBackupSize=0
  614. fi
  615.  
  616. echo "$CommandName: ### `date --rfc-822` ###"
  617. echo "$CommandName: Current backup information (Size in $BlockSize bytes blocks.):"
  618. if [ $FullDiffBackupSize -eq 0 ]
  619.   then
  620.     echo "$CommandName: No $FullBackupBaseName files found!"
  621.     echo "############"
  622.     echo "$CommandName: Preparing to Create $FullBackupBaseName."
  623.     DiffBackupNr=0
  624.     LastDiffBackup=$DiffBackupNr
  625.   else
  626.     echo "$CommandName: ..$FullBackupBaseName: $FullDiffBackupSize."
  627.     if [ $TotalDiffBackupSize -eq 0 ]
  628.       then
  629.         DiffBackupNr=1
  630.         LastDiffBackup=0
  631.         BaseName=$DiffBackupBaseName`TwoDigits $DiffBackupNr`
  632.         echo "############"
  633.         echo "$CommandName: Preparing to Create $BaseName."
  634.       else
  635.         echo "$CommandName: ..$DiffBackupBaseName: $TotalDiffBackupSize:"
  636.         DiffBackupNr=0
  637.         LastDiffBackup=$DiffBackupNr
  638.         BestChoiceDiffLevel=""
  639.         RemainingDiffSize=$TotalDiffBackupSize
  640.         CurrentSize=1
  641.         while [ $CurrentSize -ne 0 ]
  642.           do
  643.             let DiffBackupNr++
  644.             BaseName=$DiffBackupPath`TwoDigits $DiffBackupNr`
  645.             CurrentSize=`ls -1 -s --block-size=$BlockSize $BaseName.* 2> /dev/null | awk '{s = s + $1}
  646.                                                                                          END {print s}'`
  647.             if [ "$CurrentSize" == "" ]
  648.              then
  649.                CurrentSize=0
  650.             fi
  651.             if [ $CurrentSize -ne 0 ]
  652.               then
  653.                 LastDiffBackup=$DiffBackupNr
  654.                 let RemainingDiffSize=$RemainingDiffSize-$CurrentSize
  655.                 if [ "$BestChoiceDiffLevel" == "" ] && [ $CurrentSize -lt $RemainingDiffSize ]
  656.                   then
  657.                     BestChoiceDiffLevel=$DiffBackupNr
  658.                 fi
  659.                 BaseName=$DiffBackupBaseName`TwoDigits $DiffBackupNr`
  660.                 echo "$CommandName: ....$BaseName: $CurrentSize."
  661.             fi
  662.           done
  663.         echo "############"
  664.         let NextDiffBackup=$LastDiffBackup+1
  665.         if [ "$BestChoiceDiffLevel" == "" ]
  666.           then
  667.             BestChoiceDiffLevel=$NextDiffBackup
  668.         fi
  669.          Choice[4]="Exit $CommandName."
  670.         let MinDiffBackupSize=$FullDiffBackupSize*$MinDiffPercentOfFullBackup/100
  671.         if [ $TotalDiffBackupSize -lt $MinDiffBackupSize ]
  672.           then
  673.             BestChoiceDiffLevel=1
  674.             Choice[1]=" ($DiffBackupBaseName<$MinDiffPercentOfFullBackup%$FullBackupBaseName)."
  675.         fi
  676.         if [ $LastDiffBackup -gt $MaxNrOfDiffBackups ]
  677.           then
  678.             BestChoiceDiffLevel=1
  679.             Choice[1]=${Choice[1]}" (NrOfDiffBackups>$MaxNrOfDiffBackups)."
  680.         fi
  681.         BaseName=$DiffBackupBaseName`TwoDigits $BestChoiceDiffLevel`
  682.         Choice[1]=" $BaseName."${Choice[1]}
  683.         BaseName=$DiffBackupBaseName`TwoDigits $NextDiffBackup`
  684.         Choice[2]="Create $BaseName. Faster."
  685.         Choice[3]="Rewrite $FullBackupBaseName ($DiffBackupBaseName>$MaxDiffPercentOfFullBackup%$FullBackupBaseName). Recommended!"
  686.         let MaxDiffBackupSize=$FullDiffBackupSize*$MaxDiffPercentOfFullBackup/100
  687.         if [ $NextDiffBackup -eq $BestChoiceDiffLevel ]
  688.           then
  689.             if [ $TotalDiffBackupSize -gt $MaxDiffBackupSize ]
  690.               then
  691.                 Choices="1 3"
  692.                 CreateRewriteMode="Create"
  693.                 Choice[1]=${Choice[1]}" Faster."
  694.               else
  695.                 Choices=""
  696.             fi
  697.           else
  698.             CreateRewriteMode="Rewrite"
  699.             if [ $TotalDiffBackupSize -gt $MaxDiffBackupSize ]
  700.               then
  701.                 Choices="1 2 3"
  702.               else
  703.                 Choices="1 2"
  704.                 Choice[1]=${Choice[1]}" Recommended!"
  705.             fi
  706.         fi
  707.         Choice[1]=$CreateRewriteMode${Choice[1]}
  708.         if [ "$Choices" == "" ]
  709.           then
  710.             DiffBackupNr=$BestChoiceDiffLevel
  711.             BaseName=$DiffBackupBaseName`TwoDigits $DiffBackupNr`
  712.             echo "$CommandName: Preparing to Create $BaseName."
  713.           else
  714.             Choices=$Choices" 4"
  715.             echo "$CommandName: Options:"
  716.             ChoiceNr=1
  717.             for i in $Choices
  718.               do
  719.                 echo "$CommandName: $ChoiceNr.${Choice[$i]}"
  720.                 let ChoiceNr++
  721.               done
  722.             echo "############"
  723.             if $NoUserChoice
  724.               then
  725.                 echo $Choices | grep "3" &> /dev/null
  726.                 if [ $? -eq 0 ]
  727.                   then
  728.                     Choice=3
  729.                   else
  730.                     Choice=1
  731.                 fi
  732.               else
  733.                 let ChoiceNr--
  734.                 ValidNumber=false
  735.                 until $ValidNumber
  736.                   do
  737.                     read -p "$CommandName: Please choose a number: " UserChoice
  738.                     case $UserChoice in
  739.                       [a-zA-Z-_.,]* | *[a-zA-Z-_.,] | *[a-zA-Z-_.,]*)
  740.                         echoE "$CommandName: No alpha characters allowed. Please try again.";;
  741.                       "") ;;
  742.                       *)
  743.                         ValidNumber=true;;
  744.                     esac
  745.                     if $ValidNumber
  746.                      then
  747.                        if [ $UserChoice -lt 1 ] || [ $UserChoice -gt $ChoiceNr ]
  748.                          then
  749.                            echoE "$CommandName: Allowed number range: 1..$ChoiceNr. Please try again."
  750.                            ValidNumber=false
  751.                        fi
  752.                     fi
  753.                   done
  754.                 ChoiceNr=0
  755.                 for i in $Choices
  756.                   do
  757.                     let ChoiceNr++
  758.                     if [ $ChoiceNr -eq $UserChoice ]
  759.                      then
  760.                        Choice=$i
  761.                     fi
  762.                   done
  763.                 echo "############"
  764.             fi
  765.             case $Choice in
  766.               1)
  767.                 DiffBackupNr=$BestChoiceDiffLevel
  768.                 BaseName=$DiffBackupBaseName`TwoDigits $DiffBackupNr`
  769.                 echo "$CommandName: Preparing to $CreateRewriteMode $BaseName.";;
  770.               2)
  771.                 DiffBackupNr=$NextDiffBackup
  772.                 BaseName=$DiffBackupBaseName`TwoDigits $DiffBackupNr`
  773.                 echo "$CommandName: Preparing to Create $BaseName.";;
  774.               3)
  775.                 echo "$CommandName: Preparing to Rewrite $FullBackupBaseName."
  776.                 DiffBackupNr=0;;
  777.               4)
  778.                 echoE "$CommandName: Program exits at user request."
  779.                 UmountDirs
  780.                 exit 4;;
  781.               *)
  782.                 echoE "$CommandName:Warning: Incorrect choice: $Choice. Aborting."
  783.                 UmountDirs
  784.                 exit 5;;
  785.             esac
  786.         fi
  787.     fi
  788. fi
  789.  
  790. if [ $DiffBackupNr -eq 0 ]
  791.   then
  792.     FullBackup=true
  793.   else
  794.     FullBackup=false
  795. fi
  796.  
  797. if $FullBackup
  798.   then
  799.     BackupNameOption=$BackupNameOption$FullBackupPath
  800.     if [ $FullDiffBackupSize -ne 0 ]
  801.       then
  802.         echo "############"
  803.         echo "$CommandName: Removing previous $FullBackupBaseName files."
  804.         for i in $FullBackupPath.*
  805.           do
  806.             if test -f $i
  807.               then
  808.                 if (rm $i)
  809.                   then
  810.                     echo "$CommandName: $i removed."
  811.                   else
  812.                     echoE "$CommandName:Warning: Failure to remove $i."
  813.                 fi
  814.             fi
  815.           done
  816.     fi
  817.     if [ $TotalDiffBackupSize -ne 0 ]
  818.       then
  819.         echo "############"
  820.         echo "$CommandName: Removing previous $DiffBackupBaseName files."
  821.         for i in $DiffBackupPath??.*
  822.           do
  823.             if test -f $i
  824.               then
  825.                 if (rm $i)
  826.                   then
  827.                     echo "$CommandName: $i removed."
  828.                   else
  829.                     echoE "$CommandName:Warning: Failure to remove $i."
  830.                 fi
  831.             fi
  832.           done
  833.     fi
  834.     if test -f $DataBasePath
  835.       then
  836.         echo "############"
  837.         echo "$CommandName: Removing previous $DataBaseName file."
  838.         if (rm $DataBasePath)
  839.           then
  840.             echo "$CommandName: $DataBasePath removed."
  841.           else
  842.             echoE "$CommandName:Warning: Failure to remove $DataBasePath."
  843.         fi
  844.     fi
  845.     echo "############"
  846.     echo "$CommandName: creating $FullBackupBaseName. Please wait."
  847.     echo "###"
  848.     sh <<End
  849.     $DarName $FirstSliceSizeOption $SliceSizeOption $BackupNameOption $DarOptions $IncludeExclDirsOption $InclExclFilesOption
  850. End
  851.     ExitCode=$?
  852.     if [ $ExitCode -eq 0 ]
  853.       then
  854.         echo "###"
  855.         echo "$CommandName: $FullBackupBaseName created successfully."
  856.         echo "############"
  857.         echo "$CommandName: Creating $DataBaseName."
  858.         AddToDataBaseOption=$AddToDataBaseOption$FullBackupPath
  859.         if ($DarManagerName $CreateDataBaseOption)
  860.           then
  861.             echo "$CommandName: Adding $FullBackupBaseName to $DataBaseName. Please wait."
  862.             if ($DarManagerName $DataBaseNameOption $AddToDataBaseOption)
  863.               then
  864.                 echo "$CommandName: $FullBackupBaseName added to $DataBaseName successfully."
  865.               else
  866.                 echoE "$CommandName: Warning: Adding $FullBackupBaseName to $DataBaseName failed."
  867.             fi
  868.           else
  869.             echoE "$CommandName: Warning: Creation of $DataBaseName failed."
  870.         fi
  871.       else
  872.         echoE "$CommandName: Sorry. '$DarName' failed with exit code $ExitCode. $FullBackupBaseName backup aborted."
  873.         UmountDirs
  874.     fi
  875.   else
  876.     BackupName=$DiffBackupBaseName`TwoDigits $DiffBackupNr`
  877.     BackupPath=$DiffBackupPath`TwoDigits $DiffBackupNr`
  878.     BackupNameOption=$BackupNameOption$BackupPath
  879.     if [ $DiffBackupNr -gt 1 ]
  880.       then
  881.         PrevDiffBackup=$DiffBackupNr
  882.         let PrevDiffBackup--
  883.         ReferenceBackupOption=$ReferenceBackupOption$DiffBackupPath`TwoDigits $PrevDiffBackup`
  884.       else
  885.         ReferenceBackupOption=$ReferenceBackupOption$FullBackupPath
  886.     fi
  887. #   In fact $DiffBackupNr+1 because FullBackup=1. Set to $DiffBackupNr for use in 'Erase .. from DataBase' loop below.
  888.     DataBaseLastValidArchive=$DiffBackupNr
  889.     if [ $LastDiffBackup -ne 0 ]
  890.       then
  891.         if [ $DiffBackupNr -le $LastDiffBackup ]
  892.           then
  893.             echo "############"
  894.             echo "$CommandName: Removing previous $DiffBackupBaseName files."
  895.         fi
  896.         let DiffBackupNr--
  897.         while [ $DiffBackupNr -lt $LastDiffBackup ]
  898.           do
  899.             let DiffBackupNr++
  900.             BaseName=$DiffBackupPath`TwoDigits $DiffBackupNr`
  901.             for i in $BaseName.*
  902.               do
  903.                 if test -f $i
  904.                   then
  905.                     if (rm $i)
  906.                       then
  907.                         echo "$CommandName: $i removed."
  908.                       else
  909.                         echoE "$CommandName:Warning: Failure to remove $i."
  910.                     fi
  911.                 fi
  912.               done
  913.           done
  914.     fi
  915.     if test -f $DataBasePath
  916.       then
  917. #       $DarManagerName -l outputs to standard error.
  918.         LastArchiveInDB=`$DarManagerName $DataBaseNameOption -l 2>&1 | awk 'END {print $1}'`
  919.         case $LastArchiveInDB in
  920.           [a-zA-Z-_.,]* | *[a-zA-Z-_.,] | *[a-zA-Z-_.,]*)
  921. #           If DataBase empty last line produced by 'dar_manager -l' is full of '--'
  922.             echoE "$CommandName: Warning: $DataBaseName is empty. Aborting."
  923.             UmountDirs
  924.             exit 6;;
  925.           *)
  926.             if [ $LastArchiveInDB -gt $DataBaseLastValidArchive ]
  927.               then
  928.                 echo "############"
  929.                 echo "$CommandName: Erasing previous Differential backups from $DataBaseName."
  930.                 while [ $LastArchiveInDB -gt $DataBaseLastValidArchive ]
  931.                   do
  932.                     let ArchiveBaseName=$LastArchiveInDB-1
  933.                     BaseName=`TwoDigits $ArchiveBaseName`
  934.                     ArchiveBaseName=$DiffBackupBaseName$BaseName
  935.                     if ($DarManagerName $DataBaseNameOption -D $LastArchiveInDB)
  936.                       then
  937.                         echo "$CommandName: Archive $LastArchiveInDB ($ArchiveBaseName) erased from $DataBaseName."
  938.                       else
  939.                         echoE "$CommandName: Warning: Erasing of Archive $LastArchiveInDB ($ArchiveBaseName)\
  940.                                                                                   from $DataBaseName failed."
  941.                     fi
  942.                     let LastArchiveInDB--
  943.                   done
  944.             fi;;
  945.         esac
  946.       else
  947.         echoE "$CommandName: Warning! $DataBaseName does not exist. Aborting."
  948.         UmountDirs
  949.         exit 7
  950.     fi
  951.     echo "############"
  952.     echo "$CommandName: creating $BackupName. Please wait."
  953.     echo "###"
  954.     sh <<End
  955.     $DarName $SliceSizeOption $BackupNameOption $ReferenceBackupOption $DarOptions $IncludeExclDirsOption $InclExclFilesOption
  956. End
  957.     ExitCode=$?
  958.     if [ $ExitCode -eq 0 ]
  959.       then
  960.         echo "###"
  961.         echo "$CommandName: $BackupName created successfully."
  962.         echo "############"
  963.         echo "$CommandName: Updating $DataBaseName. Please wait."
  964.         AddToDataBaseOption=$AddToDataBaseOption$BackupPath
  965.         if ($DarManagerName $DataBaseNameOption $AddToDataBaseOption)
  966.           then
  967.             echo "$CommandName: $BackupName added to $DataBaseName successfully."
  968.           else
  969.             echoE "$CommandName: Warning: Adding $BackupName to $DataBaseName failed."
  970.         fi
  971.       else
  972.         echoE "$CommandName: Sorry. '$DarName' failed with exit code $ExitCode. $BackupName backup aborted."
  973.     fi
  974. fi
  975.  
  976. UmountDirs
  977.  
  978. exit 0
  979.  
  980. Exit codes:
  981. 0 Sucessful run.
  982. 1 Wrong arguments;
  983.   || Not root;
  984.   || Wrong runlevel.
  985. 2 File system could not be mounted.
  986. 3 Incorrect Origin directory or it does not exist;
  987.   || Incorrect Destination directory or it does not exist;
  988.   || $DarName or $DarManagerName not in $PATH or $DarDir.
  989. 4 User request exit.
  990. 5 Wrong user choice.
  991. 6 Database empty.
  992. 7 Database does not exist.

advertising

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will not expire by default. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

comments powered by Disqus
worth-right