Select Git revision
simple-backup.sh 2.92 KiB
#!/bin/bash
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$script_dir" || exit
set -o errexit
set -o nounset
set -o pipefail
##### READ CONFIGURATION #####
config_file="simple-backup.conf"
# Get nfs mount path
section="nfs"
key="path"
nfs_mount_path=$(awk -F= -v section="$section" -v key="$key" '
$0 ~ "\\[" section "\\]" { in_section=1; next }
$0 ~ /^\[.*\]/ { in_section=0 }
in_section && $1 ~ key { gsub(/^[ \t]+|[ \t]+$/, "", $2); print $2; exit }
' $config_file)
if test -z "$nfs_mount_path"; then
echo "No nfs volume path was provided"
exit 1
fi
# Get nfs server
section="nfs"
key="server"
nfs_server=$(awk -F= -v section="$section" -v key="$key" '
$0 ~ "\\[" section "\\]" { in_section=1; next }
$0 ~ /^\[.*\]/ { in_section=0 }
in_section && $1 ~ key { gsub(/^[ \t]+|[ \t]+$/, "", $2); print $2; exit }
' $config_file)
if test -z "$nfs_server"; then
echo "No nfs volume path was provided"
exit 1
fi
# Get nfs volume path
section="nfs"
key="volume"
nfs_volume_path=$(awk -F= -v section="$section" -v key="$key" '
$0 ~ "\\[" section "\\]" { in_section=1; next }
$0 ~ /^\[.*\]/ { in_section=0 }
in_section && $1 ~ key { gsub(/^[ \t]+|[ \t]+$/, "", $2); print $2; exit }
' $config_file)
if test -z "$nfs_volume_path"; then
echo "No nfs volume path was provided"
exit 1
fi
# Get source directories
section="files"
key="source"
source_directories=($(awk -F= -v section="$section" -v key="$key" '
$0 ~ "\\[" section "\\]" { in_section=1; next }
$0 ~ /^\[.*\]/ { in_section=0 }
in_section && $1 ~ key {
gsub(/^[ \t]+|[ \t]+$/, "", $2);
print $2
}
' $config_file))
if test -z "$source_directories"; then
echo "No source directory was provided"
exit 1
fi
# Get excluded directories
section="files"
key="exclude"
# Read the INI file and extract all values into an array
excludes=($(awk -F= -v section="$section" -v key="$key" '
$0 ~ "\\[" section "\\]" { in_section=1; next }
$0 ~ /^\[.*\]/ { in_section=0 }
in_section && $1 ~ key {
gsub(/^[ \t]+|[ \t]+$/, "", $2);
print $2
}
' $config_file))
##### MOUNTS NFS SHARE #####
if [ ! -d "$nfs_mount_path" ]; then
mkdir -p "$nfs_mount_path"
echo "Directory '$nfs_mount_path' created."
fi
mountpoint -q $nfs_mount_path || mount $nfs_server:$nfs_volume_path $nfs_mount_path
##### Backup #####
source_string=""
for source in "${source_directories[@]}"; do
source_string+="${source} "
done
exclude_string=""
for exclude in "${excludes[@]}"; do
exclude_string+="--exclude=${exclude} "
done
readonly BACKUP_DIR="${nfs_mount_path}/backups/${HOSTNAME}"
readonly DATETIME="$(date '+%Y-%m-%d_%H:%M:%S')"
readonly BACKUP_PATH="${BACKUP_DIR}/${DATETIME}"
readonly LATEST_LINK="${BACKUP_DIR}/latest"
mkdir -p "${BACKUP_DIR}"
rsync_cmd_str="rsync -avR --delete ${source_string} --link-dest ${LATEST_LINK} ${exclude_string} ${BACKUP_PATH}"
echo $rsync_cmd_str
$rsync_cmd_str
rm -rf "${LATEST_LINK}"
ln -s "${BACKUP_PATH}" "${LATEST_LINK}"