feat: Add backup.sh
This commit is contained in:
251
src/lib/backup.sh
Normal file
251
src/lib/backup.sh
Normal file
@ -0,0 +1,251 @@
|
||||
#! /usr/bin/env bash
|
||||
|
||||
# backup.sh - functions for backup processing.
|
||||
# Copyright (c) 2022 ge <https://nixhacks.net/>
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
process_source() {
|
||||
# Run handler function for source URI by scheme.
|
||||
#
|
||||
# Usage: process_source URI
|
||||
|
||||
local uri
|
||||
local scheme
|
||||
local handler
|
||||
|
||||
uri="$1"
|
||||
scheme="${uri%%:*}"
|
||||
|
||||
case "$scheme" in
|
||||
file) handler='backup_files';;
|
||||
mysql) handler='backup_mysql';;
|
||||
postgres) handler='backup_postgres';;
|
||||
sqlite) handler='backup_sqlite';;
|
||||
*) echo "Error: Unsupported URI scheme: $scheme" >&2; exit 1;;
|
||||
esac
|
||||
|
||||
# Run handler function
|
||||
"$handler" "$uri"
|
||||
}
|
||||
|
||||
process_target() {
|
||||
# Run handler function for target URI by scheme.
|
||||
#
|
||||
# Usage: process_target URI
|
||||
|
||||
local uri
|
||||
local scheme
|
||||
local handler
|
||||
|
||||
uri="$1"
|
||||
scheme="${uri%%:*}"
|
||||
|
||||
case "$scheme" in
|
||||
file) handler='transfer_files';;
|
||||
ftp) handler='transfer_ftp';;
|
||||
sftp) handler='transfer_sftp';;
|
||||
rsync) handler='transfer_rsync';;
|
||||
s3) handler='transfer_s3';;
|
||||
sj) handler='transfer_sj';;
|
||||
swift) handler='transfer_swift';;
|
||||
dav) handler='transfer_dav';;
|
||||
davs) handler='transfer_davs';;
|
||||
*) echo "Error: Unsupported URI scheme: $scheme" >&2; exit 1;;
|
||||
esac
|
||||
|
||||
# Run handler function
|
||||
"$handler" "$uri"
|
||||
}
|
||||
|
||||
builtin_backup() {
|
||||
# Backup function.
|
||||
#
|
||||
# Usage: builtin_backup
|
||||
|
||||
for source in "${sources[@]}"; do
|
||||
process_source "$source"
|
||||
done
|
||||
|
||||
for target in "${targets[@]}"; do
|
||||
process_target "$target"
|
||||
done
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------- #
|
||||
# * Backup functions #
|
||||
# ---------------------------------------------------------- #
|
||||
|
||||
backup_files() {
|
||||
# Backup local files with tar(1). Handle 'file' URI scheme.
|
||||
#
|
||||
# Usage: backup_files URI
|
||||
|
||||
local uri
|
||||
local src_path
|
||||
local dst_path
|
||||
local archive
|
||||
local file_ext
|
||||
|
||||
uri="$1"
|
||||
dst_path="$__main_target_path"
|
||||
|
||||
parse_uri "$uri"
|
||||
|
||||
if [ -f "$path" ] || [ -d "$path" ]; then
|
||||
src_path="$path"
|
||||
else
|
||||
echo "Error: Path '$path' from URI '$uri' does not exists" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
is_installed tar # Exit if tar is not installed
|
||||
|
||||
# Overwrire __tar_options
|
||||
if [ -n "$tar_options" ]; then
|
||||
__tar_options="$tar_options"
|
||||
fi
|
||||
|
||||
# TODO выбор сжатия, можно в переменной __tar_options заменять букву z на
|
||||
# другую для соответствующего сжатия
|
||||
file_ext=.tar.gz
|
||||
|
||||
archive="${dst_path}/$(gen_backup_name "$file_ext")"
|
||||
|
||||
tar "$__tar_options" "$archive" "$src_path" |& log -p
|
||||
|
||||
# Append path to 'backups' array
|
||||
backups+=("$archive")
|
||||
}
|
||||
|
||||
backup_mysql() {
|
||||
echo Not implemented >&2; exit 1
|
||||
}
|
||||
|
||||
backup_postgres() {
|
||||
echo Not implemented >&2; exit 1
|
||||
}
|
||||
|
||||
backup_sqlite() {
|
||||
echo Not implemented >&2; exit 1
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------- #
|
||||
# * Functions for targets processing #
|
||||
# ---------------------------------------------------------- #
|
||||
|
||||
transfer_files() {
|
||||
# Transfer files to another location from __main_target_path using cp(1)
|
||||
#
|
||||
# Usage: transfer_files URI
|
||||
|
||||
local uri
|
||||
local dst_path
|
||||
|
||||
uri="$1"
|
||||
|
||||
if [[ "$uri" == "$__main_target" ]]; then
|
||||
: # Do nothing. Source and destination is the same
|
||||
else
|
||||
# Copy backups to another destination
|
||||
parse_uri "$uri"
|
||||
|
||||
if [ -f "$path" ] || [ -d "$path" ]; then
|
||||
dst_path="$path"
|
||||
else
|
||||
echo "Error: Path '$path' from URI '$uri' does not exists" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Copy files preserving metadata
|
||||
for backup in "${backups[@]}"; do
|
||||
cp --archive "$backup" "$dst_path"
|
||||
done
|
||||
fi
|
||||
}
|
||||
|
||||
transfer_ftp() {
|
||||
echo Not implemented >&2; exit 1
|
||||
}
|
||||
|
||||
transfer_sftp() {
|
||||
echo Not implemented >&2; exit 1
|
||||
}
|
||||
|
||||
transfer_rsync() {
|
||||
echo Not implemented >&2; exit 1
|
||||
}
|
||||
|
||||
transfer_s3() {
|
||||
echo Not implemented >&2; exit 1
|
||||
}
|
||||
|
||||
transfer_sj() {
|
||||
echo Not implemented >&2; exit 1
|
||||
}
|
||||
|
||||
transfer_swift() {
|
||||
echo Not implemented >&2; exit 1
|
||||
}
|
||||
|
||||
transfer_dav() {
|
||||
echo Not implemented >&2; exit 1
|
||||
}
|
||||
|
||||
transfer_davs() {
|
||||
echo Not implemented >&2; exit 1
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------- #
|
||||
# * Helper functions #
|
||||
# ---------------------------------------------------------- #
|
||||
|
||||
is_installed() {
|
||||
# Check if the program is installed.
|
||||
# See good answer: https://stackoverflow.com/a/677212
|
||||
#
|
||||
# Usage: is_installed COMMAND
|
||||
|
||||
local cmd
|
||||
|
||||
cmd="$1"
|
||||
|
||||
if ! command -v "$cmd" >/dev/null 2>&1; then
|
||||
echo "Error: Command $cmd not found." \
|
||||
"Please install $cmd or check your PATH if it's actually installed." >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
gen_backup_name() {
|
||||
# Generate backup file name. Return (echo) string.
|
||||
#
|
||||
# Usage: gen_backup_name NAME_EXT
|
||||
|
||||
local prefix
|
||||
local name
|
||||
local date_fmt
|
||||
local name_ext
|
||||
|
||||
[ -n "$name_prefix" ] || { prefix="${__user_script}_"; }
|
||||
name="$(basename $path)" # 'path' is variable parsed from URI
|
||||
name_ext="$1"
|
||||
|
||||
# Overwrite __name_date_fmt
|
||||
if [ -n "$name_date_fmt" ]; then
|
||||
__name_date_fmt="$name_date_fmt"
|
||||
fi
|
||||
|
||||
date +"${prefix}${name}${__name_date_fmt}${name_ext}"
|
||||
}
|
Reference in New Issue
Block a user