<commit_before>#!/bin/bash

#####################################
## CONFIG 
#####################################

# The type of encryption to use, options:
#  - symmetric (keyfile)
#  - asymmetric (public/private)
ENCRYPTION_MODE="symmetric"

# The key file path, defaults to a supplied parameter
ENCRYPTION_KEY="$2"

# A temporary directory to store the backup
BACKUP_TMP="/tmp/"

# Modes:
#  - attach: Attach the encrypted backup as a file
BACKUP_MODE="attach"

#####################################
## PROCESS
#####################################

# No configuration below here
NL=$'\n'
NOW=$(date +"%Y-%m-%d") 

# If the file .backupconfig.sh exists then we will include it
# You can override the configuration here!
if [[ -f ".backupconfig.sh" ]]; then
	source ".backupconfig.sh"
fi

function do_encrypt {
	if [[ "$ENCRYPTION_MODE" == "symmetric" ]]; then
		cat - | openssl enc -aes-256-cbc -kfile "$1" -z
	else
		cat - | gzip | openssl smime -encrypt -aes256 -binary -outform DEM "$1"
	fi
}

function do_backup {
	BACKUP_FILE="$BACKUP_TMP$1.gz.enc"
	BACKUP_EMAIL="$2"
	BACKUP_SUBJECT="$3 - $NOW"

	if [[ -f "$BACKUP_FILE" ]]; then
		rm "$BACKUP_FILE"
	fi

	do_encrypt "$4" > "$BACKUP_FILE"
	STATUS=$?

	if [[ $STATUS != "0" ]]; then
		echo "Backup Failed"
		echo "Backup Failed" | mutt -s "[FAIL] $BACKUP_SUBJECT" -- "$BACKUP_EMAIL"
	elif [[ "$1" == "output" ]] ; then
		echo "Backup File: $BACKUP_FILE"
	elif [[ "$5" == "attach" ]]; then
		echo "Backup Complete: $NOW" | mutt -a "$BACKUP_FILE" -s "[OK] $BACKUP_SUBJECT" -- "$BACKUP_EMAIL"
	else 
		FUNC="upload_$5"
		BACKUP_LINK=$(eval ${FUNC} "$BACKUP_FILE")
		echo "Backup Complete: ${NOW}${NL}Backup Link:${BACKUP_LINK}" | mutt -s "[OK] $BACKUP_SUBJECT" -- "$BACKUP_EMAIL"
	fi
}

function do_decrypt {
	if [[ "$ENCRYPTION_MODE" == "symmetric" ]]; then
		cat - | openssl enc -aes-256-cbc -d -kfile "$1" -z
	else
		cat - | openssl smime -decrypt -binary -inform DEM -inkey "$1" | gzip -d
	fi
}

function create_keypair {<commit_msg>include upload module if available<commit_after>#!/bin/bash

#####################################
## CONFIG 
#####################################

# The type of encryption to use, options:
#  - symmetric (keyfile)
#  - asymmetric (public/private)
ENCRYPTION_MODE="symmetric"

# The key file path, defaults to a supplied parameter
ENCRYPTION_KEY="$2"

# A temporary directory to store the backup
BACKUP_TMP="/tmp/"

# Modes:
#  - attach: Attach the encrypted backup as a file
BACKUP_MODE="attach"

#####################################
## PROCESS
#####################################

# No configuration below here
NL=$'\n'
NOW=$(date +"%Y-%m-%d") 

# If the file .backupconfig.sh exists then we will include it
# You can override the configuration here!
if [[ -f ".backupconfig.sh" ]]; then
	source ".backupconfig.sh"
fi

function do_encrypt {
	if [[ "$ENCRYPTION_MODE" == "symmetric" ]]; then
		cat - | openssl enc -aes-256-cbc -kfile "$1" -z
	else
		cat - | gzip | openssl smime -encrypt -aes256 -binary -outform DEM "$1"
	fi
}

function do_backup {
	BACKUP_FILE="$BACKUP_TMP$1.gz.enc"
	BACKUP_EMAIL="$2"
	BACKUP_SUBJECT="$3 - $NOW"

	if [[ -f "$BACKUP_FILE" ]]; then
		rm "$BACKUP_FILE"
	fi

	do_encrypt "$4" > "$BACKUP_FILE"
	STATUS=$?

	if [[ $STATUS != "0" ]]; then
		echo "Backup Failed"
		echo "Backup Failed" | mutt -s "[FAIL] $BACKUP_SUBJECT" -- "$BACKUP_EMAIL"
	elif [[ "$1" == "output" ]] ; then
		echo "Backup File: $BACKUP_FILE"
	elif [[ "$5" == "attach" ]]; then
		echo "Backup Complete: $NOW" | mutt -a "$BACKUP_FILE" -s "[OK] $BACKUP_SUBJECT" -- "$BACKUP_EMAIL"
	else 
		FUNC="upload_$5"
		if [[ -f "upload/$FUNC.sh" ]]; then
			source "upload/$FUNC.sh"
		fi
		BACKUP_LINK=$(eval ${FUNC} "$BACKUP_FILE")
		echo "Backup Complete: ${NOW}${NL}Backup Link:${BACKUP_LINK}" | mutt -s "[OK] $BACKUP_SUBJECT" -- "$BACKUP_EMAIL"
	fi
}

function do_decrypt {
	if [[ "$ENCRYPTION_MODE" == "symmetric" ]]; then
		cat - | openssl enc -aes-256-cbc -d -kfile "$1" -z
	else
		cat - | openssl smime -decrypt -binary -inform DEM -inkey "$1" | gzip -d
	fi
}

function create_keypair {
{'fraction_non_alphanumeric': 0.22756253065228052, 'fraction_numerical': 0.010789602746444336, 'mean_word_length': 4.176395939086294, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1611239', 'n_tokens_mistral': 1568, 'n_tokens_neox': 1463, 'n_words': 407}