Skip to content

Commit

Permalink
Merge pull request #54 from cytopia/ci-incremental-backup-checks
Browse files Browse the repository at this point in the history
Validate incremental backups during CI
  • Loading branch information
cytopia authored Apr 3, 2020
2 parents fbdb7dc + 588cf2c commit 7d43337
Show file tree
Hide file tree
Showing 6 changed files with 351 additions and 12 deletions.
73 changes: 73 additions & 0 deletions tests/.lib/dir-size.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env bash

set -e
set -u
set -o pipefail


# -------------------------------------------------------------------------------------------------
# PUBLIC FUNCTIONS
# -------------------------------------------------------------------------------------------------

check_dir_size() {
local src="${1}"
local dst="${2}"

src_size="$( get_dir_size_with_hardlinks "${src}" )"
dst_size="$( get_dir_size_with_hardlinks "${dst}" )"

if [ "${src_size}" -eq "${dst_size}" ]; then
printf "[TEST] [OK] src-dir(%s) and dst-dir(%s) size match\\r\\n" "${src_size}" "${dst_size}"
return 0
fi
printf "[TEST] [FAIL] src-dir(%s) and dst-dir(%s) size don't match: (src: %s) (dst: %s)\\r\\n" "${src_size}" "${dst_size}" "${src}" "${dst}"
exit 1
}


# -------------------------------------------------------------------------------------------------
# PRIVATE FUNCTIONS
# -------------------------------------------------------------------------------------------------

###
### Return total size of directory in bytes.
### It also counts the size of hardlinks.
###
### @param abs_path directory
###
get_dir_size_with_hardlinks() {
local dir="${1}"
local size=


size="$( run "cd '${dir}' && du -d0 '.' | awk '{print \$1}'" "1" "stderr" )"
echo "${size}"
}

###
### Return total size of directory in bytes.
### Subtract the size of any hardlinks.
###
### @param abs_path directory
###
get_dir_size_without_hardlinks() {
local dir="${1}"
local suffix="${2:-}"
local actual_path=
local current_dir_name=
local parent_dir_path=
local size=

# Return the actual path (in case we're in a symlink)
actual_path="$( run "cd '${dir}' && pwd -P" "1" "stderr" )"

# Get only the name of the current directory
current_dir_name="$( run "basename '${actual_path}'" "1" "stderr" )"

# Get the parent directory path
parent_dir_path="$( run "dirname '${actual_path}'" "1" "stderr" )"


size="$( run "cd '${parent_dir_path}' && du -d2 2>/dev/null | grep -E '${current_dir_name}${suffix}\$' | head -1 | awk '{print \$1}'" "1" "stderr" )"
echo "${size}"
}
2 changes: 2 additions & 0 deletions tests/.lib/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ SCRIPT_PATH="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
# shellcheck disable=SC1090
. "${SCRIPT_PATH}/.lib/run-backup.sh"
# shellcheck disable=SC1090
. "${SCRIPT_PATH}/.lib/dir-size.sh"
# shellcheck disable=SC1090
. "${SCRIPT_PATH}/.lib/file-exist.sh"
# shellcheck disable=SC1090
. "${SCRIPT_PATH}/.lib/file-permissions.sh"
Expand Down
72 changes: 69 additions & 3 deletions tests/01-run-local-default-abs-noslash-noslash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,50 @@ check_link() {
check_src_dst_file_equal "${link}" "${SRC_DIR}" "${destination}"
}

check_dir() {
local src="${1}"
local dst="${2}"
local destination=
destination="${dst}/current/$(basename "${src}")"

check_dir_size "${src}" "${destination}"
}

check_backup() {
local src="${1}"
local dst="${2}"
local backup1="${3}"
local backup2="${4}"

local src_actual_size
local dst_actual_size
local backup1_actual_size
local backup2_actual_size

print_subline "Check incremental Backup"

backup1_actual_size="$( get_dir_size_without_hardlinks "${backup1}" "/$(basename "${src}")" )"
backup2_actual_size="$( get_dir_size_without_hardlinks "${backup2}" "/$(basename "${src}")" )"
if [ "${backup1_actual_size}" -eq "${backup2_actual_size}" ]; then
printf "[TEST] [FAIL] Incremental: inital backup (%s) and incremental backup (%s) disk sizes are equal\\r\\n" "${backup1_actual_size}" "${backup2_actual_size}"
exit 1
fi
printf "[TEST] [OK] Incremental: inital backup (%s) and incremental backup (%s) disk sizes differ\\r\\n" "${backup1_actual_size}" "${backup2_actual_size}"


print_subline "Check incremental Backup after deleting initial full backup"

run "rm -rf '${backup1}'"
src_actual_size="$( get_dir_size_with_hardlinks "${src}" )"
dst_actual_size="$( get_dir_size_without_hardlinks "${dst}/current/" "/$(basename "${src}")" )"

if [ "${src_actual_size}" -ne "${dst_actual_size}" ]; then
printf "[TEST] [FAIL] Incremental Backup: src-dir(%s) size is not equal to dst-dir(%s)\\r\\n" "${src_actual_size}" "${dst_actual_size}"
exit 1
fi
printf "[TEST] [OK] Incremental Backup: src-dir(%s) size is equal to dst-dir(%s)\\r\\n" "${src_actual_size}" "${dst_actual_size}"
}


### ################################################################################################
### ################################################################################################
Expand All @@ -122,9 +166,6 @@ run_backup \
"${RSYNC_ARGS}" \
"full"

# TODO: check for .inprogress
# TODO: add --append-verify (and check for rsync version >= 3)

check_file "${FILE1_NAME}" "${FILE1_PERM}"
check_file "${FILE2_NAME}" "${FILE2_PERM}"
check_file "${FILE3_NAME}" "${FILE3_PERM}"
Expand All @@ -133,6 +174,10 @@ check_link "${LINK1_NAME}"
check_link "${LINK2_NAME}"
check_link "${LINK3_NAME}"

check_dir "${SRC_DIR}" "${DST_DIR}"

BACKUP_PATH_1="$( cd "${DST_DIR}/current/" && pwd -P )"


### ################################################################################################
### ################################################################################################
Expand All @@ -159,3 +204,24 @@ check_file "${FILE3_NAME}" "${FILE3_PERM}"
check_link "${LINK1_NAME}"
check_link "${LINK2_NAME}"
check_link "${LINK3_NAME}"

check_dir "${SRC_DIR}" "${DST_DIR}"

BACKUP_PATH_2="$( cd "${DST_DIR}/current/" && pwd -P )"


### ################################################################################################
### ################################################################################################
###
### Validate Backups
###
### ################################################################################################
### ################################################################################################

print_headline "Validate Backups"

check_backup \
"${SRC_DIR}" \
"${DST_DIR}" \
"${BACKUP_PATH_1}" \
"${BACKUP_PATH_2}"
72 changes: 69 additions & 3 deletions tests/01-run-local-default-abs-noslash-slash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,50 @@ check_link() {
check_src_dst_file_equal "${link}" "${SRC_DIR}" "${destination}"
}

check_dir() {
local src="${1}"
local dst="${2}"
local destination=
destination="${dst}/current/$(basename "${src}")"

check_dir_size "${src}" "${destination}"
}

check_backup() {
local src="${1}"
local dst="${2}"
local backup1="${3}"
local backup2="${4}"

local src_actual_size
local dst_actual_size
local backup1_actual_size
local backup2_actual_size

print_subline "Check incremental Backup"

backup1_actual_size="$( get_dir_size_without_hardlinks "${backup1}" "/$(basename "${src}")" )"
backup2_actual_size="$( get_dir_size_without_hardlinks "${backup2}" "/$(basename "${src}")" )"
if [ "${backup1_actual_size}" -eq "${backup2_actual_size}" ]; then
printf "[TEST] [FAIL] Incremental: inital backup (%s) and incremental backup (%s) disk sizes are equal\\r\\n" "${backup1_actual_size}" "${backup2_actual_size}"
exit 1
fi
printf "[TEST] [OK] Incremental: inital backup (%s) and incremental backup (%s) disk sizes differ\\r\\n" "${backup1_actual_size}" "${backup2_actual_size}"


print_subline "Check incremental Backup after deleting initial full backup"

run "rm -rf '${backup1}'"
src_actual_size="$( get_dir_size_with_hardlinks "${src}" )"
dst_actual_size="$( get_dir_size_without_hardlinks "${dst}/current/" "/$(basename "${src}")" )"

if [ "${src_actual_size}" -ne "${dst_actual_size}" ]; then
printf "[TEST] [FAIL] Incremental Backup: src-dir(%s) size is not equal to dst-dir(%s)\\r\\n" "${src_actual_size}" "${dst_actual_size}"
exit 1
fi
printf "[TEST] [OK] Incremental Backup: src-dir(%s) size is equal to dst-dir(%s)\\r\\n" "${src_actual_size}" "${dst_actual_size}"
}


### ################################################################################################
### ################################################################################################
Expand All @@ -122,9 +166,6 @@ run_backup \
"${RSYNC_ARGS}" \
"full"

# TODO: check for .inprogress
# TODO: add --append-verify (and check for rsync version >= 3)

check_file "${FILE1_NAME}" "${FILE1_PERM}"
check_file "${FILE2_NAME}" "${FILE2_PERM}"
check_file "${FILE3_NAME}" "${FILE3_PERM}"
Expand All @@ -133,6 +174,10 @@ check_link "${LINK1_NAME}"
check_link "${LINK2_NAME}"
check_link "${LINK3_NAME}"

check_dir "${SRC_DIR}" "${DST_DIR}"

BACKUP_PATH_1="$( cd "${DST_DIR}/current/" && pwd -P )"


### ################################################################################################
### ################################################################################################
Expand All @@ -159,3 +204,24 @@ check_file "${FILE3_NAME}" "${FILE3_PERM}"
check_link "${LINK1_NAME}"
check_link "${LINK2_NAME}"
check_link "${LINK3_NAME}"

check_dir "${SRC_DIR}" "${DST_DIR}"

BACKUP_PATH_2="$( cd "${DST_DIR}/current/" && pwd -P )"


### ################################################################################################
### ################################################################################################
###
### Validate Backups
###
### ################################################################################################
### ################################################################################################

print_headline "Validate Backups"

check_backup \
"${SRC_DIR}" \
"${DST_DIR}" \
"${BACKUP_PATH_1}" \
"${BACKUP_PATH_2}"
72 changes: 69 additions & 3 deletions tests/01-run-local-default-abs-slash-noslash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,50 @@ check_link() {
check_src_dst_file_equal "${link}" "${SRC_DIR}" "${destination}"
}

check_dir() {
local src="${1}"
local dst="${2}"
local destination=
destination="${dst}/current"

check_dir_size "${src}" "${destination}"
}

check_backup() {
local src="${1}"
local dst="${2}"
local backup1="${3}"
local backup2="${4}"

local src_actual_size
local dst_actual_size
local backup1_actual_size
local backup2_actual_size

print_subline "Check incremental Backup"

backup1_actual_size="$( get_dir_size_without_hardlinks "${backup1}" )"
backup2_actual_size="$( get_dir_size_without_hardlinks "${backup2}" )"
if [ "${backup1_actual_size}" -eq "${backup2_actual_size}" ]; then
printf "[TEST] [FAIL] Incremental: inital backup (%s) and incremental backup (%s) disk sizes are equal\\r\\n" "${backup1_actual_size}" "${backup2_actual_size}"
exit 1
fi
printf "[TEST] [OK] Incremental: inital backup (%s) and incremental backup (%s) disk sizes differ\\r\\n" "${backup1_actual_size}" "${backup2_actual_size}"


print_subline "Check incremental Backup after deleting initial full backup"

run "rm -rf '${backup1}'"
src_actual_size="$( get_dir_size_with_hardlinks "${src}" )"
dst_actual_size="$( get_dir_size_without_hardlinks "${dst}/current/" )"

if [ "${src_actual_size}" -ne "${dst_actual_size}" ]; then
printf "[TEST] [FAIL] Incremental Backup: src-dir(%s) size is not equal to dst-dir(%s)\\r\\n" "${src_actual_size}" "${dst_actual_size}"
exit 1
fi
printf "[TEST] [OK] Incremental Backup: src-dir(%s) size is equal to dst-dir(%s)\\r\\n" "${src_actual_size}" "${dst_actual_size}"
}


### ################################################################################################
### ################################################################################################
Expand All @@ -122,9 +166,6 @@ run_backup \
"${RSYNC_ARGS}" \
"full"

# TODO: check for .inprogress
# TODO: add --append-verify (and check for rsync version >= 3)

check_file "${FILE1_NAME}" "${FILE1_PERM}"
check_file "${FILE2_NAME}" "${FILE2_PERM}"
check_file "${FILE3_NAME}" "${FILE3_PERM}"
Expand All @@ -133,6 +174,10 @@ check_link "${LINK1_NAME}"
check_link "${LINK2_NAME}"
check_link "${LINK3_NAME}"

check_dir "${SRC_DIR}" "${DST_DIR}"

BACKUP_PATH_1="$( cd "${DST_DIR}/current/" && pwd -P )"


### ################################################################################################
### ################################################################################################
Expand All @@ -159,3 +204,24 @@ check_file "${FILE3_NAME}" "${FILE3_PERM}"
check_link "${LINK1_NAME}"
check_link "${LINK2_NAME}"
check_link "${LINK3_NAME}"

check_dir "${SRC_DIR}" "${DST_DIR}"

BACKUP_PATH_2="$( cd "${DST_DIR}/current/" && pwd -P )"


### ################################################################################################
### ################################################################################################
###
### Validate Backups
###
### ################################################################################################
### ################################################################################################

print_headline "Validate Backups"

check_backup \
"${SRC_DIR}" \
"${DST_DIR}" \
"${BACKUP_PATH_1}" \
"${BACKUP_PATH_2}"
Loading

0 comments on commit 7d43337

Please sign in to comment.