Browse Source

allow to backup a specific host

master
max.mehl 3 months ago
parent
commit
6da9f1fabc
Signed by: mxmehl GPG Key ID: 2704E4AB371E2E92
  1. 66
      README.md
  2. 2
      hosts.csv.sample
  3. 21
      uberspace-backup.sh

66
README.md

@ -7,17 +7,27 @@ SPDX-License-Identifier: GPL-3.0-or-later
[![REUSE compliant](https://api.reuse.software/badge/src.mehl.mx/mxmehl/uberspace-backup)](https://api.reuse.software/info/src.mehl.mx/mxmehl/uberspace-backup)
This Bash script is able to backup directories from Uberspace users (and also other SSH resources). For Uberspace hosts it can also backup MySQL databases by copying the backups Uberspace cleverly created for you.
This Bash script is able to backup directories from Uberspace users (and also
other SSH resources). For Uberspace hosts it can also backup MySQL databases by
copying the backups Uberspace cleverly created for you.
It is designed to work automatically on another server with enough harddisk space.
It is designed to work automatically on another server with enough harddisk
space.
## Features
- Transfers files securely via rsync over SSH.
- Encrypts backups with GnuPG, using a public key only. Once a backup is encrypted it can only be decrypted with a private key. Make sure to delete the private key from the backupping server (after saving it on a more secure space of course) to keep your backups even safer.
- If desired, it can delete older backups and only retain a configurable amount of backups.
- Encrypts backups with GnuPG, using a public key only. Once a backup is
encrypted it can only be decrypted with a private key. Make sure to delete the
private key from the backupping server (after saving it on a more secure space
of course) to keep your backups even safer.
- If desired, it can delete older backups and only retain a configurable amount
of backups.
- Rather verbose logs will be written to backup.log.
- With the helper script `ssh-checker.sh` one can automatically test whether the hosts provided in the hosts file can be accessed. If not, the little helper is trying to put your public SSH key to the remote hosts' authorized_keys files by letting you type in the password manually once.
- With the helper script `ssh-checker.sh` one can automatically test whether the
hosts provided in the hosts file can be accessed. If not, the little helper is
trying to put your public SSH key to the remote hosts' authorized_keys files
by letting you type in the password manually once.
## Configuration
@ -25,24 +35,45 @@ Configuration happens in two files: config.cfg and hosts.csv.
### config.cfg
Everything should be self-explanatory with the comments. Make sure to use the correct GPG fingerprint, and make sure to have its public key imported by the user executing the script. No private key has to be installed on the backupping system (but on the decrypting one of course).
Everything should be self-explanatory with the comments. Make sure to use the
correct GPG fingerprint, and make sure to have its public key imported by the
user executing the script. No private key has to be installed on the backupping
system (but on the decrypting one of course).
### hosts.csv
This file contains the hosts and its directories that shall be saved. It consists of two rows separated by `;`. The first one contains a `username@hostname` combination that will be used to sync files via SSH, and also as the backup destination directory name.
This file contains the hosts and its directories that shall be saved. It
consists of two rows separated by `;`. The first one contains a
`username@hostname` combination that will be used to sync files via SSH, and
also as the backup destination directory name.
The latter one contains all source directories that shall be transferred. This can be absolute file paths, or – if it's a Uberspace host – some special shortcuts:
The latter one contains all source directories that shall be transferred. This
can be absolute file paths, or – if it's a Uberspace host – some special
shortcuts:
- `%virtual` backups the virtual folder of your uberspace host (`/var/www/virtual/username/`) where for example the `html` folder is located in.
- `%mysql` downloads the latest backup of your MySQL databases that have been created by Uberspace themselves (their backup system is quite sophisticated).
- `%mails` downloads the directory `users` in the home directory which contains all email files of virtual mail users.
- `%home` simply downloads the whole user's home directory.
- `%virtual` backups the virtual folder of your uberspace host
(`/var/www/virtual/username/`) where for example the `html` folder is located
in.
- `%mysql` downloads the latest backup of your MySQL databases that have been
created by Uberspace themselves (their backup system is quite sophisticated).
- `%mails` downloads the directory `users` in the home directory which contains
all email files of virtual mail users.
- `%home` simply downloads the whole user's home directory.
You can give multiple locations that shall be backed up. Just separate them by `|` characters. See the example file for more.
You can give multiple locations that shall be backed up. Just separate them by
`|` characters. See the example file for more.
## Manual run
You can run `ssh-checker.sh` and `uberspace-backup.sh` manually. Without any arguments given, both will check/backup all hosts.
You can provide an argument to check/backup a specific host. This argument has
to fully match a server's `user@hostname[:port]` declaration as on `hosts.csv`.
## Automatic runs
In order to let the script run regularily, simply put the script's absolute path in a cron file. For example, run `crontab -e` and insert at the bottom:
In order to let the script run regularily, simply put the script's absolute path
in a cron file. For example, run `crontab -e` and insert at the bottom:
```
10 3 * * * /home/archiver/uberspace-backup/uberspace-backup.sh
@ -52,5 +83,8 @@ This will run the backups every night at 3:10.
## Known limitations
- Please note that paths like `~` or `$HOME` haven't been tested yet. Use absolute paths instead.
- At the moment, the backups don't follow symbolic links. That's why for example error logs aren't downloaded when using `%virtual`. Make sure to regularily check your backups to make sure all important files are saved.
- Please note that paths like `~` or `$HOME` haven't been tested yet. Use
absolute paths instead.
- At the moment, the backups don't follow symbolic links. That's why for example
error logs aren't downloaded when using `%virtual`. Make sure to regularly
check your backups to make sure all important files are saved.

2
hosts.csv.sample

@ -1,6 +1,6 @@
# SPDX-FileCopyrightText: 2019 Max Mehl <mail [at] mehl [dot] mx>
# SPDX-License-Identifier: CC0-1.0
# Username@Hostname; Path1 | Path2 | Path3; Uberspace version (default = 7)
# Username@Hostname[:Port]; Path1 | Path2 | Path3; Uberspace version (default = 7)
user@host.uberspace.de; %virtual | %mysql | /home/user/service
root@server:2222; /home

21
uberspace-backup.sh

@ -26,6 +26,8 @@ else
SSH_KEY=~/.ssh/id_rsa
fi
ARG1="$1"
# Get current date
DATE=$(date +"%Y-%m-%d_%H-%M")
LOG="$CURDIR"/backup.log
@ -48,6 +50,18 @@ while read -r line; do
if echo "$line" | grep -qE "^\s*(#|$)"; then continue; fi
RHOST=$(echo "$line" | cut -d";" -f1 | trim)
# Jump to next line if this line's host does not match host of ARG1 (if given)
if [[ "${ARG1}" != "" ]] && [[ "${ARG1}" != "${RHOST}" ]]; then
continue
fi
# Task ssh-checker.sh to check this host
if ! "${CURDIR}"/ssh-checker.sh "${RHOST}"; then
logecho "${RHOST}: ERROR when connecting via SSH. Please run ssh-checker.sh to debug."
logecho "${RHOST}: Aborting backup after an error."
continue
fi
RUSER=$(echo "$RHOST" | cut -d"@" -f1)
ALLRDIR=$(echo "$line" | cut -d";" -f2 | trim)
US_VERSION=$(echo "$line" | cut -d";" -f3 | trim)
@ -65,13 +79,6 @@ while read -r line; do
logecho "${RHOST}: Starting backups"
# Task ssh-checker.sh to check this host
if ! "${CURDIR}"/ssh-checker.sh "${RHOST}"; then
logecho "${RHOST}: ERROR when connecting via SSH. Please run ssh-checker.sh to debug."
logecho "${RHOST}: Aborting backup after an error."
continue
fi
NORDIR=$(echo "$ALLRDIR" | grep -o "|" | wc -l)
NORDIR=$(($NORDIR + 1))

Loading…
Cancel
Save