eurybox.functions.create 8.3 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
  case ${EURYBOX_BACKUP_ARCHIVE[FORMAT]} in
12 13
    "tar" ) TAR_OPTIONS="-Stv";;
    "tar.gz" ) TAR_OPTIONS="-Stv --use-compress-program=pigz";;
14
    "tar.bz2" ) TAR_OPTIONS="-Stv --use-compress-program=pbzip2";;
15 16
  esac
  STATUS=1234
17
  DESTINATION=${EURYBOX_BACKUP_DESTINATION[MOUNT]}/${EURYBOX_BACKUP_ARCHIVE[NAME]}.desc
18
  DESCRIPTION="ARCHIVE_CORPORATE:${EURYBOX_CORPORATE}\nARCHIVE_FORMAT:${EURYBOX_BACKUP_ARCHIVE[FORMAT]}\nARCHIVE_TARGETS:${EURYBOX_BACKUP_TARGETS[*]}\nARCHIVE_TYPE:${EURYBOX_BACKUP_TYPE}\nARCHIVE_VERSION:${EURYBOX_VERSION}\nFEC_FORMAT:${EURYBOX_BACKUP_ARCHIVE[FEC]}\nHASH_FORMAT:${EURYBOX_BACKUP_ARCHIVE[HASH]}\nENCRYPTED_ARCHIVE:${EURYBOX_BACKUP_ARCHIVE[ENCRYPT]}\n"
19 20
  if [[ ${EURYBOX_BACKUP_ARCHIVE[ENCRYPT]} = "true" ]]
  then
21
    DESCRIPTION+="ENCRYPTION_ALGORITHM:${EURYBOX_BACKUP_ARCHIVE[ENC_ALGO]}\nARCHIVE_CONTENT:\n"
22
    #FIXME: function to read archive with variable archive format required here
23
    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]} -pass file:${EURYBOX_BACKUP_ARCHIVE[PASSFILE]} | tar ${TAR_OPTIONS}" 2>&1`
24 25
    STATUS=$?
  else
26
    DESCRIPTION+="ARCHIVE_CONTENT:\n"
27
    #FIXME: function to read archive with variable archive format required here
28
    DESCRIPTION+=`sudo sh -c "tar ${TAR_OPTIONS} -f ${EURYBOX_BACKUP_DESTINATION[MOUNT]}/${EURYBOX_BACKUP_ARCHIVE[NAME]}.${EURYBOX_BACKUP_ARCHIVE[FORMAT]}" 2>&1`
29 30
    STATUS=$?
  fi
31 32
  if [[ !($STATUS -eq 0) ]]
  then
33
    eurybox_display_message warning CREATE "Archive description creation failed - error $STATUS:\n$DESCRIPTION"
34
  else
35
    DESC_OUT=`sudo sh -c "printf %b \"$DESCRIPTION\" > $DESTINATION" 2>&1`
36 37 38
    STATUS=$?
    if [[ !($STATUS -eq 0) ]]
    then
39
        eurybox_display_message warning CREATE "Archive description write failed - error $STATUS:\n$DESC_OUT"
40
    else
41 42 43 44 45 46 47 48 49 50 51 52
      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
53
    fi
54 55 56 57 58 59
  fi
}

#FIXME: extract function parameters to transfer to control section use and make functions more generic and reusable
eurybox_create_archive_tar ()
{
60 61 62 63
  local TAR_OUT
  local SYNC_OUT
  local STATUS
  if [[ $EURYBOX_LOG_LEVEL = "debug" || $EURYBOX_LOG_LEVEL = "verbose" ]]
64 65 66 67 68 69
  then
    TAR_OPTIONS="-v"
  else
    TAR_OPTIONS="-"
  fi
  case ${EURYBOX_BACKUP_ARCHIVE[FORMAT]} in
70 71 72
    "tar" ) TAR_OPTIONS="${TAR_OPTIONS}Sch";;
    "tar.gz" ) TAR_OPTIONS="${TAR_OPTIONS}Sch --use-compress-program=pigz";;
    "tar.bz2" ) TAR_OPTIONS="${TAR_OPTIONS}Sch --use-compress-program=pbzip2";;
73
  esac
74 75 76 77
  if [[ ${EURYBOX_BACKUP_ARCHIVE[ENCRYPT]} = "true" ]]
  then
    TAR_OUT=$((
    (
78
      sudo sh -c "tar ${TAR_OPTIONS} ${EURYBOX_BACKUP_TARGETS[*]} | openssl enc -salt -e -${EURYBOX_BACKUP_ARCHIVE[ENC_ALGO]} -pass file:${EURYBOX_BACKUP_ARCHIVE[PASSFILE]} -out ${EURYBOX_BACKUP_DESTINATION[MOUNT]}/${EURYBOX_BACKUP_ARCHIVE[NAME]}.${EURYBOX_BACKUP_ARCHIVE[FORMAT]}"
79
#          STATUS=$(( ${PIPESTATUS[0]} + ${PIPESTATUS[1]} ))
80 81 82
    )) 2>&1)
    STATUS=$?
  else
83
    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`
84 85
    STATUS=$?
  fi
86 87
  if [[ !($STATUS -eq 0) ]]
  then
88
    eurybox_display_message warning CREATE "Archive creation failed - error: $STATUS:\n$TAR_OUT"
89 90
  else
    eurybox_display_message message CREATE "Archive creation - OK"
91 92 93 94 95
    eurybox_display_message debug CREATE "Tar command output:\n$TAR_OUT"
    SYNC_OUT=`sudo sync 2>&1`
    STATUS=$?
    if [[ !($STATUS -eq 0) ]]
    then
96
      eurybox_display_message warning CREATE "Archive creation FS sync failed - error: $STATUS:\n$SYNC_OUT"
97
    else
98
      eurybox_display_message message CREATE "Archive creation FS sync - OK"
99 100
      eurybox_display_message debug CREATE "Sync command output:\n$SYNC_OUT"
    fi
101 102 103
  fi
}

104
#FIXME: par2 has issues with curlftpfs - flush failed (ftpfs: operation ftpfs_write failed because Input/output error)
105
#FIXME: extract function parameters to transfer to control section use and make functions more generic and reusable
106
eurybox_create_archive_fec ()
107
{
108
  local FEC_OUT
109 110
  local SYNC_OUT
  local STATUS
111 112 113 114 115 116
  local FEC_LEVEL="${EURYBOX_BACKUP_ARCHIVE[FEC_LEVEL]}"
  local FEC_TYPE="${EURYBOX_BACKUP_ARCHIVE[FEC]}"
  local FEC_FILE_NUM="${EURYBOX_BACKUP_ARCHIVE[FEC_FILE_NUM]}"
  local FEC_FILE_NUM_MIN="${EURYBOX_BACKUP_ARCHIVE[FEC_FILE_NUM_MIN]}"
  local FEC_TARGET="${EURYBOX_BACKUP_DESTINATION[MOUNT]}/${EURYBOX_BACKUP_ARCHIVE[NAME]}.${EURYBOX_BACKUP_ARCHIVE[FORMAT]}"
  local FEC_DESTINATION="${EURYBOX_BACKUP_DESTINATION[MOUNT]}/${EURYBOX_BACKUP_ARCHIVE[NAME]}.${EURYBOX_BACKUP_ARCHIVE[FEC]}"
117
  case $EURYBOX_LOG_LEVEL in
118 119 120 121
    debug|verbose ) FEC_OPTIONS="-v";;
    message ) FEC_OPTIONS="";;
    warning ) FEC_OPTIONS="";;
    error ) FEC_OPTIONS="-q";;
122
  esac
123 124 125 126 127 128 129
  case "$FEC_TYPE" in
    par2 )
      FEC_OUT=`sudo sh -c "par2 c $FEC_OPTIONS -r${FEC_LEVEL} -n${FEC_FILE_NUM} -u -t+ ${FEC_DESTINATION} ${FEC_TARGET}" 2>&1`
      STATUS=$?
    ;;
    zfec )
      FEC_OUT=`sudo sh -c "zfec $FEC_OPTIONS -m ${FEC_FILE_NUM} -k ${FEC_FILE_NUM_MIN} ${FEC_TARGET}" 2>&1`
130 131
      STATUS=$?
    ;;
132
    * )
133
      eurybox_display_message error CREATE "Archive FEC format unknown: ${EURYBOX_BACKUP_ARCHIVE[FEC]}"
134 135 136 137
    ;;
  esac
  if [[ !($STATUS -eq 0) ]]
  then
138
    eurybox_display_message warning ARCHIVE "Archive FEC creation failed - error $STATUS:\n$FEC_OUT"
139
  else
140 141
    eurybox_display_message message CREATE "Archive FEC creation - OK"
    eurybox_display_message debug CREATE "FEC command output:\n$FEC_OUT"
142 143 144 145
    SYNC_OUT=`sudo sync 2>&1`
    STATUS=$?
    if [[ !($STATUS -eq 0) ]]
    then
146
      eurybox_display_message warning CREATE "Archive FEC FS sync failed - error $STATUS:\n$SYNC_OUT"
147
    else
148
      eurybox_display_message message CREATE "Archive FEC FS sync - OK"
149 150
      eurybox_display_message debug CREATE "Sync command output:\n$SYNC_OUT"
    fi
151 152 153
  fi
}

154

155
#FIXME: extract function parameters to transfer to control section use and make functions more generic and reusable
156
eurybox_create_archive_hash ()
157
{
158
  local HASH_OUT
159 160
  local SYNC_OUT
  local STATUS
161
  case $EURYBOX_LOG_LEVEL in
162 163 164 165
    debug|verbose ) SHA_OPTIONS="";;
    message ) SHA_OPTIONS="";;
    warning ) SHA_OPTIONS="";;
    error ) SHA_OPTIONS="";;
166
  esac
167 168
  case "${EURYBOX_BACKUP_ARCHIVE[HASH]}" in
    "sha256" )
169
      HASH_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`
170 171 172
      STATUS=$?
    ;;
    "sha512" )
173
      HASH_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`
174
      STATUS=$?
175 176 177 178
    ;;
    * )
      eurybox_display_message error CREATE "Archive hash format unknown: ${EURYBOX_BACKUP_ARCHIVE[HASH]}"
    ;;
179 180 181
  esac
  if [[ !($STATUS -eq 0) ]]
  then
182
    eurybox_display_message warning CREATE "Archive hash creation failed - error $STATUS:\n$HASH_OUT"
183 184
  else
    eurybox_display_message message CREATE "Archive hash creation - OK"
185
    eurybox_display_message debug CREATE "Hash command output:\n$HASH_OUT"
186 187 188 189
    SYNC_OUT=`sudo sync 2>&1`
    STATUS=$?
    if [[ !($STATUS -eq 0) ]]
    then
190
      eurybox_display_message warning CREATE "Archive hash FS sync failed - error $STATUS:\n$SYNC_OUT"
191
    else
192
      eurybox_display_message message CREATE "Archive hash FS sync - OK"
193 194
      eurybox_display_message debug CREATE "Sync command output:\n$SYNC_OUT"
    fi
195 196 197
  fi
}