Bryan 2020-11-01 18:15:16 -06:00
commit 3f0389f688
5 changed files with 158 additions and 23 deletions

79
config Normal file
View File

@ -0,0 +1,79 @@
######################################################################
## SET THE FOLLOWING TO "TRUE" WHEN YOU HAVE CONFIGURED THIS FILE ####
######################################################################
CONFIGURED="FALSE"
#############################
if [ "${CONFIGURED}" != "TRUE" ]; then
echo "Config file has not been set up."
exit 1
fi
# Beginning of configuration:
#############################
#BORG="/usr/bin/borg"
SYSTEM_NAME="ths"
BORG_REPO="bryan@172.16.1.83:/srv/borg/${SYSTEM_NAME}"
# If using a non-standard SSH port ssh:// have to be specified (per borg v1.0.9)
#BORG_REPO="ssh://<user>@<reposerver>:<ssh_port>/srv/borg/${SYSTEM_NAME}"
BORG_PASSPHRASE="uD6Db2MrhxdCoKmD4gpW"
PACKAGE_LIST_DIRECTORY="/srv/dumps/"
MYSQL_DUMP_DIRECTORY="/srv/dumps/"
MONGODB_DUMP_DIRECTORY="/srv/dumps/"
PATHS=(
"/etc"
"/home"
"/root"
"/srv"
"/usr/local"
"/var/spool/cron/crontabs"
"/mnt"
)
EXCLUDES=(
"sh:/home/**/.cache"
"/root/.cache"
"*.pyc"
)
KEEP_HOURLY=0
KEEP_DAILY=31
KEEP_WEEKLY=0
KEEP_MONTHLY=12
KEEP_YEARLY=5
# Limit bandwith used when backing up to SSH repositories. Requires the utility `pv`.
# The unit is bytes per second. 0 means no limit. The numeric part must be an integer value.
# You can use K, M, G or T binary prefixes (power of 1024), e.g., 5M = 5MiB/s = 5242880 bytes/s.
#BWLIMIT=0
# Location of borgwrapper lock files
#LOCKDIR="/run/lock/borgwrapper"
# You can add args, such as a custom identity key file, to ssh here
#BORG_RSH="ssh"
# Optional arguments to the different borg commands.
# The current default values are listed. Modify if needed.
#BORG_CREATE_ARGS=(
# --info
# --stats
# --list
# --filter AME
# --compression lz4
#)
#BORG_PRUNE_ARGS=(
# --info
# --stats
# --list
#)
#BORG_CHECK_ARGS=(
# --info
#)
#BORG_INIT_ARGS=(
# --encryption repokey-blake2
#)

View File

@ -1,3 +1,16 @@
######################################################################
## SET THE FOLLOWING TO "TRUE" WHEN YOU HAVE CONFIGURED THIS FILE ####
######################################################################
CONFIGURED="FALSE"
if [ "${CONFIGURED}" != "TRUE" ]; then
echo "Config file has not been set up."
exit 1
fi
# Beginning of configuration:
#############################
#BORG="/usr/bin/borg" #BORG="/usr/bin/borg"
SYSTEM_NAME="name-of-computer" SYSTEM_NAME="name-of-computer"

36
install Executable file
View File

@ -0,0 +1,36 @@
#! /bin/bash
cp -i ./src/borgwrapper /usr/local/bin/borgwrapper
chown root. /usr/local/bin/borgwrapper
chmod 750 /usr/local/bin/borgwrapper
if [ ! -d /etc/borgwrapper/ ]; then
mkdir /etc/borgwrapper
fi
if [ ! -e ./config ]; then
echo "Please rename \"config.example\" to \"config\""
echo "And make sure to update with your settings before running \"install\" again!"
exit 1
fi
cp -i ./config /etc/borgwrapper/config
chown root. /etc/borgwrapper/config
chmod 600 /etc/borgwrapper/config
cp ./systemd/*.{timer,service} /etc/systemd/system/
for config_file in /etc/borgwrapper/*
do
config_file=$(basename ${config_file})
systemctl enable borgwrapper-backup@"${config_file}".timer
systemctl enable borgwrapper-verify@"${config_file}".timer
systemctl start borgwrapper-backup@"${config_file}".timer
systemctl start borgwrapper-verify@"${config_file}".timer
done
echo
echo "Please edit \"/etc/borgwrapper/config\" to make changes to configuration."
echo "Read \"README.md\" for more information."
echo

View File

@ -1,8 +1,16 @@
#!/bin/bash #!/bin/bash
VERSION="1.5.2" VERSION="1.5.2"
SCRIPT_NAME=$(basename $0)
log () {
echo "$@" | logger -p user.notice -t ${SCRIPT_NAME}
}
err () {
echo "$@" | logger -p user.err -t ${SCRIPT_NAME}
}
print_usage () { print_usage () {
cat << EOF cat << EOF
Usage: borgwrapper [OPTIONS] MODE Usage: borgwrapper [OPTIONS] MODE
@ -27,13 +35,13 @@ error_handler () {
local SCRIPT_NAME="$0" local SCRIPT_NAME="$0"
local LINE="$1" local LINE="$1"
local EXIT_CODE="$2" local EXIT_CODE="$2"
>&2 echo "${SCRIPT_NAME}: Error in line ${LINE} (exit code ${EXIT_CODE})" err "${SCRIPT_NAME}: Error in line ${LINE} (exit code ${EXIT_CODE})"
exit ${EXIT_CODE} exit ${EXIT_CODE}
} }
save_package_list () { save_package_list () {
if ! which apt-mark; then if ! which apt-mark; then
return 2 return
fi fi
if [ ! -d "${PACKAGE_LIST_DIRECTORY}" ]; then if [ ! -d "${PACKAGE_LIST_DIRECTORY}" ]; then
@ -45,28 +53,30 @@ save_package_list () {
dump_sql () { dump_sql () {
if ! which mysqldump; then if ! which mysqldump; then
return 2 log "No mysqldump"
return
fi fi
if [ ! -d "${MYSQL_DUMP_DIRECTORY}" ] if [ ! -d "${MYSQL_DUMP_DIRECTORY}" ]
then mkdir -p "${MYSQL_DUMP_DIRECTORY}" then mkdir -p "${MYSQL_DUMP_DIRECTORY}"
fi fi
log "Dumping mysql databases"
for DB in $(mysql -u 'root' -e 'show databases' -s --skip-column-names); do for DB in $(mysql -u 'root' -e 'show databases' -s --skip-column-names); do
mysqldump --single-transaction -u 'root' "${DB}" > "${MYSQL_DUMP_DIRECTORY}${DB}.sql.bak"; mysqldump --single-transaction -u 'root' "${DB}" > "${MYSQL_DUMP_DIRECTORY}${DB}.sql.bak" || err "Error dumping mysql"
done done
} }
dump_mongo () { dump_mongo () {
if ! which mongodump; then if ! which mongodump; then
return 2 log "No mongodump"
return
fi fi
if [ ! -d "${MONGODB_DUMP_DIRECTORY}" ] if [ ! -d "${MONGODB_DUMP_DIRECTORY}" ]
then mkdir -p "${MONGODB_DUMP_DIRECTORY}/mongodump" then mkdir -p "${MONGODB_DUMP_DIRECTORY}/mongodump"
fi fi
log "Dumping mongo database"
mongodump --out "${MONGODB_DUMP_DIRECTORY}/mongodump" mongodump --out "${MONGODB_DUMP_DIRECTORY}/mongodump" || err "Error dumping mongo database"
} }
@ -77,6 +87,7 @@ borg_init () {
--encryption repokey-blake2 --encryption repokey-blake2
) )
fi fi
log "Initializing ${BORG_REPO}"
${BORG} init "${BORG_INIT_ARGS[@]}" "${BORG_REPO}" ${BORG} init "${BORG_INIT_ARGS[@]}" "${BORG_REPO}"
} }
@ -102,11 +113,14 @@ borg_backup () {
BORG_CREATE_ARGS+=( --stats ) BORG_CREATE_ARGS+=( --stats )
fi fi
log "Creating borg repo using ${BORG} create ${BORG_CREATE_ARGS[@]} ${BORG_REPO}::${SYSTEM_NAME}-$(date -u +'%Y-%m-%dT%H-%M-%SZ') ${PATHS[@]} ${EXCLUDE_CMD[@]}"
${BORG} create \ ${BORG} create \
"${BORG_CREATE_ARGS[@]}" \ "${BORG_CREATE_ARGS[@]}" \
"${BORG_REPO}"::"${SYSTEM_NAME}-$(date -u +'%Y-%m-%dT%H-%M-%SZ')" \ "${BORG_REPO}"::"${SYSTEM_NAME}-$(date -u +'%Y-%m-%dT%H-%M-%SZ')" \
"${PATHS[@]}" \ "${PATHS[@]}" \
"${EXCLUDE_CMD[@]}" "${EXCLUDE_CMD[@]}" \
|| log "Error backing up. Has repo been initialized?"
} }
borg_prune () { borg_prune () {
@ -122,7 +136,7 @@ borg_prune () {
else else
BORG_PRUNE_ARGS+=( --stats ) BORG_PRUNE_ARGS+=( --stats )
fi fi
log "Running borg prune"
${BORG} prune \ ${BORG} prune \
"${BORG_PRUNE_ARGS[@]}" \ "${BORG_PRUNE_ARGS[@]}" \
--prefix "${SYSTEM_NAME}-" \ --prefix "${SYSTEM_NAME}-" \
@ -141,6 +155,7 @@ borg_verify () {
) )
fi fi
log "Running borg check"
${BORG} check "${BORG_CHECK_ARGS[@]}" "${BORG_REPO}" ${BORG} check "${BORG_CHECK_ARGS[@]}" "${BORG_REPO}"
} }
@ -277,11 +292,16 @@ mkdir -p "${LOCKDIR}"
elif [[ ${MODE} == "backup" ]]; then elif [[ ${MODE} == "backup" ]]; then
trap 'exit_backup $?' ERR INT TERM trap 'exit_backup $?' ERR INT TERM
save_package_list save_package_list
echo "saving package list"
dump_mongo dump_mongo
echo "dumping mongo"
dump_sql dump_sql
echo "dumping sql"
borg_backup borg_backup
echo "running borg backup"
borg_prune borg_prune
echo "running borg prune"
exit_backup 0 exit_backup 0
elif [[ ${MODE} == "verify" ]]; then elif [[ ${MODE} == "verify" ]]; then
trap 'exit_verify $?' ERR INT TERM trap 'exit_verify $?' ERR INT TERM

View File

@ -1,13 +0,0 @@
#! /bin/bash
cp -i ./borgwrapper /usr/local/bin/borgwrapper
chown root. /usr/local/bin/borgwrapper
chmod 750 /usr/local/bin/borgwrapper
if [! -d /etc/borgwrapper/ ]; then
mkdir /etc/borgwrapper/config
fi
cp -i config /etc/borgwrapper/config
chown root. /etc/borgwrapper/config
chmod 600 /etc/borgwrapper/config