eurybox.functions.create 9.94 KB
Newer Older
1 2 3 4 5 6
#!/bin/bash
#EuryBOX create functions file

#FIXME: extract function parameters to transfer to control section use and make functions more generic and reusable
eurybox_create_archive_desc ()
{
7 8 9
  local DESC_OUT
  local SYNC_OUT
  local STATUS
10
  local DESCRIPTION
11 12 13 14 15 16 17
#TODO: IMPLEMENT PARALLEL EXTRACTION FOR GZ AND BZ2
  case ${EURYBOX_BACKUP_ARCHIVE[FORMAT]} in
    "tar" ) TAR_OPTIONS="-Stv" ;;
    "tar.gz" ) TAR_OPTIONS="-Stvz" ;;
    "tar.bz2" ) TAR_OPTIONS="-Stvj" ;;
  esac
  STATUS=1234
18
  DESTINATION=${EURYBOX_BACKUP_DESTINATION[MOUNT]}/${EURYBOX_BACKUP_ARCHIVE[NAME]}.desc
19 20 21 22 23 24 25 26 27 28 29 30 31
  DESCRIPTION="ARCHIVE_CONTENT:${EURYBOX_CORPORATE}\nARCHIVE_FILE:${EURYBOX_BACKUP_ARCHIVE[NAME]}\nARCHIVE_FORMAT:${EURYBOX_BACKUP_ARCHIVE[FORMAT]}\nARCHIVE_TARGETS:${EURYBOX_BACKUP_TARGETS[*]}\nARCHIVE_TYPE:${EURYBOX_BACKUP_TYPE}\nARCHIVE_VERSION:${EURYBOX_VERSION}\nECC_FILE:${EURYBOX_BACKUP_ARCHIVE[NAME]}.${EURYBOX_BACKUP_ARCHIVE[ECC]}\nECC_FORMAT:${EURYBOX_BACKUP_ARCHIVE[ECC]}\nECC_LEVEL:${EURYBOX_BACKUP_ARCHIVE[ECC_LEVEL]\%}\nHASH_FILE:${EURYBOX_BACKUP_ARCHIVE[NAME]}.${EURYBOX_BACKUP_ARCHIVE[HASH]}\nENCRYPTED_ARCHIVE:${EURYBOX_BACKUP_ARCHIVE[ENCRYPT]}\n"
#  DESC_OUT=`sudo sh -c "echo ARCHIVE_CONTENT:$EURYBOX_CORPORATE >> $DESTINATION" 2>&1`
#  DESC_OUT=`sudo sh -c "echo ARCHIVE_FILE:${EURYBOX_BACKUP_ARCHIVE[NAME]} >> $DESTINATION" 2>&1`
#  DESC_OUT=`sudo sh -c "echo ARCHIVE_FORMAT:${EURYBOX_BACKUP_ARCHIVE[FORMAT]} >> $DESTINATION" 2>&1`
#  DESC_OUT=`sudo sh -c "echo ARCHIVE_TARGETS:${EURYBOX_BACKUP_TARGETS[*]} >> $DESTINATION" 2>&1`
#  DESC_OUT=`sudo sh -c "echo ARCHIVE_TYPE:$EURYBOX_BACKUP_TYPE >> $DESTINATION" 2>&1`
#  DESC_OUT=`sudo sh -c "echo ARCHIVE_VERSION:$EURYBOX_VERSION >> $DESTINATION" 2>&1`
#  DESC_OUT=`sudo sh -c "echo ECC_FILE:${EURYBOX_BACKUP_ARCHIVE[NAME]}.${EURYBOX_BACKUP_ARCHIVE[ECC]} >> $DESTINATION" 2>&1`
#  DESC_OUT=`sudo sh -c "echo ECC_FORMAT:${EURYBOX_BACKUP_ARCHIVE[ECC]} >> $DESTINATION" 2>&1`
#  DESC_OUT=`sudo sh -c "echo ECC_LEVEL:${EURYBOX_BACKUP_ARCHIVE[ECC_LEVEL]\%} >> $DESTINATION" 2>&1`
#  DESC_OUT=`sudo sh -c "echo HASH_FILE:${EURYBOX_BACKUP_ARCHIVE[NAME]}.${EURYBOX_BACKUP_ARCHIVE[HASH]} >> $DESTINATION" 2>&1`
#  DESC_OUT=`sudo sh -c "echo HASH_FORMAT:${EURYBOX_BACKUP_ARCHIVE[HASH]} >> $DESTINATION" 2>&1`
#  DESC_OUT=`sudo sh -c "echo ENCRYPTED_ARCHIVE:${EURYBOX_BACKUP_ARCHIVE[ENCRYPT]} >> $DESTINATION" 2>&1`
32 33
  if [[ ${EURYBOX_BACKUP_ARCHIVE[ENCRYPT]} = "true" ]]
  then
34
    DESCRIPTION+="ENCRYPTION_ALGORITHM:${EURYBOX_BACKUP_ARCHIVE[ENC_ALGO]}\nARCHIVE_CONTENT:\n"
35
    #FIXME: function to read archive with variable archive format required here
36 37 38 39 40 41
    DESCRIPTION+=`sudo sh -c "openssl enc -${EURYBOX_BACKUP_ARCHIVE[ENC_ALGO]} -d -in ${EURYBOX_BACKUP_DESTINATION[MOUNT]}/${EURYBOX_BACKUP_ARCHIVE[NAME]}.${EURYBOX_BACKUP_ARCHIVE[FORMAT]} -k ${EURYBOX_BACKUP_ARCHIVE[PASSWORD]} | tar ${TAR_OPTIONS}"`
#    DESC_OUT=`sudo sh -c "echo ENCRYPTION_ALGORITHM:${EURYBOX_BACKUP_ARCHIVE[ENC_ALGO]} >> $DESTINATION" 2>&1`
#    DESC_OUT=`sudo sh -c "echo ARCHIVE_CONTENT: >> $DESTINATION" 2>&1`
#    DESC_OUT=$((
#    (
#      sudo sh -c "openssl enc -${EURYBOX_BACKUP_ARCHIVE[ENC_ALGO]} -d -in ${EURYBOX_BACKUP_DESTINATION[MOUNT]}/${EURYBOX_BACKUP_ARCHIVE[NAME]}.${EURYBOX_BACKUP_ARCHIVE[FORMAT]} -k ${EURYBOX_BACKUP_ARCHIVE[PASSWORD]} | tar ${TAR_OPTIONS} >> $DESTINATION"
42
#          STATUS=$(( ${PIPESTATUS[0]} + ${PIPESTATUS[1]} ))
43
#    )) 2>&1)
44 45
    STATUS=$?
  else
46
    DESCRIPTION+="ARCHIVE_CONTENT:\n"
47
    #FIXME: function to read archive with variable archive format required here
48 49 50 51 52 53
    DESCRIPTION+=`sudo sh -c "tar ${TAR_OPTIONS}f ${EURYBOX_BACKUP_DESTINATION[MOUNT]}/${EURYBOX_BACKUP_ARCHIVE[NAME]}.${EURYBOX_BACKUP_ARCHIVE[FORMAT]}"`
#    DESC_OUT=`sudo sh -c "sudo echo ARCHIVE_CONTENT: >> $DESTINATION" 2>&1`
#    DESC_OUT=$((
#    (
#      sudo sh -c "tar ${TAR_OPTIONS}f ${EURYBOX_BACKUP_DESTINATION[MOUNT]}/${EURYBOX_BACKUP_ARCHIVE[NAME]}.${EURYBOX_BACKUP_ARCHIVE[FORMAT]} >> $DESTINATION"
#    )) 2>&1)
54 55
    STATUS=$?
  fi
56 57
  if [[ !($STATUS -eq 0) ]]
  then
58
    eurybox_display_message warning CREATE "Archive description creation failed - error $STATUS:\n$DESCRIPTION"
59
  else
60
    DESC_OUT=`sudo sh -c "printf %b \"$DESCRIPTION\" > $DESTINATION" 2>&1`
61 62 63
    STATUS=$?
    if [[ !($STATUS -eq 0) ]]
    then
64
        eurybox_display_message warning CREATE "Archive description write failed - error $STATUS:\n$DESC_OUT"
65
    else
66 67 68 69 70 71 72 73 74 75 76 77
      eurybox_display_message message CREATE "Archive description creation - OK"
      eurybox_display_message debug CREATE "Description:\n$DESCRIPTION"
      eurybox_display_message debug CREATE "Description command output:\n$DESC_OUT"
      SYNC_OUT=`sudo sync 2>&1`
      STATUS=$?
      if [[ !($STATUS -eq 0) ]]
      then
        eurybox_display_message warning CREATE "Archive description FS sync failed - error $STATUS:\n$SYNC_OUT"
      else
        eurybox_display_message message CREATE "Archive description FS sync - OK"
        eurybox_display_message debug CREATE "Sync command output:\n$SYNC_OUT"
      fi
78
    fi
79 80 81 82 83 84
  fi
}

#FIXME: extract function parameters to transfer to control section use and make functions more generic and reusable
eurybox_create_archive_tar ()
{
85 86 87 88
  local TAR_OUT
  local SYNC_OUT
  local STATUS
  if [[ $EURYBOX_LOG_LEVEL = "debug" || $EURYBOX_LOG_LEVEL = "verbose" ]]
89 90 91 92 93 94 95 96 97 98 99
  then
    TAR_OPTIONS="-v"
  else
    TAR_OPTIONS="-"
  fi
  case ${EURYBOX_BACKUP_ARCHIVE[FORMAT]} in
    "tar" ) TAR_OPTIONS="${TAR_OPTIONS}Sc" ;;
    "tar.gz" ) TAR_OPTIONS="${TAR_OPTIONS}Sc --use-compress-program=pigz" ;;
    "tar.bz2" ) TAR_OPTIONS="${TAR_OPTIONS}Sc --use-compress-program=pbzip2 " ;;
    #xz
  esac
100 101 102 103
  if [[ ${EURYBOX_BACKUP_ARCHIVE[ENCRYPT]} = "true" ]]
  then
    TAR_OUT=$((
    (
104
      sudo sh -c "tar ${TAR_OPTIONS} ${EURYBOX_BACKUP_TARGETS[*]} | openssl enc -salt -e -${EURYBOX_BACKUP_ARCHIVE[ENC_ALGO]} -k ${EURYBOX_BACKUP_ARCHIVE[PASSWORD]} -out ${EURYBOX_BACKUP_DESTINATION[MOUNT]}/${EURYBOX_BACKUP_ARCHIVE[NAME]}.${EURYBOX_BACKUP_ARCHIVE[FORMAT]}"
105
#          STATUS=$(( ${PIPESTATUS[0]} + ${PIPESTATUS[1]} ))
106 107 108
    )) 2>&1)
    STATUS=$?
  else
109
    TAR_OUT=`sudo sh -c "tar ${TAR_OPTIONS} -f ${EURYBOX_BACKUP_DESTINATION[MOUNT]}/${EURYBOX_BACKUP_ARCHIVE[NAME]}.${EURYBOX_BACKUP_ARCHIVE[FORMAT]} ${EURYBOX_BACKUP_TARGETS[*]}" 2>&1`
110 111
    STATUS=$?
  fi
112 113
  if [[ !($STATUS -eq 0) ]]
  then
114
    eurybox_display_message error CREATE "Archive creation failed - error: $STATUS:\n$TAR_OUT"
115 116
  else
    eurybox_display_message message CREATE "Archive creation - OK"
117 118 119 120 121 122 123 124 125 126
    eurybox_display_message debug CREATE "Tar command output:\n$TAR_OUT"
    SYNC_OUT=`sudo sync 2>&1`
    STATUS=$?
    if [[ !($STATUS -eq 0) ]]
    then
      eurybox_display_message warning CREATE "Archive description FS sync failed - error: $STATUS:\n$SYNC_OUT"
    else
      eurybox_display_message message CREATE "Archive description FS sync - OK"
      eurybox_display_message debug CREATE "Sync command output:\n$SYNC_OUT"
    fi
127 128 129 130 131 132
  fi
}

#FIXME: extract function parameters to transfer to control section use and make functions more generic and reusable
eurybox_create_archive_par ()
{
133 134 135
  local PAR_OUT
  local SYNC_OUT
  local STATUS
136
  case $EURYBOX_LOG_LEVEL in
137 138 139 140
    debug|verbose ) PAR_OPTIONS="-v -v";;
    message ) PAR_OPTIONS="-v";;
    warning ) PAR_OPTIONS="-q";;
    error ) PAR_OPTIONS="-q -q";;
141
  esac
142 143
  case "${EURYBOX_BACKUP_ARCHIVE[ECC]}" in
    "par2" )
144
      PAR_OUT=`sudo sh -c "par2 c $PAR_OPTIONS -r${EURYBOX_BACKUP_ARCHIVE[ECC_LEVEL]} -t+ ${EURYBOX_BACKUP_DESTINATION[MOUNT]}/${EURYBOX_BACKUP_ARCHIVE[NAME]}.${EURYBOX_BACKUP_ARCHIVE[ECC]} ${EURYBOX_BACKUP_DESTINATION[MOUNT]}/${EURYBOX_BACKUP_ARCHIVE[NAME]}.${EURYBOX_BACKUP_ARCHIVE[FORMAT]}" 2>&1`
145 146
      STATUS=$?
    ;;
147
    * )
148
      eurybox_display_message error CREATE "Archive ecc format unknown: ${EURYBOX_BACKUP_ARCHIVE[HASH]}"
149 150 151 152
    ;;
  esac
  if [[ !($STATUS -eq 0) ]]
  then
153
    eurybox_display_message warning ARCHIVE "Archive ecc creation failed - error $STATUS:\n$PAR_OUT"
154 155
  else
    eurybox_display_message message CREATE "Archive ecc creation - OK"
156 157 158 159 160 161 162 163 164 165
    eurybox_display_message debug CREATE "Ecc command output:\n$PAR_OUT"
    SYNC_OUT=`sudo sync 2>&1`
    STATUS=$?
    if [[ !($STATUS -eq 0) ]]
    then
      eurybox_display_message warning CREATE "Archive description FS sync failed - error $STATUS:\n$SYNC_OUT"
    else
      eurybox_display_message message CREATE "Archive description FS sync - OK"
      eurybox_display_message debug CREATE "Sync command output:\n$SYNC_OUT"
    fi
166 167 168 169 170 171
  fi
}

#FIXME: extract function parameters to transfer to control section use and make functions more generic and reusable
eurybox_create_archive_sha ()
{
172 173 174
  local SHA_OUT
  local SYNC_OUT
  local STATUS
175
  case $EURYBOX_LOG_LEVEL in
176 177 178 179
    debug|verbose ) SHA_OPTIONS="";;
    message ) SHA_OPTIONS="";;
    warning ) SHA_OPTIONS="";;
    error ) SHA_OPTIONS="";;
180
  esac
181 182
  case "${EURYBOX_BACKUP_ARCHIVE[HASH]}" in
    "sha256" )
183
      SHA_OUT=`sudo sh -c "sha256sum $SHA_OPTIONS ${EURYBOX_BACKUP_DESTINATION[MOUNT]}/${EURYBOX_BACKUP_ARCHIVE[NAME]}* > ${EURYBOX_BACKUP_DESTINATION[MOUNT]}/${EURYBOX_BACKUP_ARCHIVE[NAME]}.${EURYBOX_BACKUP_ARCHIVE[HASH]}" 2>&1`
184 185 186
      STATUS=$?
    ;;
    "sha512" )
187
      SHA_OUT=`sudo sh -c "sha512sum $SHA_OPTIONS ${EURYBOX_BACKUP_DESTINATION[MOUNT]}/${EURYBOX_BACKUP_ARCHIVE[NAME]}* > ${EURYBOX_BACKUP_DESTINATION[MOUNT]}/${EURYBOX_BACKUP_ARCHIVE[NAME]}.${EURYBOX_BACKUP_ARCHIVE[HASH]}" 2>&1`
188
      STATUS=$?
189 190 191 192
    ;;
    * )
      eurybox_display_message error CREATE "Archive hash format unknown: ${EURYBOX_BACKUP_ARCHIVE[HASH]}"
    ;;
193 194 195
  esac
  if [[ !($STATUS -eq 0) ]]
  then
196
    eurybox_display_message warning CREATE "Archive hash creation failed - error $STATUS:\n$SHA_OUT"
197 198
  else
    eurybox_display_message message CREATE "Archive hash creation - OK"
199 200 201 202 203 204 205 206 207 208
    eurybox_display_message debug CREATE "Hash command output:\n$SHA_OUT"
    SYNC_OUT=`sudo sync 2>&1`
    STATUS=$?
    if [[ !($STATUS -eq 0) ]]
    then
      eurybox_display_message warning CREATE "Archive description FS sync failed - error $STATUS:\n$SYNC_OUT"
    else
      eurybox_display_message message CREATE "Archive description FS sync - OK"
      eurybox_display_message debug CREATE "Sync command output:\n$SYNC_OUT"
    fi
209 210 211
  fi
}