9 Commits

Author SHA1 Message Date
Asif Bacchus a26bb36f17 define containerName once 2019-09-10 17:22:43 -06:00
Asif Bacchus bbbdf38dcc change mailcowConfigFilePath to explicit param 2019-09-10 16:58:30 -06:00
Asif Bacchus 43f1bb0021 update gitignore, add gitattributes 2019-09-10 16:34:53 -06:00
Asif Bacchus b371e18ab0 'containerName' instead of 'rname' for consistency 2019-09-10 15:23:45 -06:00
Asif Bacchus fb8091ecce Merge pull request #1 from hockeymikey/master
Ensure non-exactly named services are found
2019-09-09 16:45:55 -06:00
hockeymikey 25c6b407a6 Ensure non-exactly named services are found
My naming scheme for my mailcown was like -mailcow_1_k8bdj9op so the script never found them. This ensures they do.
2019-09-07 12:33:54 -05:00
Asif Bacchus 3ddc2c5d4a quoted path-related vars to prevent issues with spaces in path 2019-01-30 12:27:54 -07:00
Asif Bacchus 82f80d0f83 made explanations clearer regarding remote server and keys 2019-01-30 12:22:02 -07:00
Asif Bacchus 0597127cbf clear up confusion between ssh and borg key passwords 2019-01-30 12:19:24 -07:00
6 changed files with 239 additions and 96 deletions
+75
View File
@@ -0,0 +1,75 @@
# Common settings that generally should always be used with your language specific settings
# Auto detect text files and perform LF normalization
# https://www.davidlaing.com/2012/09/19/customise-your-gitattributes-to-become-a-git-ninja/
* text=auto
#
# The above will handle all files NOT found below
#
# Documents
*.bibtex text diff=bibtex
*.doc diff=astextplain
*.DOC diff=astextplain
*.docx diff=astextplain
*.DOCX diff=astextplain
*.dot diff=astextplain
*.DOT diff=astextplain
*.pdf diff=astextplain
*.PDF diff=astextplain
*.rtf diff=astextplain
*.RTF diff=astextplain
*.md text
*.tex text diff=tex
*.adoc text
*.textile text
*.mustache text
*.csv text
*.tab text
*.tsv text
*.txt text
*.sql text
# Graphics
*.png binary
*.jpg binary
*.jpeg binary
*.gif binary
*.tif binary
*.tiff binary
*.ico binary
# SVG treated as an asset (binary) by default.
*.svg text
# If you want to treat it as binary,
# use the following line instead.
# *.svg binary
*.eps binary
# Scripts
*.bash text eol=lf
*.sh text eol=lf
# These are explicitly windows files and should use crlf
*.bat text eol=crlf
*.cmd text eol=crlf
*.ps1 text eol=crlf
# Serialisation
*.json text
*.toml text
*.xml text
*.yaml text
*.yml text
# Archives
*.7z binary
*.gz binary
*.tar binary
*.zip binary
#
# Exclude files from exporting
#
.gitattributes export-ignore
.gitignore export-ignore
+6 -1
View File
@@ -1,5 +1,10 @@
# ignore all vscode config files
.vscode/
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/numbered-bookmarks.json
# ignore all generated logfiles
*.log
+3
View File
@@ -0,0 +1,3 @@
{
"bookmarks": []
}
+82 -29
View File
@@ -25,33 +25,37 @@ This script automates the following tasks:
- [Environment notes](#environment-notes)
- [Why this script must be run as root](#why-this-script-must-be-run-as-root)
- [Script parameters](#script-parameters)
- [Optional parameters](#optional-parameters)
- [Docker container STOP timeout before error: -1 _number_](#docker-container-stop-timeout-before-error--1-_number_)
- [Docker container START timeout before error: -2 _number_](#docker-container-start-timeout-before-error--2-_number_)
- [Path to 503 error page: -5 _/path/to/filename.html_](#path-to-503-error-page--5-_pathtofilenamehtml_)
- [Path to borg details file: -b _/path/to/filename.file_](#path-to-borg-details-file--b-_pathtofilenamefile_)
- [File name of docker-compose configuration file: -d _filename.file_](#file-name-of-docker-compose-configuration-file--d-_filenamefile_)
- [Log file location: -l _/path/to/filename.file_](#log-file-location--l-_pathtofilenamefile_)
- [File name of Mailcow master configuration file: -m _filename.file_](#file-name-of-mailcow-master-configuration-file--m-_filenamefile_)
- [Verbose output from borg: -v (no arguments)](#verbose-output-from-borg--v-no-arguments)
- [Path to webroot: -w _/path/to/webroot/_](#path-to-webroot--w-_pathtowebroot_)
- [Optional parameters](#optional-parameters)
- [Docker container STOP timeout before error: -1 _number_](#docker-container-stop-timeout-before-error--1-number)
- [Docker container START timeout before error: -2 _number_](#docker-container-start-timeout-before-error--2-number)
- [Path to 503 error page: -5 _/path/to/filename.html_](#path-to-503-error-page--5-pathtofilenamehtml)
- [Path to borg details file: -b _/path/to/filename.file_](#path-to-borg-details-file--b-pathtofilenamefile)
- [File name of docker-compose configuration file: -d _filename.file_](#file-name-of-docker-compose-configuration-file--d-filenamefile)
- [Log file location: -l _/path/to/filename.file_](#log-file-location--l-pathtofilenamefile)
- [File name of Mailcow master configuration file: -m _filename.file_](#file-name-of-mailcow-master-configuration-file--m-filenamefile)
- [Verbose output from borg: -v (no arguments)](#verbose-output-from-borg--v-no-arguments)
- [Path to webroot: -w _/path/to/webroot/_](#path-to-webroot--w-pathtowebroot)
- [Borg details file](#borg-details-file)
- [Protect your borg details file](#protect-your-borg-details-file)
- [borg specific entries (lines 1-4)](#borg-specific-entries-lines-1-4)
- [additional files/directories to backup](#additional-filesdirectories-to-backup)
- [exclusion patterns](#exclusion-patterns)
- [prune timeframe options](#prune-timeframe-options)
- [borg remote location](#borg-remote-location)
- [Examples](#examples)
- [Protect your borg details file](#protect-your-borg-details-file)
- [borg specific entries (lines 1-4)](#borg-specific-entries-lines-1-4)
- [Line 1: Path to borg base directory](#line-1-path-to-borg-base-directory)
- [Line 2: Path to SSH key for remote server](#line-2-path-to-ssh-key-for-remote-server)
- [Line 3: Connection string to remote repo](#line-3-connection-string-to-remote-repo)
- [Line 4: Password for borg repo/repo key](#line-4-password-for-borg-reporepo-key)
- [additional files/directories to backup](#additional-filesdirectories-to-backup)
- [exclusion patterns](#exclusion-patterns)
- [prune timeframe options](#prune-timeframe-options)
- [borg remote location](#borg-remote-location)
- [Examples](#examples)
- [503 functionality](#503-functionality)
- [Conditional forwarding by your webserver](#conditional-forwarding-by-your-webserver)
- [NGINX](#nginx)
- [Apache](#apache)
- [Disabling 503 functionality altogether](#disabling-503-functionality-altogether)
- [Conditional forwarding by your webserver](#conditional-forwarding-by-your-webserver)
- [NGINX](#nginx)
- [Apache](#apache)
- [Disabling 503 functionality altogether](#disabling-503-functionality-altogether)
- [Scheduling: Cron](#scheduling-cron)
- [The log file](#the-log-file)
- [Using Logwatch](#using-logwatch)
- [Remember to rotate your logs](#remember-to-rotate-your-logs)
- [Using Logwatch](#using-logwatch)
- [Remember to rotate your logs](#remember-to-rotate-your-logs)
- [Final notes](#final-notes)
## Installation/copying
@@ -209,9 +213,9 @@ example entries. The file must have the following information in the following
order:
1. path to borg base directory **(required)**
2. path to ssh private key for repo **(required)**
2. path to ssh private key for remote server **(required)**
3. connection string to remote repo **(required)**
4. password for ssh key/repo **(required)**
4. password for borg repo/repo key **(required)**
5. path to file listing additional files/directories to backup
6. path to file containing borg-specific exclusion patterns
7. prune timeframe options
@@ -234,10 +238,59 @@ chmod 600 mc_borg.details # grant access to root user only (read/write)
If you need help with these options, then you should consult the borg
documentation or search my blog at
[https://mytechiethoughts.com](https://mytechiethoughts.com) for borg. This is
especially true if you want to understand why an SSH key and passphrase are
preferred and why just a passphrase on it's own presents problems automating
borg backups.
[https://mytechiethoughts.com](https://mytechiethoughts.com) for borg. Here's a
very brief overview:
#### Line 1: Path to borg base directory
This is primary directory on your local system where your borg configuration is
located, **NOT* the path to your borg binary. The base directory contains the
borg configuration, cache, security files and keys.
#### Line 2: Path to SSH key for remote server
This is the SSH key used to connect to your remote (backup) server where your
borg repo is located. **This is NOT your borg repo key!**
> Please note: If you are planning on executing this script via cron or some
> other form of automation, it is *highly recommended* that you use an SSH key
> **without** a password! SSH is designed such that passwords cannot simply be
> passed to it via environment variables, etc. so this is something not easily
> automated by a script such as this for security reasons. As such, your
> computer will sit and wait for you to enter the password and will NOT execute
> the actual backup portion of the script until the SSH key password is provided.
>
> If you really want/need to use an SSH key password, you will have to look into
> somethign like GNOME keyring or SSH-agent to provide a secure automated way to
> provide that password to SSH and allow this script to continue.
>
> In practice, SSH keys without passwords are still quite safe since the key
> must still be known in order to connect and most keys are quite long. In
> addition, they key only connects to the remote server, your actual information
> within the borg repository is still encrypted and secured with both a key and
> password.
#### Line 3: Connection string to remote repo
This is the full server and path required to connect to your borg repo on the
remote server. Very often it is the in the form of:
```
user@servername.tld:repo-name/
```
for rsync.net it is in the following form:
```
username@server-number.rsync.net:repo-name/
```
#### Line 4: Password for borg repo/repo key
This is the password needed to access and decrypt your *borg repo*. Assuming
you set up your borg repo using recommended practices, this will actually be the
password for your *borg repo private key*. **This is NOT your SSH key
password!**
### additional files/directories to backup
Regular → Executable
+65 -58
View File
@@ -33,10 +33,15 @@ function scriptHelp {
echo -e "\tMailcow to operational status."
echo -e "\nThe readme file included in this script's git contains detailed"
echo -e "usage information. The following is a brief summary:\n"
echo -e "${bold}***This script scans for your mailcow configuration file"
echo -e "either with the default name or with your provided filename. If"
echo -e "multiple files with this name are on your system, the script"
echo -e "WILL get confused and exit with errors***${normal}${default}"
echo -e "${bold}***You MUST provide the full path to your mailcow"
echo -e "configuration file using the '-m' parameter***${normal}${default}"
echo -e "${bold}${note}\nMandatory parameters:${normal}${default}"
echo -e "${lit}\n-m, File name of the Mailcow build configuration file${default}"
echo -e "FULL PATH to your Mailcow master build configuration file containing"
echo -e "all variables and configuration info unique to your Mailcow setup."
echo -e "The path specified here is also used for all docker-related"
echo -e "operations in this script."
echo -e "${info}Default: <none>${default}"
echo -e "${bold}${note}\nOptional parameters:${normal}${default}"
echo -e "${lit}\n-1, Timeout for containers to STOP before error${default}"
echo -e "The number of seconds to wait for a docker container to STOP"
@@ -63,20 +68,12 @@ function scriptHelp {
echo -e "${lit}\n-d, File name of the docker-compose configuration file${default}"
echo -e "Name of the docker-compose configuration file that Mailcow uses"
echo -e "to build/start/stop all containers. This will only be searched"
echo -e "for in the path found to contain your mailcow configuration file."
echo -e "for in the path provided for your mailcow configuration file."
echo -e "${info}Default: docker-compose.yml${default}"
echo -e "${lit}\n-l, Location to save log file${default}"
echo -e "This script writes a detailed log file of all activities. It is"
echo -e "structured in an way easy for log parsers (like Logwatch) to read."
echo -e "${info}Default: ScriptPath/ScriptName.log${default}"
echo -e "${lit}\n-m, File name of the Mailcow build configuration file${default}"
echo -e "Name of the Mailcow master build configuration file that has all"
echo -e "variables and configuration info unique to your Mailcow setup."
echo -e "This script will search for any file matching what you specify"
echo -e "so please ensure you don't have multiple files laying around with"
echo -e "the same name! The path where this file is found is used for all"
echo -e "docker-based operations in this script."
echo -e "${info}Default: mailcow.conf${default}"
echo -e "${lit}\n-v, Verbose output from borgbackup${default}"
echo -e "By default, this script will only log summary data from borg."
echo -e "If you need/want more detailed information, the verbose setting"
@@ -228,26 +225,25 @@ function cleanup {
### operate docker containers
function operateDocker {
# determine action to take
if [ "$1" = "stop" ]; then
echo -e "${op}[$(stamp)] Stopping ${2}-mailcow container...${normal}" \
>> "$logFile"
docker-compose stop --timeout ${dockerStopTimeout} ${2}-mailcow \
2>> "$logFile"
# verify container stopped (should return true)
dockerResultState=$(docker inspect -f '{{ .State.Running }}' \
${COMPOSE_PROJECT_NAME}_${2}-mailcow_1)
# verify clean stop (exit code 0)
dockerResultExit=$(docker inspect -f '{{ .State.ExitCode }}' \
${COMPOSE_PROJECT_NAME}_${2}-mailcow_1)
elif [ "$1" = "start" ]; then
echo -e "${op}[$(stamp)] Starting ${2}-mailcow container...${normal}" \
>> "$logFile"
docker-compose start ${2}-mailcow 2>> "$logFile"
# verify
dockerResultState=$(docker inspect -f '{{ .State.Running }}' \
${COMPOSE_PROJECT_NAME}_${2}-mailcow_1)
fi
containerName="$(docker ps --format '{{ .Names }}' --filter name=${COMPOSE_PROJECT_NAME}_${2}-mailcow_1)"
# determine action to take
if [ "$1" = "stop" ]; then
echo -e "${op}[$(stamp)] Stopping ${2}-mailcow container...${normal}" \
>> "$logFile"
docker-compose stop --timeout ${dockerStopTimeout} ${2}-mailcow \
2>> "$logFile"
# verify container stopped (should return true)
dockerResultState="$(docker inspect -f '{{ .State.Running }}' $containerName)"
# verify clean stop (exit code 0)
dockerResultExit="$(docker inspect -f '{{ .State.ExitCode }}' $containerName)"
elif [ "$1" = "start" ]; then
echo -e "${op}[$(stamp)] Starting ${2}-mailcow container...${normal}" \
>> "$logFile"
docker-compose start ${2}-mailcow 2>> "$logFile"
# verify
dockerResultState="$(docker inspect -f '{{ .State.Running }}' $containerName)"
fi
}
### End of Functions ###
@@ -261,9 +257,6 @@ scriptPath="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
scriptName="$( basename ${0} )"
logFile="$scriptPath/${scriptName%.*}.log"
# Set default mailcow configuration filename
mailcowConfigFile=mailcow.conf
# Set default docker-compose filename
dockerComposeFile=docker-compose.yml
@@ -384,8 +377,8 @@ while getopts ':l:v5:w:b:m:d:1:2:' PARAMS; do
borgDetails="${OPTARG%/}"
;;
m)
# name of mailcow configuration file
mailcowConfigFile="${OPTARG}"
# full path to mailcow.conf configuration file
mailcowConfigFilePath="${OPTARG%/}"
;;
d)
# name of docker-compose configuration file
@@ -415,20 +408,33 @@ if [ $(id -u) -ne 0 ]; then
exit 3
fi
## Find mailcow configuration file so additional variables can be read
mailcowConfigFilePath=$( find / -mount -name "$mailcowConfigFile" -print )
## verify mailcow.conf location provided
if [ -z "$mailcowConfigFilePath" ]; then
echo -e "\n${err}Could not locate the specified mailcow configuration" \
"file: ${lit}${mailcowConfigFile}${normal}"
echo -e "\n${err}You MUST provide the full path to your mailcow.conf" \
"configuration file. Exiting.${normal}"
exit 1
fi
## Find docker-compose file using mailcow configuration file path as a reference
mailcowPath="${mailcowConfigFilePath%/$mailcowConfigFile*}"
dockerComposeFilePath="$mailcowPath/$dockerComposeFile"
checkExist ff "$dockerComposeFilePath"
checkResult="$?"
if [ "$checkResult" = 1 ]; then
## verify mailcow.conf and extract path
if checkExist ff "$mailcowConfigFilePath"; then
# extract directory name
case $mailcowConfigFilePath in
*/*)
mailcowPath=${mailcowConfigFilePath%/*}
;;
*)
mailcowPath="."
;;
esac
else
echo -e "\n${err}Could not locate the specified mailcow configuration" \
"file: ${lit}${mailcowConfigFilePath}${normal}"
exit 1
fi
# verify docker-compose.yml exists at location of mailcow.conf (standard setup)
dockerComposeFilePath="${mailcowPath}/${dockerComposeFile}"
if ! checkExist ff "$dockerComposeFilePath"; then
echo -e "\n${err}Could not locate docker-compose configuration file:" \
"${lit}${dockerComposeFilePath}${normal}"
exit 1
@@ -453,8 +459,9 @@ echo -e "${info}[$(stamp)] -- [INFO] using ${lit}${mailcowConfigFilePath}" \
echo -e "${info}[$(stamp)] -- [INFO] using ${lit}${dockerComposeFilePath}" \
>> "$logFile"
### Import additional variables from mailcow configuration file
source "${mailcowConfigFilePath}"
source "$mailcowConfigFilePath"
### Export PATH so this script can access all docker and docker-compose commands
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
@@ -797,21 +804,21 @@ if [ -z "$borgExclude" ]; then
echo -e "${bold}${op}[$(stamp)] Executing borg without exclusions${normal}" \
>> "$logFile"
borg --show-rc create ${borgCreateParams} ::`date +%Y-%m-%d_%H%M%S` \
${xtraFiles[@]} \
${sqlDumpDir} \
${dockerVolumeMail} ${dockerVolumeRspamd} ${dockerVolumePostfix} \
${dockerVolumeRedis} ${dockerVolumeCrypt} \
"${xtraFiles[@]}" \
"${sqlDumpDir}" \
"${dockerVolumeMail}" "${dockerVolumeRspamd}" "${dockerVolumePostfix}" \
"${dockerVolumeRedis}" "${dockerVolumeCrypt}" \
2>> "$logFile"
else
# borgExclude is not empty
echo -e "${bold}${op}[$(stamp)] Executing borg with exclusions${normal}" \
>> "$logFile"
borg --show-rc create ${borgCreateParams} --exclude-from ${borgExclude} \
borg --show-rc create ${borgCreateParams} --exclude-from "${borgExclude}" \
::`date +%Y-%m-%d_%H%M%S` \
${xtraFiles[@]} \
${sqlDumpDir} \
${dockerVolumeMail} ${dockerVolumeRspamd} ${dockerVolumePostfix} \
${dockerVolumeRedis} ${dockerVolumeCrypt} \
"${xtraFiles[@]}" \
"${sqlDumpDir}" \
"${dockerVolumeMail}" "${dockerVolumeRspamd}" "${dockerVolumePostfix}" \
"${dockerVolumeRedis}" "${dockerVolumeCrypt}" \
2>> "$logFile"
fi
+8 -8
View File
@@ -1,8 +1,8 @@
<path to borgbackup base directory> /var/borgbackup
<path to SSH private key for repo> /var/borgbackup/sshPrivate.key
<connection string to remote repo> user@server-number.rsync.net:repoName/
<password for SSH key/repo> pAsSwOrd
<path to file listing extra files> /root/scripts/xtraLocations.borg
<path to file with exclusions> /root/scripts/excludeLocations.borg
<purge timeframe options> --keep-within=7d --keep-daily=30 --keep-weekly=12 --keep-monthly=-1
<location of borg remote instance> borg1
<path to borgbackup base directory> /var/borgbackup
<path to SSH private key for remote server> /var/borgbackup/sshPrivate.key
<connection string to remote repo> user@servername.tld:repoName/
<password for repo> pAsSwOrd
<path to file listing extra files> /root/scripts/xtraLocations.borg
<path to file with exclusions> /root/scripts/excludeLocations.borg
<purge timeframe options> --keep-within=7d --keep-daily=30 --keep-weekly=12 --keep-monthly=-1
<location of borg remote instance> borg1