Commit b1fa0d5e authored by Björn's avatar Björn

adding

parent 85131eb2
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: tock not found
Error: "tock" not found!
Error: "tock" not found!
Error: "g" not found!
Error: "tock" not found!
Error: "g" not found!
Error: "tock" not found!
Error: "g" not found!
Error: "tock" not found!
Error: "g" not found!
Error: "test" not found!
gpg --list-secret-keys --keyid-format LONG
# backup-tar
A simple bash-script to backup with tar, split and gpg.
......
#!/usr/bin/env bash
#
# functions to read config file
#
#
# @author Björn Hase
#
set -o errexit
set -o pipefail
set -o nounset
_config_file='.config'
# defaults for .config-file
export targets=''
export mysql_username=''
export mysql_password=''
export mysql_host='localhost'
export work_directory=''
export remote_username=''
export remote_host=''
export remote_port=22
export remote_directory=''
export max_age=0
export chunk_size=10g
export gpg_key_id=''
export prefix=''
export excludes=''
#
# options
#
#
_config_options()
{
local option
while [[ $# -gt 0 ]]; do
option="$1"
case $option in
# add config file
-c|--config)
_config_file=${2:-}
;;
esac
shift
done
}
#
# validate
#
#
_config_validate()
{
local error=false
# check if config file is set and load it
if [ -f "${_config_file}" ]; then
source "${_config_file}"
else
echo "Whoops, ${_config_file} not found"
exit 0
fi
# check if config file has chmod 600
if [ "$(stat --format '%a' "${_config_file}")" != '600' ]; then
echo "Whoops, ${_config_file} has wrong Permissions, set this to 600"
error=true
fi
# check if files are set
if [ -z "${targets}" ]; then
echo "Whoops, \"targets\" has to be set in .config"
error=true
fi
# check if remote user is set
if [ -z "${remote_username}" ]; then
echo "Whoops, \"remote_user\" has to be set in .config"
error=true
fi
# check if remote host is set
if [ -z "${remote_host}" ]; then
echo "Whoops, \"remote_host\" has to be set in .config"
error=true
fi
# if any error shows up, exit
if [ "${error}" = true ]; then
exit 0
fi
}
#
# defaults
#
#
_config_defaults()
{
# check if work_directory is set, if not set default
if [ -z "${work_directory}" ]; then
work_directory="$HOME/backup"
fi
# adding trailing slash to remote_directory if set
if [ -n "${remote_directory}" ] && [[ "${remote_directory}" =~ /?([?\#]+.+)?$ ]]; then
remote_directory+="/"
fi
}
#
# init
#
#
_config_init()
{
local error=false
# check if work directory is already in use
if [ -d "${work_directory}" ]; then
echo "Whoops, ${work_directory} already in use, please change work_directory or delete it"
error=true
fi
# if any error shows up, exit
if [ "${error}" = true ]; then
exit 0
fi
mkdir -p "${work_directory}"
}
#!/usr/bin/env bash
#
#
# Usage:
# rsync
#
#
# @author Björn Hase
#
source "$(dirname "$0")/_config.sh"
set -o errexit
set -o pipefail
set -o nounset
_download_remote_path=""
#
# options
#
#
_download_options()
{
_download_filename=${1:-}
}
#
# validate
#
#
_download_validate()
{
local error=false
if [ -z "${_download_filename}" ]; then
echo -e "Whoops! <\$1> must be the filename you want to download"
error=true
fi
# if any error shows up, exit
if [ ${error} = true ]; then
exit 0
fi
}
#
# main
#
#
_download_main()
{
_config_options "$@"
_config_validate
_download_options "$@"
_download_validate
if [ ! -z "${remote_directory}" ]; then
_download_remote_path="${remote_directory}/"
fi
rsync -avH --include="${_download_filename}*" --exclude="*" -e 'ssh -p '"${remote_port}"'' "${remote_username}"@"${remote_host}":"${remote_directory}" "${work_directory}"
}
# let it rain
_download_main "$@"
#!/usr/bin/env bash
#
#
# Usage:
# _gpg_decrypt.sh
#
# options:
# -c|--config-file
#
# @author Björn Hase
#
source "$(dirname "$0")/_config.sh"
set -o errexit
set -o pipefail
set -o nounset
#
# options
#
#
_gpg_decrypt_options()
{
_gpg_decrypt_filename=${1:-}
}
#
# validate
#
#
_gpg_decrypt_validate()
{
local error=false
if [ -z "${_gpg_decrypt_filename}" ]; then
echo -e "Whoops! \$1 must be the filename to join"
error=true
fi
# if any error shows up, exit
if [ "${error}" = true ]; then
exit 0
fi
}
#
# main
#
#
_gpg_decrypt_main()
{
_config_options "$@"
_config_validate
_gpg_decrypt_options "$@"
_gpg_decrypt_validate
if [ -n "$gpg_key_id" ]; then
for encrypt_filename in $(find ${work_directory} -name "${_gpg_decrypt_filename}*.gpg" 2> /dev/null); do
# get decrypted filename
local decrypt_filename
decrypt_filename="${encrypt_filename//.gpg/}"
# decrypt
gpg --output "${decrypt_filename}" --decrypt "${encrypt_filename}"
# delete encrypted file
rm -f -v "${encrypt_filename}"
done
fi
}
# let it rain
_gpg_decrypt_main "$@"
#!/usr/bin/env bash
#
#
# Usage:
# _gpg_encrypt.sh
#
# options:
# -c|--config-file
#
# @author Björn Hase
#
source "$(dirname "$0")/_config.sh"
set -o errexit
set -o pipefail
set -o nounset
#
# options
#
#
_gpg_encrypt_options()
{
_gpg_encrypt_filename=${1:-}
}
#
# validate
#
#
_gpg_encrypt_validate()
{
local error=false
if [ -z "${_gpg_encrypt_filename}" ]; then
echo -e "Whoops! \$1 must be the filename to join"
error=true
fi
# if any error shows up, exit
if [ "${error}" = true ]; then
exit 0
fi
}
#
# main
#
#
_gpg_encrypt_main()
{
_config_options "$@"
_config_validate
_gpg_encrypt_options "$@"
_gpg_encrypt_validate
if [ -n "$gpg_key_id" ]; then
for filename in $(find ${work_directory} -name "${_gpg_encrypt_filename}*" 2> /dev/null); do
gpg --output "${filename}".gpg --encrypt --recipient "${gpg_key_id}" "${filename}"
rm -f -v "${filename}"
done
fi
}
# let it rain
_gpg_encrypt_main "$@"
#!/usr/bin/env bash
#
#
# Usage:
# join.sh
#
# options:
# -c|--config-file
#
# @author Björn Hase
#
source "$(dirname "$0")/_config.sh"
set -o errexit
set -o pipefail
set -o nounset
#
# options
#
#
_join_options()
{
_join_filename=${1:-}
}
#
# validate
#
#
_join_validate()
{
local error=false
if [ -z "${_join_filename}" ]; then
echo -e "Whoops! \$1 must be the filename to join"
error=true
fi
# if any error shows up, exit
if [ "${error}" = true ]; then
exit 0
fi
}
#
# main
#
#
_join_main()
{
_config_options "$@"
_config_validate
_join_options "$@"
_join_validate
local parts=$(find "${work_directory}" -regex '.+\.[0-9]+' | sort -t'/' -n -k10)
if [ ! -z "${parts}" ]; then
cat ${parts} > "${work_directory}/${_join_filename}"
rm -f -v "${parts}"
fi
}
# let it rain
_join_main "$@"
#!/usr/bin/env bash
#
# dump all mysql
#
# Usage:
# mysql.sh
#
# options:
# -c|--config-file
#
# @author Björn Hase
#
source "$(dirname "$0")/_config.sh"
set -o errexit
set -o pipefail
set -o nounset
readonly _mysql_prefix_file="mysql-"$(date --utc +%FT%TZ)
readonly _mysql_ignore="(^mysql|_schema$)"
#
# databases
#
#
_mysql_databases()
{
local sql="SHOW DATABASES WHERE \`Database\` NOT REGEXP '"${_mysql_ignore}"'"
echo $(mysql --user="${mysql_username}" --password="${mysql_password}" --host="${mysql_host}" -e "${sql}"|awk -F " " '{if (NR!=1) print $1}')
}
#
# main
#
#
_mysql_main()
{
_config_options "$@"
_config_validate
# dump each database that you get from _mysql_databases
for database in $(_mysql_databases)
do
mysqldump --user="${mysql_username}" --password="${mysql_password}" --host="${mysql_host}" "${database}" > "${work_directory}/${_mysql_prefix_file}-${database}.sql"
done
}
# let it rain
_mysql_main "$@"
#!/usr/bin/env bash
#
# packing files and spilt them if the are bigger
# than the chunk_size
#
# Usage:
# tar.sh
#
# options:
# -c|--config-file
#
# @author Björn Hase
#
source "$(dirname "$0")/_config.sh"
set -o errexit
set -o pipefail
set -o nounset
#
# options
#
#
_tar_options()
{
_tar_filename=${1:-}
}
#
# validate
#
#
_tar_validate()
{
local error=false
if [ -z "${_tar_filename}" ]; then
echo -e "Whoops! \$1 must be the filename you want to compress"
error=true
fi
# if any error shows up, exit
if [ "${error}" = true ]; then
exit 0
fi
}
#
# get excludes, adding current $work_directory to ignore tar
# and add all files from exludes in config
#
#
#
_tar_get_excludes()
{
_tar_excludes=(--exclude="${work_directory}/${_tar_filename}")
if [ -n "${excludes}" ]; then
# set delimiter
IFS=' '
read -ra excludes <<< "$excludes"
for value in ${excludes[@]}; do
_tar_excludes+=(--exclude="${value}")
done
fi
}
#
# main
#
#
_tar_main()
{
local _tar_fikesize
_config_options "$@"
_config_validate
_tar_options "$@"
_tar_validate
_tar_filename="${_tar_filename}.tar.bz2"
_tar_get_excludes
if [ -n "${mysql_username}" ] && [ -n "${mysql_password}" ]; then
targets="${targets} ${work_directory}/*.sql"
fi
# tar the files
tar "${_tar_excludes[@]}" -cvzf "${work_directory}/${_tar_filename}" ${targets}
# check filesize of archive
_tar_filesize=$(find "${work_directory}" -name "${_tar_filename}" -size +$(echo "${chunk_size}" | tr a-z A-Z) | wc -l)
if [ "$_tar_filesize" -gt 0 ]; then
split -b "${chunk_size}" -a 1 -d "${work_directory}"/"${_tar_filename}" "${work_directory}"/"${_tar_filename}."
rm -v -f "${work_directory}"/"${_tar_filename}"
fi
}
# let it rain
_tar_main "$@"
#!/usr/bin/env bash
#
#
# Usage:
# rsync
#
#
# @author Björn Hase
#
source "$(dirname "$0")/_config.sh"
set -o errexit
set -o pipefail
set -o nounset
_upload_remote_path=""
#
# options
#
#
_upload_options()
{
_upload_filename=${1:-}
}
#
# validate
#
#
_upload_validate()
{
local error=false
if [ -z "${_upload_filename}" ]; then
echo -e "Whoops! \$1 must be the filename you want to upload"
error=true
fi
# if any error shows up, exit
if [ "${error}" = true ]; then
exit 0
fi
}
#
# main
#
#
_upload_main()
{
_config_options "$@"
_config_validate
_upload_options "$@"
_upload_validate
rsync -avH --include="${_upload_filename}*" --exclude="*" "${work_directory}/" -e 'ssh -p '"${remote_port}"'' "${remote_username}"@"${remote_host}":"${remote_directory}"
}
# let it rain
_upload_main "$@"
......@@ -2,12 +2,12 @@
#
#
#
#
class Config:
class AddKey:
#
#
#
#
def __init__(self):
......@@ -11,11 +11,14 @@
# pip3 install python-gnupg
# pip3 install configparser
#
# max_age = s, m, h, D, W, M, Y (seconds, minutes, hours, days, weeks, months, years)
#
import argparse
import configparser
import os
import io
import subprocess
import paramiko
#
#
......@@ -33,12 +36,23 @@ class BackupFiles:
parser = argparse.ArgumentParser()
# define arguments
parser.add_argument('command', help='backup|restore|list')
parser.add_argument('config', help='full path of config-file')
parser.add_argument('command', help='start|restore|status|files')
# define options
parser.add_argument('-c', '--config', help='full path to config-file')
parser.add_argument('-d', '--destination', help='destination for files')
parser.add_argument('-f', '--force', help='force current action', action='store_true')
# parse arguments
self._args = parser.parse_args()
if (self._args.config is None):
config = '.config'
else:
config = self._args.config
self._config = configparser.RawConfigParser(allow_no_value=True)
self._config.read_file(open(self._args.config))
self._config.read_file(open(config))
#
# adding message to log-file
......@@ -85,54 +99,170 @@ class BackupFiles:
return '--encrypt-key=' + self._config.get('default', 'gpg_key_id')
#