Update script using best practices
This commit is contained in:
parent
32f5d053f1
commit
44fe0ab8f9
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
.idea/
|
@ -1,52 +1,50 @@
|
||||
#! /usr/bin/env bash
|
||||
|
||||
# all executed commands are printed to stdout
|
||||
# All executed commands are printed to stdout
|
||||
set -x
|
||||
|
||||
# redirect stdout (and stderr to stdout) to a file
|
||||
exec 1> /var/log/backup/vaultwarden-backup/vaultwarden-backup-$(date +%F).log 2>&1
|
||||
# Redirect stdout (and stderr to stdout) to a file
|
||||
exec 1>/var/log/backup/vaultwarden-backup/vaultwarden-backup-"$(date +%F)".log 2>&1
|
||||
|
||||
# abort on nonzero exitstatus
|
||||
# Abort on nonzero exit status
|
||||
set -o errexit
|
||||
|
||||
# abort on unbound variable
|
||||
# Abort on unbound variable
|
||||
set -o nounset
|
||||
|
||||
# don't hide errors within pipes
|
||||
# Don't hide errors within pipes
|
||||
set -o pipefail
|
||||
|
||||
# set your API key here
|
||||
# Set your API key here
|
||||
key=
|
||||
|
||||
# set your chat id here
|
||||
# Set your chat id here
|
||||
chat_id=
|
||||
|
||||
function send_message() {
|
||||
if [ $# -eq 0 ]
|
||||
then
|
||||
echo "No argument supplied. Please specify the message to send."
|
||||
else
|
||||
curl --silent --show-error --fail --request POST https://api.telegram.org/${key}/sendMessage --data chat_id=${chat_id} --data text="${1}" --output /dev/null
|
||||
fi
|
||||
if [ $# -eq 0 ]; then
|
||||
echo "No argument supplied, please specify the message to send"
|
||||
else
|
||||
curl --silent --show-error --fail --request POST "https://api.telegram.org/$key/sendMessage" --data chat_id="$chat_id" --data text="$1" --output /dev/null
|
||||
fi
|
||||
}
|
||||
|
||||
function backup_vaultwarden {
|
||||
# retrieve the date in YYYY-MM-DD format and the timestamp in H-M-S format
|
||||
backup_time=$(date +%Y-%m-%d_%I-%M-%S)
|
||||
|
||||
# set the destination directory for backups
|
||||
destination_directory=/var/local/vaultwarden/backups/${backup_time}
|
||||
|
||||
# create the destination directory
|
||||
mkdir --parents ${destination_directory}
|
||||
# Retrieve the date in YYYY-MM-DD format and the timestamp in H-M-S format
|
||||
backup_time=$(date +%Y-%m-%d_%I-%M-%S)
|
||||
|
||||
# save the SQLite 3 database
|
||||
if /usr/bin/sqlite3 /var/lib/vaultwarden/data/db.sqlite3 ".backup ${destination_directory}/backup.sqlite3"
|
||||
then
|
||||
send_message "[Backup] - Vaultwarden has just been backuped."
|
||||
else
|
||||
send_message "[Backup] - Error during Vaultwarden bacup. Please log in as soon as possible and see what went wrong."
|
||||
fi
|
||||
# Set the destination directory for backups
|
||||
destination_directory=/var/local/vaultwarden/backups/${backup_time}
|
||||
|
||||
# Create the destination directory
|
||||
mkdir --parents "$destination_directory"
|
||||
|
||||
# Save the SQLite 3 database
|
||||
if /usr/bin/sqlite3 /var/lib/vaultwarden/data/db.sqlite3 ".backup $destination_directory/backup.sqlite3"; then
|
||||
send_message "[Backup] - Vaultwarden has just been backed up"
|
||||
else
|
||||
send_message "[Backup] - Error during Vaultwarden backup, please log in as soon as possible and see what went wrong"
|
||||
fi
|
||||
}
|
||||
|
||||
backup_vaultwarden
|
||||
|
Loading…
Reference in New Issue
Block a user