Skip to content

Instantly share code, notes, and snippets.

@acpmasquerade
Last active January 10, 2019 14:00
Show Gist options
  • Save acpmasquerade/abd0cc5c443511898f99bac1afb3b390 to your computer and use it in GitHub Desktop.
Save acpmasquerade/abd0cc5c443511898f99bac1afb3b390 to your computer and use it in GitHub Desktop.
Postgres WAL Archive Script (archive_command) checking File Integrity based upon the checksum of source and target
#!/bin/bash
# -----------------
# DISCLAIMER !!!
# To be used as is.
# --------------------
# Syntax = wal_archive.sh wall_source_fullpath wall_name
# archive_command='wal_archive.sh "%p" "%f"'
# PS: The order is %p followed by %f
wal_archives_dir='/var/lib/postgresql/wal.archives/main_9.5'
# wal_archives_dir='/mnt/wal.archives'
wal_source="${1}" # %p is the full path
wal_filename="${2}" # %f is the filename
echo "Params !!"
echo " -- Source WAL ${wal_source}"
echo " -- WAL Filename ${wal_filename}"
target_file="${wal_archives_dir}/${wal_filename}"
echo "Archives !!"
echo " -- Archive Location ${wal_archives_dir}"
echo " -- Archive Fullpath ${target_file}"
# Customize the checksum file as per the OS
function checksum()
{
chk_file="${1}"
# mac
md5 "${chk_file}" | awk '{print $4}'
# ubuntu
# md5sum "${chk_file}" | awk '{print $1}'
}
echo "Checksum !!"
wal_checksum=$(checksum "${wal_source}")
echo " -- WAL Hash ${wal_checksum}"
if [ -f "${target_file}" ];
then
# Checksum
target_checksum=$(checksum "${target_file}")
if [[ $target_checksum == $wal_checksum ]];
then
echo " -- File was already copied correctly !! Ignored"
exit 0
fi
fi
echo " -- Beginning to copy file ... "
cp "${wal_source}" "${target_file}"
target_checksum=$(checksum "${target_file}")
if [[ $target_checksum == $wal_checksum ]];
then
echo " -- File is copied correctly"
exit 0
fi
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment