Update Code
This commit is contained in:
103
README.md
103
README.md
@@ -1,91 +1,28 @@
|
||||
tar-multibackup
|
||||
===============
|
||||
Bash script to backup multiple folders and to clean up old backups based on a retention time. Features configurable post/pre-commands, tar excludes as well as backup retentions.
|
||||
# 🔐 sshs – Interaktiver SSH-Verbindungshelfer mit `fzf`
|
||||
|
||||
### Installation
|
||||
cd /usr/local/src
|
||||
git clone https://gitea.gnilebein.de/gnilebein/tar-multibackup.git
|
||||
ln -sf /usr/local/src/tar-multibackup/multibackup /usr/local/bin/multibackup
|
||||
chmod +x multibackup
|
||||
cp /usr/local/src/tar-multibackup/multibackup.conf ~/.multibackup.conf
|
||||
Ein komfortables Bash-Skript zur schnellen SSH-Verbindung mit bekannten oder neuen Hosts.
|
||||
Jetzt auch mit direktem Aufruf über Parameter (z. B. `sshs host:port`).
|
||||
|
||||
### Configuration and usage
|
||||
---
|
||||
|
||||
* `timestamp` = Format of the timestamp, used in the backup target filename
|
||||
* `backup_destination` = Directory which is used to store the archives/backups
|
||||
* `folders_to_backup` = Array of folders to backup
|
||||
* `backup_retention` = Retention time how long we should keep the backups
|
||||
* `pre_commands` = Array of commands that are executed before the backup starts (stop specific service)
|
||||
* `post_commands` = Array of commands that are executed after the backup finished (start specific service)
|
||||
* `tar_options` = Additional tar Options
|
||||
## 📦 Features
|
||||
|
||||
### Environment configurations
|
||||
- Liest bekannte Hosts aus `~/.ssh/known_hosts`
|
||||
- Zeigt Liste als `host:port` (durchsuchbar)
|
||||
- Direkteingabe oder freie manuelle Eingabe möglich
|
||||
- **Parameter-Unterstützung:**
|
||||
- `sshs host`
|
||||
- `sshs host:port`
|
||||
- `sshs user@host`
|
||||
- `sshs user@host:port`
|
||||
- Standard-Port 22 bei fehlender Angabe
|
||||
- Benutzername optional wählbar
|
||||
|
||||
* `DEBUG` = if set to "true", `set -x` will be set
|
||||
* `CONFIG` = if you want to use a different configuration file than the
|
||||
---
|
||||
|
||||
Example:
|
||||
## 🧪 Beispiele
|
||||
|
||||
CONFIG=/tmp/testbackup.conf DEBUG=true multibackup
|
||||
Interaktiv starten:
|
||||
|
||||
#### Example configuration
|
||||
|
||||
In the example below you can find a `multibackup` configuration file to backup a productional [LiveConfig](http://www.liveconfig.com/) instance.
|
||||
|
||||
`vi ~/.multibackup.conf`
|
||||
|
||||
# Timestamp format, used in the backup target filename
|
||||
timestamp=$(date +%Y%m%d)
|
||||
|
||||
# Destination where you want to store your backups
|
||||
backup_destination="/var/backups"
|
||||
|
||||
# Folders to backup
|
||||
folders_to_backup=(
|
||||
"/etc"
|
||||
"/var/mail"
|
||||
"/var/www"
|
||||
"/var/lib/mysql"
|
||||
"/var/spool/cron"
|
||||
"/var/lib/liveconfig"
|
||||
)
|
||||
|
||||
# Files and folders that are excluded in the tar command
|
||||
tar_excludes=(
|
||||
"nginx-php-fcgi.sock"
|
||||
)
|
||||
|
||||
# Additional tar Options
|
||||
tar_options=()
|
||||
|
||||
# How long to you want to keep your backups (in days)
|
||||
backup_retention="+7"
|
||||
|
||||
# Commands that are executed before the backup started
|
||||
# (We have to make sure the liveconfig process is not running)
|
||||
# (otherwise the databases changes while we try to save it)
|
||||
pre_commands=(
|
||||
"service liveconfig stop"
|
||||
)
|
||||
|
||||
# Commands that are executed after the backup is completed
|
||||
# (To restart the liveconfig process again, once the backup is completed)
|
||||
post_commands=(
|
||||
"service liveconfig start"
|
||||
)
|
||||
|
||||
#### Cronjob setup
|
||||
|
||||
To make sure the backup is executed automatically and recurring, we're going to add a simple cronjob:
|
||||
|
||||
`vi /etc/cron.d/backup-liveconfig`
|
||||
|
||||
#
|
||||
# cronjob to backup LiveConfig, daily at 5:00 am
|
||||
#
|
||||
|
||||
0 5 * * * root /usr/local/bin/multibackup &>/dev/null
|
||||
|
||||
#### CREDITS
|
||||
This Backup Script is a fork of https://github.com/frdmn/tar-multibackup
|
||||
Text kopiert!
|
||||
```bash
|
||||
sshs
|
||||
|
||||
102
sshs.bash
102
sshs.bash
@@ -2,62 +2,102 @@
|
||||
|
||||
KNOWN_HOSTS="$HOME/.ssh/known_hosts"
|
||||
|
||||
# Hosts extrahieren – ignoriert gehashte Zeilen
|
||||
# --- Funktion: SSH-Verbindung aufbauen ---
|
||||
connect_ssh() {
|
||||
local host="$1"
|
||||
local port="$2"
|
||||
local user="$3"
|
||||
|
||||
if [[ -n "$user" ]]; then
|
||||
TARGET="${user}@${host}"
|
||||
else
|
||||
TARGET="$host"
|
||||
fi
|
||||
|
||||
echo -e "\n🔌 Verbinde mit: $TARGET (Port $port)"
|
||||
sleep 0.5
|
||||
exec ssh -p "$port" "$TARGET"
|
||||
}
|
||||
|
||||
# --- Wenn Parameter übergeben wurde, direkt nutzen ---
|
||||
if [[ -n "$1" ]]; then
|
||||
INPUT="$1"
|
||||
|
||||
# user@host:port
|
||||
if [[ "$INPUT" =~ ^([^@]+)@([^:]+):([0-9]+)$ ]]; then
|
||||
SSH_USER="${BASH_REMATCH[1]}"
|
||||
HOST="${BASH_REMATCH[2]}"
|
||||
PORT="${BASH_REMATCH[3]}"
|
||||
|
||||
# user@host
|
||||
elif [[ "$INPUT" =~ ^([^@]+)@(.+)$ ]]; then
|
||||
SSH_USER="${BASH_REMATCH[1]}"
|
||||
HOST="${BASH_REMATCH[2]}"
|
||||
PORT="22"
|
||||
|
||||
# host:port
|
||||
elif [[ "$INPUT" =~ ^([^:]+):([0-9]+)$ ]]; then
|
||||
SSH_USER=""
|
||||
HOST="${BASH_REMATCH[1]}"
|
||||
PORT="${BASH_REMATCH[2]}"
|
||||
|
||||
# host
|
||||
else
|
||||
SSH_USER=""
|
||||
HOST="$INPUT"
|
||||
PORT="22"
|
||||
fi
|
||||
|
||||
connect_ssh "$HOST" "$PORT" "$SSH_USER"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# --- Hosts aus known_hosts im Format host:port extrahieren ---
|
||||
HOSTS=$(awk '
|
||||
{
|
||||
split($1, hosts, ",");
|
||||
for (i in hosts)
|
||||
if (substr(hosts[i], 1, 1) != "|")
|
||||
print hosts[i];
|
||||
for (i in hosts) {
|
||||
if (substr(hosts[i], 1, 1) != "|") {
|
||||
if (match(hosts[i], /^\[.*\]:[0-9]+$/)) {
|
||||
hostport = hosts[i]
|
||||
gsub(/^\[/, "", hostport)
|
||||
gsub(/\]/, "", hostport)
|
||||
print hostport
|
||||
} else {
|
||||
print hosts[i] ":22"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
' "$KNOWN_HOSTS" | sort -u)
|
||||
|
||||
# Eingabe mit fzf (Suchfenster)
|
||||
INPUT=$( (echo "$HOSTS") | fzf --prompt="SSH zu Host (neu eingeben möglich): " --height=100% --layout=reverse --border --print-query)
|
||||
|
||||
# fzf gibt als erstes die Suchanfrage aus, dann die Auswahl
|
||||
# split in erste Zeile (Query) und zweite Zeile (Auswahl)
|
||||
# --- Auswahl via fzf ---
|
||||
INPUT=$( (echo "$HOSTS") | fzf --prompt="SSH zu Host (z. B. host:port oder frei eingeben): " --height=100% --layout=reverse --border --print-query)
|
||||
readarray -t LINES <<< "$INPUT"
|
||||
QUERY="${LINES[0]}"
|
||||
SELECTED="${LINES[1]}"
|
||||
|
||||
# Wenn nichts eingegeben und nichts ausgewählt → Abbruch
|
||||
if [[ -z "$QUERY" && -z "$SELECTED" ]]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Wenn etwas ausgewählt wurde, nutze das, sonst das Query (freie Eingabe)
|
||||
if [[ -n "$SELECTED" ]]; then
|
||||
TARGET_RAW="$SELECTED"
|
||||
else
|
||||
TARGET_RAW="$QUERY"
|
||||
fi
|
||||
|
||||
# Port & Host extrahieren aus Known Hosts Format [host]:port oder normal
|
||||
if [[ "$TARGET_RAW" =~ ^\[([^\]]+)\]:(.+)$ ]]; then
|
||||
# host:port erkennen
|
||||
if [[ "$TARGET_RAW" =~ ^([^:]+):([0-9]+)$ ]]; then
|
||||
HOST="${BASH_REMATCH[1]}"
|
||||
PORT="${BASH_REMATCH[2]}"
|
||||
else
|
||||
HOST="$TARGET_RAW"
|
||||
PORT=""
|
||||
PORT="22"
|
||||
fi
|
||||
|
||||
# Port-Abfrage nur wenn kein Port aus known_hosts gesetzt
|
||||
if [[ -z "$PORT" ]]; then
|
||||
read -rp "🔢 Port (leer für 22): " PORT
|
||||
PORT=${PORT:-22}
|
||||
fi
|
||||
|
||||
# User-Abfrage
|
||||
# Benutzername abfragen
|
||||
read -rp "👤 Benutzername (leer für aktuellen User: $USER): " SSH_USER
|
||||
[ -z "$SSH_USER" ] && SSH_USER=""
|
||||
|
||||
if [[ -n "$SSH_USER" ]]; then
|
||||
TARGET="${SSH_USER}@${HOST}"
|
||||
else
|
||||
TARGET="$HOST"
|
||||
fi
|
||||
|
||||
echo -e "\n🔌 Verbinde mit: $TARGET (Port $PORT)"
|
||||
sleep 0.5
|
||||
|
||||
exec ssh -p "$PORT" "$TARGET"
|
||||
connect_ssh "$HOST" "$PORT" "$SSH_USER"
|
||||
|
||||
Reference in New Issue
Block a user