Skip to content

Commit

Permalink
Merge pull request #145 from ikifar2012/fix-error-notifications
Browse files Browse the repository at this point in the history
Fix error notifications
  • Loading branch information
ikifar2012 authored Jun 1, 2024
2 parents a3fc66f + c44efc6 commit 6ac7ff2
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 16 deletions.
11 changes: 10 additions & 1 deletion remote-backup/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# 2024.6.0

- Fix error notifications
- Fix SSH key permissions
- ⬆️ Update actions/checkout digest to a5ac7e5 by @renovate in https://github.com/ikifar2012/remote-backup-addon/pull/142
- ⬆️ Update Add-on base image to v16 (major) by @renovate in https://github.com/ikifar2012/remote-backup-addon/pull/144

**Full Changelog**: https://github.com/ikifar2012/remote-backup-addon/compare/2024.4.0...2024.5.0

# 2024.4.0

- ⬆️ Update peter-evans/repository-dispatch action to v3 by @renovate in https://github.com/ikifar2012/remote-backup-addon/pull/130
Expand Down Expand Up @@ -72,7 +81,7 @@
**Be aware that some of the configuration options have been renamed and may overwrite your current settings**

Please backup your configuration before upgrading by clicking the vertical dots in the top right corner of the add-on configuration page
and click "Edit in YAML", you can then copy that to a text file and map those settings to their new config options as per the
and click "Edit in YAML", you can then copy that to a text file and map those settings to their new config options as per the
[documentation](https://addons.mathesonsteplock.ca/docs/addons/remote-backup/basic-config).

- enable rsync key-based authentication #51
Expand Down
2 changes: 1 addition & 1 deletion remote-backup/config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Remote Backup
version: "2024.4.0"
version: "2024.6.0"
slug: remote_backup
description: Automatically create and transfer HA backups using SFTP (SCP), rsync, or rclone (experimental)
image: ikifar/remote-backup-{arch}
Expand Down
63 changes: 49 additions & 14 deletions remote-backup/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ function migrate_config {
if bashio::fs.directory_exists "/ssl" && bashio::fs.directory_exists "/config"; then
bashio::log.notice "Migration complete."
fi

}
function run_setup {
# create directory for failed backups
bashio::log.notice "Creating directories for failed backup alerts."
mkdir -p /tmp/backup
mkdir -p /tmp/scp
mkdir -p /tmp/rsync
mkdir -p /tmp/rclone
bashio::log.notice "Directories created."
bashio::log.notice "Setup complete."


}
# script global shortcuts
declare -r BACKUP_NAME="$(bashio::config 'backup_custom_prefix' '') $(date +'%Y-%m-%d %H-%M')"
Expand Down Expand Up @@ -98,10 +111,11 @@ function add-ssh-key {
# prepare SSH key pair
mkdir -p ${SSH_HOME} || bashio::log.error "Failed to create .ssh directory!"
if bashio::config.has_value "remote_key"; then
bashio::log.notice "Setting up SSH key pair."
(
cp "/config/$(bashio::config 'remote_key')" "${SSH_HOME}/id_rsa"
ssh-keygen -y -f ${SSH_HOME}/id_rsa > ${SSH_HOME}/id_rsa.pub
chmod 600 "${SSH_HOME}/id_rsa"
ssh-keygen -y -f ${SSH_HOME}/id_rsa > ${SSH_HOME}/id_rsa.pub
chmod 644 "${SSH_HOME}/id_rsa.pub"
) || bashio::log.error "Failed to create SSH key pair!"
fi
Expand Down Expand Up @@ -173,20 +187,24 @@ function create-local-backup {
data="$(echo $data | tr -d '}'), \"addons\": ${addons}, \"folders\": ${folders}}" # append addon and folder set
if ! SLUG=$(bashio::api.supervisor POST /backups/new/partial "${data}" .slug); then
bashio::log.fatal "Error creating ${bak_type} partial backup!"
return "${__BASHIO_EXIT_NOK}"
touch "/tmp/backup/failed"
fi
else
bashio::log.info "Creating ${bak_type} full backup: \"${BACKUP_NAME}\""

if ! SLUG=$(bashio::api.supervisor POST /backups/new/full "${data}" .slug); then
bashio::log.fatal "Error creating ${bak_type} full backup!"
return "${__BASHIO_EXIT_NOK}"
touch "/tmp/backup/failed"
fi

fi

if bashio::fs.file_exists "/tmp/backup/failed"; then
rm -f "/tmp/backup/failed"
return "${__BASHIO_EXIT_NOK}"
else
bashio::log.info "Backup created: ${SLUG}"
return "${__BASHIO_EXIT_OK}"
fi
}

function copy-backup-to-remote {
Expand All @@ -202,21 +220,28 @@ function copy-backup-to-remote {
fi

bashio::log.info "Copying backup using SFTP/SCP."

(
sshpass -p "${REMOTE_PASSWORD}" \
scp ${DEBUG_FLAG:-} -F "${SSH_HOME}/config" "/backup/${SLUG}.tar" remote:"${remote_directory}/${remote_name}.tar" || (
bashio::log.warning "SFTP transfer failed, falling back to SCP: $(sshpass_error $?)"
sshpass -p "${REMOTE_PASSWORD}" \
scp ${DEBUG_FLAG:-} -O -F "${SSH_HOME}/config" "/backup/${SLUG}.tar" remote:"${remote_directory}/${remote_name}.tar" || (
bashio::log.error "Error copying backup ${SLUG}.tar to ${remote_directory} on ${REMOTE_HOST}: $(sshpass_error $?)"
return "${__BASHIO_EXIT_NOK}"
touch "/tmp/scp/failed"
)
)
)

return "${__BASHIO_EXIT_OK}"
if bashio::fs.file_exists "/tmp/scp/failed"; then
rm -f "/tmp/scp/failed"
return "${__BASHIO_EXIT_NOK}"
else
return "${__BASHIO_EXIT_OK}"
fi
}


function rsync-folders {
if ! bashio::config.true "rsync_enabled"; then
bashio::log.debug "Rsync disabled."
Expand All @@ -239,11 +264,15 @@ function rsync-folders {
(
sshpass -p "${REMOTE_PASSWORD}" rsync ${flags} --port ${REMOTE_PORT} --exclude-from='/tmp/rsync_exclude.txt' ${folders} "${rsync_url}/" --delete || (
bashio::log.error "Error rsyncing folder(s) ${folders} to ${rsync_url}: $(sshpass_error $?)!"
return "${__BASHIO_EXIT_NOK}"
touch "/tmp/rsync/failed"
)
)

return "${__BASHIO_EXIT_OK}"
if bashio::fs.file_exists "/tmp/rsync/failed"; then
rm -f "/tmp/rsync/failed"
return "${__BASHIO_EXIT_NOK}"
else
return "${__BASHIO_EXIT_OK}"
fi
}

function rclone-backups {
Expand Down Expand Up @@ -273,7 +302,7 @@ function rclone-backups {
(
rclone ${DEBUG_FLAG:-} copyto "/backup/${SLUG}.tar" "${rclone_remote_host}:${remote_directory}/${remote_name}.tar" || (
bashio::log.error "Error rclone ${SLUG}.tar to ${rclone_remote_host}:${remote_directory}/${remote_name}.tar!"
return "${__BASHIO_EXIT_NOK}"
touch "/tmp/rclone/failed"
)
)
fi
Expand All @@ -282,7 +311,7 @@ function rclone-backups {
(
rclone ${DEBUG_FLAG:-} sync "/backup" "${rclone_remote_host}:${remote_directory}" || (
bashio::log.error "Error syncing backups by rclone!"
return "${__BASHIO_EXIT_NOK}"
touch "/tmp/rclone/failed"
)
)
fi
Expand All @@ -293,11 +322,16 @@ function rclone-backups {
(
rclone ${DEBUG_FLAG:-} copyto "${rclone_remote_host}:${remote_directory}" "/backup/${restore_name}/" || (
bashio::log.error "Error restoring backups from ${rclone_remote_host}:${remote_directory}!"
return "${__BASHIO_EXIT_NOK}"
touch "/tmp/rclone/failed"
)
)
fi
return "${__BASHIO_EXIT_OK}"
if bashio::fs.file_exists "/tmp/rclone/failed"; then
rm -f "/tmp/rclone/failed"
return "${__BASHIO_EXIT_NOK}"
else
return "${__BASHIO_EXIT_OK}"
fi
}

function clone-to-remote {
Expand Down Expand Up @@ -355,11 +389,12 @@ function delete-local-backup {
# general setup and backup
set-debug-level
migrate_config
run_setup
add-ssh-key
create-local-backup || die "Local backup process failed! See log for details."
clone-to-remote || die "Cloning backup(s) to remote host ${REMOTE_HOST} failed! See log for details."
delete-local-backup || die "Removing local backup(s) failed! See log for details."

bashio::log.info "Backup process done!"
fire-event "ok" "Backup ${BACKUP_NAME} created."
bashio::exit.ok
bashio::exit.ok

0 comments on commit 6ac7ff2

Please sign in to comment.