HiveBrain v1.2.0
Get Started
← Back to all entries
patternbashMinor

Search and merge split-archive backup parts

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
searchmergesplitbackupandpartsarchive

Problem

I wrote some code which searches parts of a split-archive backup (it's an Android backup, I used flashfire to backup) and rejoins them. As you can easily see, I've achieved this by if statements and the maximal number of archive parts is hardcoded (although NOT limited to 4 parts).

As I'm very new to "programming" (if you like to name it by that) I'm quite unsure how this could be done better, especially the part where the archive parts are searched and joined. How can I let the script search, with like a wildcard or something, the backup parts by itself without having to write an if statement for every single part there is?

```
PRINT_LINE3
echo "To create a DB make sure that you have:"
echo
echo "[REQUIRED] system.tlz4 and all system.*.tlz4 "
echo "[OPTIONAL] data.tlz4 and all data.*.tlz4"
echo "in $BASE_DIR"
echo

local READY=0
while [[ $READY -eq 0 ]]; do
echo
echo "Please fill a path for your system.tlz4 file"
echo "Potential candidates:"
echo "=-=-=-=-=-=-=-=-="
find ./base_drops -type f -iname "system.tlz4"
echo "=-=-=-=-=-=-=-=-="
read -p "system.tlz4: " SYSTEM_BAK

echo
echo "Please fill a path for your data.tlz4 file. Leave empty if none."
echo "Potential candidates:"
echo "=-=-=-=-=-=-=-=-="
find ./base_drops -type f -iname "data.tlz4"
echo "=-=-=-=-=-=-=-=-="
read -p "data.tlz4: " DATA_BAK

if [[ -f "$SYSTEM_BAK" ]] && ([[ -z "$DATA_BAK" || -f "$DATA_BAK" ]]); then
READY=1
else
echo "Some of your files do not exist"
PRESS_ENTER
fi
done
# creates db dir plus subdirs if needed
PRINT_LINE3
read -p "Name your database: " NAME
PRINT_LINE1
export ACTIVE_DB="./DB_$NAME"
export LIST_DIR="$ACTIVE_DB/lists"
export LOGS_DIR="$ACTIVE_DB/logs"
export ROM_DIR="$ACTIVE_DB/rom"
export SYS_DIR="$ACTIVE_DB/rom/system"
if [[ ! -z $DATA_BAK ]] ; then
export DATA_DIR="$ACTIVE_DB/rom/data"
fi
echo "Creating $ACTIVE_DB, please wait..."
mkdir -p "$ACTIVE_DB"
mkdir -p "$L

Solution

You could eliminate the tedious if-else chains using wildcard expansion:

cat "$SYSTEM_BAK" ./base_drops/system.000[234].tlz4 > "$ACTIVE_DB/rom/system/system.tar.lz4"


Or possibly even the simpler but less strict pattern:

cat "$SYSTEM_BAK" ./base_drops/system.*.tlz4 > "$ACTIVE_DB/rom/system/system.tar.lz4"


If you want to print the count of the number of parts, then you can store the filenames in an array:

parts=("$SYSTEM_BAK" ./base_drops/system.*.tlz4)


And then print the message like this:

echo "(${#parts[@]} parts) -> ${parts[@]}"


You can do similarly for the data files.

Btw, it seems a bit strange to prompt the user for SYSTEM_BAK, but for the parts use a fixed path. I'm wondering if the program really works as intended.

If you drop the prompting (as you mentioned in a comment), note that you cannot simplify the array creation to this:

parts=(./base_drops/system*.tlz4)


This won't order the system.tlz4 and system.XXXX.tlz4 files the way you want, as it will put system.tlz4 at the end. So you need to write as:

parts=(./base_drops/system.tlz4 ./base_drops/system.*.tlz4)

Code Snippets

cat "$SYSTEM_BAK" ./base_drops/system.000[234].tlz4 > "$ACTIVE_DB/rom/system/system.tar.lz4"
cat "$SYSTEM_BAK" ./base_drops/system.*.tlz4 > "$ACTIVE_DB/rom/system/system.tar.lz4"
parts=("$SYSTEM_BAK" ./base_drops/system.*.tlz4)
echo "(${#parts[@]} parts) -> ${parts[@]}"
parts=(./base_drops/system*.tlz4)

Context

StackExchange Code Review Q#121016, answer score: 4

Revisions (0)

No revisions yet.