Skip to content
Snippets Groups Projects
Commit bfd5e79c authored by Axel Hahn's avatar Axel Hahn
Browse files

add updateusb script

parent a153630f
No related branches found
No related tags found
1 merge request!143Add script to handle a USB repair stick
## 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
#!/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.
Finish editing this message first!
Please register or to comment