diff --git a/config b/config new file mode 100644 index 0000000..e0135f1 --- /dev/null +++ b/config @@ -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://@:/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 +#) diff --git a/src/config.example b/config.example similarity index 78% rename from src/config.example rename to config.example index f340e27..751b08a 100644 --- a/src/config.example +++ b/config.example @@ -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" SYSTEM_NAME="name-of-computer" diff --git a/install b/install new file mode 100755 index 0000000..addb987 --- /dev/null +++ b/install @@ -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 diff --git a/src/borgwrapper b/src/borgwrapper index 37e784e..26421ca 100755 --- a/src/borgwrapper +++ b/src/borgwrapper @@ -1,8 +1,16 @@ #!/bin/bash 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 () { cat << EOF Usage: borgwrapper [OPTIONS] MODE @@ -27,13 +35,13 @@ error_handler () { local SCRIPT_NAME="$0" local LINE="$1" 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} } save_package_list () { if ! which apt-mark; then - return 2 + return fi if [ ! -d "${PACKAGE_LIST_DIRECTORY}" ]; then @@ -45,28 +53,30 @@ save_package_list () { dump_sql () { if ! which mysqldump; then - return 2 + log "No mysqldump" + return fi if [ ! -d "${MYSQL_DUMP_DIRECTORY}" ] then mkdir -p "${MYSQL_DUMP_DIRECTORY}" fi - + log "Dumping mysql databases" 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 } dump_mongo () { if ! which mongodump; then - return 2 + log "No mongodump" + return fi if [ ! -d "${MONGODB_DUMP_DIRECTORY}" ] then mkdir -p "${MONGODB_DUMP_DIRECTORY}/mongodump" fi - - mongodump --out "${MONGODB_DUMP_DIRECTORY}/mongodump" + log "Dumping mongo database" + mongodump --out "${MONGODB_DUMP_DIRECTORY}/mongodump" || err "Error dumping mongo database" } @@ -77,6 +87,7 @@ borg_init () { --encryption repokey-blake2 ) fi + log "Initializing ${BORG_REPO}" ${BORG} init "${BORG_INIT_ARGS[@]}" "${BORG_REPO}" } @@ -102,11 +113,14 @@ borg_backup () { BORG_CREATE_ARGS+=( --stats ) 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_ARGS[@]}" \ "${BORG_REPO}"::"${SYSTEM_NAME}-$(date -u +'%Y-%m-%dT%H-%M-%SZ')" \ "${PATHS[@]}" \ - "${EXCLUDE_CMD[@]}" + "${EXCLUDE_CMD[@]}" \ + || log "Error backing up. Has repo been initialized?" } borg_prune () { @@ -122,7 +136,7 @@ borg_prune () { else BORG_PRUNE_ARGS+=( --stats ) fi - + log "Running borg prune" ${BORG} prune \ "${BORG_PRUNE_ARGS[@]}" \ --prefix "${SYSTEM_NAME}-" \ @@ -141,6 +155,7 @@ borg_verify () { ) fi + log "Running borg check" ${BORG} check "${BORG_CHECK_ARGS[@]}" "${BORG_REPO}" } @@ -277,11 +292,16 @@ mkdir -p "${LOCKDIR}" elif [[ ${MODE} == "backup" ]]; then trap 'exit_backup $?' ERR INT TERM save_package_list + echo "saving package list" dump_mongo + echo "dumping mongo" dump_sql + echo "dumping sql" borg_backup + echo "running borg backup" borg_prune + echo "running borg prune" exit_backup 0 elif [[ ${MODE} == "verify" ]]; then trap 'exit_verify $?' ERR INT TERM diff --git a/src/install b/src/install deleted file mode 100644 index bd79067..0000000 --- a/src/install +++ /dev/null @@ -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