Skip to content

Instantly share code, notes, and snippets.

@xyzulu
Last active March 16, 2025 02:47
Show Gist options
  • Save xyzulu/dad9df69207484f6e4c0a3653374d541 to your computer and use it in GitHub Desktop.
Save xyzulu/dad9df69207484f6e4c0a3653374d541 to your computer and use it in GitHub Desktop.
Backup cPFence settings to a remote location and then delete the backup file
#!/bin/bash
set -o errexit
set -o nounset
set -o pipefail
# Configuration
REMOTE_USER="your_user"
REMOTE_HOST="your_remote_host"
REMOTE_DIR="/path/to/remote/destination"
REMOTE_PORT="22"
SSH_KEY_FILE="/path/to/private_key"
LOCAL_FILE_PATTERN="/tmp/cpfence_backup_*.tar.gz"
# Ensure required commands exist
check_requirements() {
if ! command -v scp >/dev/null || ! command -v ssh >/dev/null || ! command -v ls >/dev/null || ! command -v rm >/dev/null || ! command -v cpfence >/dev/null; then
printf "Error: Required commands (scp, ssh, ls, rm, cpfence) not found.\n" >&2
return 1
fi
if [[ ! -f "$SSH_KEY_FILE" ]]; then
printf "Error: SSH key file '%s' not found.\n" "$SSH_KEY_FILE" >&2
return 1
fi
}
# Run cpfence backup command
run_backup() {
if ! cpfence --backup-cpf-settings; then
printf "Error: Failed to execute cpfence backup.\n" >&2
return 1
fi
printf "CPFence backup completed successfully.\n"
}
# Get the latest file matching the pattern
get_latest_file() {
local latest_file
latest_file=$(ls -t $LOCAL_FILE_PATTERN 2>/dev/null | head -n 1 || true)
if [[ -z "$latest_file" ]]; then
printf "Error: No matching files found for pattern '%s'.\n" "$LOCAL_FILE_PATTERN" >&2
return 1
fi
printf "%s" "$latest_file"
}
# Get file size in bytes
get_file_size() {
local file="$1"
local size
if ! size=$(du -b "$file" | cut -f1); then
printf "Error: Unable to determine file size for '%s'.\n" "$file" >&2
return 1
fi
printf "%s" "$size"
}
# Transfer file via SCP and rename it on the remote host
transfer_file() {
local file="$1"
local size
local local_hostname
local remote_temp_path
local remote_final_path
if [[ ! -f "$file" ]]; then
printf "Error: File '%s' does not exist.\n" "$file" >&2
return 1
fi
local_hostname=$(hostname -s)
size=$(get_file_size "$file")
remote_temp_path="${REMOTE_DIR}/$(basename "$file")"
remote_final_path="${REMOTE_DIR}/${local_hostname}_$(basename "$file")"
if ! scp -P "$REMOTE_PORT" -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i "$SSH_KEY_FILE" "$file" \
"${REMOTE_USER}@${REMOTE_HOST}:${remote_temp_path}"; then
printf "Error: Failed to transfer file '%s' to remote host.\n" "$file" >&2
return 1
fi
if ! ssh -p "$REMOTE_PORT" -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i "$SSH_KEY_FILE" \
"${REMOTE_USER}@${REMOTE_HOST}" "mv '${remote_temp_path}' '${remote_final_path}'"; then
printf "Error: Failed to rename file on remote host.\n" >&2
return 1
fi
printf "File '%s' successfully transferred to %s@%s:%s\n" "$file" "$REMOTE_USER" "$REMOTE_HOST" "$remote_final_path"
printf "Transferred data size: %s bytes\n" "$size"
}
# Delete all matching files after transfer
cleanup_files() {
if rm -f $LOCAL_FILE_PATTERN 2>/dev/null; then
printf "Deleted all files matching pattern: %s\n" "$LOCAL_FILE_PATTERN"
else
printf "Warning: No files deleted or error occurred while deleting files.\n" >&2
fi
}
main() {
check_requirements
run_backup
local latest_file
latest_file=$(get_latest_file)
transfer_file "$latest_file"
cleanup_files
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment