Skip to content
Snippets Groups Projects
Commit 84be1081 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

Merge branch 'usb-repair-stick' into 'master'

Add script to handle a USB repair stick

See merge request !143
parents a153630f 1d92c9f1
No related branches found
No related tags found
1 merge request!143Add script to handle a USB repair stick
# IML BACKUP # # IML BACKUP
Backup scripts using Restic (or Duplicity). Backup scripts using Restic (or Duplicity).
Runs on Linux: CentOS, Debian, Manjaro, Ubuntu. Runs on Linux (CentOS, Debian, Manjaro, Ubuntu) and Windows (by using compiled GNU tools eg. Cygwin, MinGW, Git).
📄 Source: <https://git-repo.iml.unibe.ch/iml-open-source/iml-backup/> \ 📄 Source: <https://git-repo.iml.unibe.ch/iml-open-source/iml-backup/> \
📜 License: GNU GPL 3.0 \ 📜 License: GNU GPL 3.0 \
...@@ -13,7 +13,7 @@ Supported backup clients: ...@@ -13,7 +13,7 @@ Supported backup clients:
* Restic: <https://restic.net/> * Restic: <https://restic.net/>
* Duplicity: <https://duplicity.gitlab.io/duplicity-web/> * Duplicity: <https://duplicity.gitlab.io/duplicity-web/>
# Why # # Why
We rollout our linux systems automatically. We try not to configure third systems We rollout our linux systems automatically. We try not to configure third systems
for backup, monitoring and other general services. for backup, monitoring and other general services.
...@@ -23,7 +23,3 @@ for backup, monitoring and other general services. ...@@ -23,7 +23,3 @@ for backup, monitoring and other general services.
* No agent needed.<br><br> * No agent needed.<br><br>
* **Automatic backup of databases**: A set of database backup scripts detects exsiting locally running database services and puts a compressed dump file per database scheme to a local backup directory.<br><br> * **Automatic backup of databases**: A set of database backup scripts detects exsiting locally running database services and puts a compressed dump file per database scheme to a local backup directory.<br><br>
* We want to use a **local encryption** of all data to backup. * We want to use a **local encryption** of all data to backup.
---
See the [docs](./docs/) folder for more details.
\ No newline at end of file
## What is the USB repair stick?
The USB stick is a helper to create a repair system on a USB stick.
This is highly recommended if you have no system backup as image that can restore a complete system.
## First run
Execute as local user
```
./updateusb.sh
_________________________________________________________________________
____/
Create/ Update Repair System
____
_________________________________________________________________________/
WELCOME!
This is a helper to create a repair system on a USB stick.
With the repair system you can connect the backup repository if
your system crashed and you have no local data anymore.
💡 Hint:
This is highly recommended if you have no system backup as image
that can restore a complete system.
Do you have a system backup as image? [y/N]
```
If you have a system backup as image, you can press `y`. Your information will be stored that you don't get the same question again.
```txt
Do you have a system backup as image? [y/N] y
OK, I won't disturb you again.
Creating [logs/last_sync2usb_skip] ...
Bye.
```
If you have no system backup as image, press `N` or just return. Then it will initialize the USB repair stick which is an rsync of the local il-backup directory including settings and logs to the USB stick. The USB stick won't be formated/ deleted - all its current data will be kept.
The mounted usb drives will be shown.
```txt
USB drive found.
I expect that you added a usb drive where to sync the backup client.
Onto the stick the backup client directory will be copied.
You don't lose any data that are already there.
Select a target path (column Mounted on)
Filesystem Size Used Avail Use% Mounted on
/dev/sde1 124G 33G 92G 27% /run/media/axel/Ventoy
Enter target path:
```
You need to enter the target path where to sync the iml-backup client and press return.
Remark: I would mark it with the mouse and insert it it with the middle mouse key.
```txt
Enter target path: /run/media/axel/Ventoy
Sync target: /run/media/axel/Ventoy/linux-pc__user__axel
Please confirm - store the backup client in this path? [y/N]
```
After entering it the hostname and username will be added as final target directory. Confirm it to start the initial sync.
```txt
--- Syncing to USB stick [/run/media/axel/Ventoy/linux-pc__user__axel] ...
OK
The sync was successful.
Unmount your USB stick and put it to a safe place.
```
## Update the USB repair stick?
Execute as local user the same script again.
```txt
./updateusb.sh
```
It shows the time since last sync. If the stick is found it will immediately start the sync.
```txt
--- Last sync: 54 s ago
-rw-r--r-- 1 axel axel 56 Okt 27 11:46 logs/last_sync2usb
___
| |
|___|
/_____\
| |
| USB |
| _ |
|_/=\_|
USB drive found.
--- Syncing to USB stick [/run/media/axel/Ventoy/linux-pc__user__axel] ...
OK
The sync was successful.
Unmount your USB stick and put it to a safe place.
```
If the stick is not mounted yet it will show an error.
```
ERROR: No USB drive found.
Plug in your recovery usb stick and mount it before trying again.
Onto the stick the backup client directory will be copied.
You don't lose any data that are already there.
```
Insert and mount your stick. Then start `./updateusb.sh` again.
\ No newline at end of file
...@@ -42,6 +42,10 @@ function fetchrc(){ ...@@ -42,6 +42,10 @@ function fetchrc(){
;; ;;
"head") sColorcode="33" # yellow "head") sColorcode="33" # yellow
;; ;;
"head2") sColorcode="36" # purple
;;
"head3") sColorcode="35" # cyan
;;
"cmd") sColorcode="94" # light blue "cmd") sColorcode="94" # light blue
;; ;;
"input") sColorcode="92" # green "input") sColorcode="92" # green
...@@ -88,7 +92,7 @@ function fetchrc(){ ...@@ -88,7 +92,7 @@ function fetchrc(){
function h2(){ function h2(){
color head color head2
echo echo
echo "========== $* ==========" echo "========== $* =========="
echo echo
...@@ -96,7 +100,7 @@ function fetchrc(){ ...@@ -96,7 +100,7 @@ function fetchrc(){
} }
function h3(){ function h3(){
color head color head3
echo echo
echo "---------- $*" echo "---------- $*"
color reset color reset
......
...@@ -267,8 +267,8 @@ ...@@ -267,8 +267,8 @@
# get list of snapshots and filter the lines with a date YYYY-MM-DD # get list of snapshots and filter the lines with a date YYYY-MM-DD
_data=$( t_restoreDoShowVolumes | grep "[12][0-9][0-9][0-9]-[0-2][0-9]-[0-3][0-9]" | tail -5 ) _data=$( t_restoreDoShowVolumes | grep "[12][0-9][0-9][0-9]-[0-2][0-9]-[0-3][0-9]" | tail -5 )
echo "..." # echo "..."
echo "$_data" # echo "$_data"
_snapshotLast=$( echo "$_data" | tail -2 | head -1 | cut -f 1 -d " ") _snapshotLast=$( echo "$_data" | tail -2 | head -1 | cut -f 1 -d " ")
_snapshotNow=$( echo "$_data" | tail -1 | cut -f 1 -d " ") _snapshotNow=$( echo "$_data" | tail -1 | cut -f 1 -d " ")
......
#!/bin/bash
# ================================================================================
#
# UPDATE REPAIR USB STICK
#
# --------------------------------------------------------------------------------
# 2024-10-27 www.axel-hahn.de v1.0
# ================================================================================
cd "$( dirname "$0" )" || exit 1
. vendor/color.class.sh || exit 1
FILE_LAST_SYNC=logs/last_sync2usb
FILE_SKIP_SYNC=logs/last_sync2usb_skip
SYNC_TARGET=
typeset -i iAge=0
typeset -i iAgeH=0
typeset -i iAgeD=0
# ----------------------------------------------------------------------
# FUNCTIONS
# ----------------------------------------------------------------------
function pageheader(){
clear
color.fg yellow
cat << EOH
_________________________________________________________________________
____/
Create/ Update Repair System
____
_________________________________________________________________________/
EOH
color.reset
}
function initializeUsbStick(){
cat << EO_WELCOME
WELCOME!
This is a helper to create a repair system on a USB stick.
With the repair system you can connect the backup repository if
your system crashed and you have no local data anymore.
💡 Hint:
This is highly recommended if you have no system backup as image
that can restore a complete system.
EO_WELCOME
color.print green "Do you have a system backup as image? [y/N] "
read -r answer
if [ "$answer" = "y" ]; then
echo "OK, I won't disturb you again."
echo "Creating [$FILE_SKIP_SYNC] ..."
date > "$FILE_SKIP_SYNC"
echo "Bye."
exit 0
fi
pageheader
}
# get age of a given file and fill global vars
# iAge
# iAgeH
# iAgeD
# param string filename
function getAge(){
local tsfile; typeset -i tsfile=0
test -r "$1" && tsfile=$( date +%s -r "$1" )
iAge=$( date +%s )-$tsfile
iAgeH=$iAge/60/60
iAgeD=$iAge/60/60/24
}
function showAge(){
getAge "$1"
if [ $iAgeD -gt 10000 ]; then
echo "NEVER"
else
test $iAgeD -gt 0 && echo -n "$iAgeD days"
test $iAgeD -eq 0 && test $iAgeH -gt 0 && echo -n "$iAgeH h"
test $iAgeD -eq 0 && test $iAgeH -eq 0 && echo -n "$iAge s"
echo " ago"
fi
}
function _showUsbdrives(){
local out;
out="$( df -H | grep media)"
grep -q "." <<< "$out" && (df -H | head -1; echo "$out")
}
function _getSyncdir(){
SYNC_TARGET="$( cat "$FILE_LAST_SYNC" 2>/dev/null | grep '^SYNC_TARGET=' | cut -f 2 -d '=' )"
}
# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------
pageheader
# ----------------------------------------------------------------------
if [ -f "$FILE_SKIP_SYNC" ]; then
echo "Found the skip file [$FILE_SKIP_SYNC]."
echo "The sync to USB will be skipped."
exit 0
fi
# ----------------------------------------------------------------------
if [ ! -f "$FILE_LAST_SYNC" ]; then
initializeUsbStick
else
echo -n "--- Last sync: "
showAge "$FILE_LAST_SYNC"
ls -l "$FILE_LAST_SYNC" 2>/dev/null
echo
fi
# ----------------------------------------------------------------------
drivelist="$( _showUsbdrives )"
color.fg green
test -z "$drivelist" && color.fg red
cat <<USB
___
| |
|___|
/_____\\
| |
| USB |
| _ |
|_/=\_|
USB
color.reset
if [ -z "$drivelist" ]; then
color.echo red " ERROR: No USB drive found."
echo " Plug in your recovery usb stick and mount it before trying again."
echo " Onto the stick the backup client directory will be copied."
echo " You don't lose any data that are already there."
echo
exit 1
else
echo " USB drive found."
echo
fi
# ----------------------------------------------------------------------
_getSyncdir
if [ -z "$SYNC_TARGET" ]; then
echo "I expect that you added a usb drive where to sync the backup client."
echo " Onto the stick the backup client directory will be copied."
echo " You don't lose any data that are already there."
echo
echo "Select a target path (column Mounted on)"
echo
echo "$drivelist" | sed "s,^, ,g"
echo
color.print green "Enter target path: "
read -r mypath
if [ -z "$mypath" ]; then
color.echo red "No target path given. Exiting."
exit 1
fi
if [ ! -d "$mypath" ]; then
color.echo red "Target path [$mypath] does not exist. Exiting."
exit 1
fi
subdir="$( hostname -f )__user__${USER}"
SYNC_TARGET="$mypath/$subdir"
echo " Sync target: $SYNC_TARGET"
echo
color.print green "Please confirm - store the backup client in this path? [y/N] "
read -r store
if [ "$store" != "y" ] && [ "$store" != "Y" ]; then
color.echo red "Target was not confirmeed. Aborting."
exit 1
fi
mkdir -p "$SYNC_TARGET"
pageheader
fi
# ----------------------------------------------------------------------
if [ ! -d "$SYNC_TARGET" ]; then
color.echo red "Target path does not exist."
echo
echo "Content of $( dirname "$SYNC_TARGET" )"
ls -l "$( dirname "$SYNC_TARGET" )"
echo
color.print green "Create subdir <$( basename "$SYNC_TARGET")> here? [y/N] "
read -r answer
if [ "$answer" != "y" ] && [ "$answer" != "Y" ]; then
echo "Bye."
exit 1
fi
mkdir -p "$SYNC_TARGET" || exit 2
fi
# ----------------------------------------------------------------------
echo "--- Syncing to USB stick [$SYNC_TARGET] ..."
if rsync -ra --delete . "$SYNC_TARGET" ; then
color.echo green "OK"
echo "SYNC_TARGET=$SYNC_TARGET" > "$FILE_LAST_SYNC"
echo
echo "The sync was successful."
echo "Unmount your USB stick and put it to a safe place."
echo
else
color.echo red "FAILED :-/"
echo "Check the errors above. Then try again."
echo
exit 1
fi
exit 0
# ----------------------------------------------------------------------
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment