4
\$\begingroup\$

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 "$LIST_DIR"
mkdir -p "$LOGS_DIR"
mkdir -p "$SYS_DIR"
if [[ ! -z "$DATA_BAK" ]] ; then  
    mkdir -p "$DATA_DIR"
fi
# SYSTEM
# find backup parts
echo "Merging: "
echo -n "System partition "
if [[ -f "./base_drops/system.0004.tlz4" ]] ; then
    export SYSTEM_BAK4="./base_drops/system.0004.tlz4"
    export SYSTEM_BAK3="./base_drops/system.0003.tlz4"
    export SYSTEM_BAK2="./base_drops/system.0002.tlz4" 
    cat "$SYSTEM_BAK" "$SYSTEM_BAK2" "$SYSTEM_BAK3" "$SYSTEM_BAK4" > "$ACTIVE_DB/rom/system/system.tar.lz4"
    echo "(4 parts) -> $SYSTEM_BAK $SYSTEM_BAK2 $SYSTEM_BAK3 $SYSTEM_BAK4"    
else
    if [[ -f "./base_drops/system.0003.tlz4" ]] ; then
        export SYSTEM_BAK3="./base_drops/system.0003.tlz4"
        export SYSTEM_BAK2="./base_drops/system.0002.tlz4"  
        cat "$SYSTEM_BAK" "$SYSTEM_BAK2" "$SYSTEM_BAK3" > "$ACTIVE_DB/rom/system/system.tar.lz4"
        echo "(3 parts) -> $SYSTEM_BAK $SYSTEM_BAK2 $SYSTEM_BAK3"    
    else
        if [[ -f "./base_drops/system.0002.tlz4" ]] ; then
            export SYSTEM_BAK2="./base_drops/system.0002.tlz4"  
            cat "$SYSTEM_BAK" "$SYSTEM_BAK2" > "$ACTIVE_DB/rom/system/system.tar.lz4"
            echo "(2 parts) -> $SYSTEM_BAK $SYSTEM_BAK2"    
        fi
    fi
fi    
# /DATA-Partition backup
if [[ ! -z $DATA_BAK ]] ; then
    echo -n  "Data partition "
    if [[ -f "./base_drops/data.0004.tlz4" ]] ; then       # check for backup split-archive parts
        export DATA_BAK4="./base_drops/data.0004.tlz4"
        export DATA_BAK3="./base_drops/data.0003.tlz4"
        export DATA_BAK2="./base_drops/data.0002.tlz4"
        cat "$DATA_BAK" "$DATA_BAK2" "$DATA_BAK3" "$DATA_BAK4" > "$ACTIVE_DB/rom/data/data.tar.lz4"        # join into one archive
        echo "(4 parts) -> $DATA_BAK $DATA_BAK2 $DATA_BAK3 $DATA_BAK4"    
    else 
        if [[ -f "./base_drops/data.0003.tlz4" ]] ; then
            export DATA_BAK3="./base_drops/data.0003.tlz4"
            export DATA_BAK2="./base_drops/data.0002.tlz4"
            cat "$DATA_BAK" "$DATA_BAK2" "$DATA_BAK3" > "$ACTIVE_DB/rom/data/data.tar.lz4"
            echo "(3 parts) -> $DATA_BAK $DATA_BAK2 $DATA_BAK3"    
        else
            if [[ -f "./base_drops/data.0002.tlz4" ]] ; then
                export DATA_BAK2="./base_drops/data.0002.tlz4"
                cat "$DATA_BAK" "$DATA_BAK2" > "$ACTIVE_DB/rom/data/data.tar.lz4"
                echo "(2 parts) -> $DATA_BAK $DATA_BAK2"    
            fi
        fi
    fi
fi
\$\endgroup\$
  • 5
    \$\begingroup\$ Welcome to Code Review! I have rolled back the last edit. Please see what you may and may not do after receiving answers. Basically, don't add new code after you get answers, even if it's the revised code: if you want to get your updated code reviewed, make an iterative review. Link contains the details. \$\endgroup\$ – Pimgd Feb 25 '16 at 13:40
  • 3
    \$\begingroup\$ if you liked Janos' answer and thought it was a good review of your code , then please upvote the answer, I will upvote your question and that should almost give you enough Rep to upvote. you need 15▲ \$\endgroup\$ – Malachi Feb 25 '16 at 13:43
  • \$\begingroup\$ Gotta hand out them points \$\endgroup\$ – Pimgd Feb 25 '16 at 13:45
  • 1
    \$\begingroup\$ @Pimgd i did not know this, will stick to the rules by now. sorry for that, thanks for editing out the "illegal" edits made by me and pointing this out to me. Also, thanks for the upvote (@Malachi) \$\endgroup\$ – chris Feb 25 '16 at 13:51
4
\$\begingroup\$

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)
\$\endgroup\$
  • \$\begingroup\$ Now that i think about it, prompting the user really is unnecessary here, copied it over from the other options (dir based and stock *.img based where this makes sense). Nonetheless it works as intended. I'm having a look at eliminating the if chains tomorrow. One question to your suggestion, the first part of the backup is named system.tlz4 while the following are named system.0002.tlz4 and so on. Would your suggestion work with this naming pattern or would i have to rename the first part of the backup? \$\endgroup\$ – chris Feb 24 '16 at 23:27
  • \$\begingroup\$ Works as expected, thanks a lot. Had to change $SYSTEM_BAKand $DATA_BAK to ./base_drops/system.tlz4 because they were "defined" from the user prompting (sry for my english, it's mostly german school english). While testrunning i found it shows an incorrect part count System partition (24 parts) -> ./base_drops/system.tlz4 ./base_drops/system.0002.tlz4 Data partition (22 parts) -> ./base_drops/data.tlz4 ./base_drops/data.0002.tlz4 ./base_drops/data.0003.tlz4 is there any way to adress that? \$\endgroup\$ – chris Feb 25 '16 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.