Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

On Ubuntu, how do you get all timestamps with file backup?

I made a better recorder function, same all else.
Bash:
function writeXE() {
    if cantUseXE $1; then
        return;
    fi
    # Write lines in order what, created at, last modified, last accessed, last changed, permissions, owner, group, inode.
    echo $1 >> $2;
    file=($(stat -c "%w %y %x %z %a %U %G %i" $1));
    echo "${file[0]} ${file[1]} ${file[2]}" >> $2;
    echo "${file[3]} ${file[4]} ${file[5]}" >> $2;
    echo "${file[6]} ${file[7]} ${file[8]}" >> $2;
    echo "${file[9]} ${file[10]} ${file[11]}" >> $2;
    echo "${file[12]}" >> $2;
    echo "${file[13]}" >> $2;
    echo "${file[14]}" >> $2;
    echo "${file[15]}" >> $2;
    echo "" >> $2;
    #X E.
}
#X E.
About same speed. Should I run as root or what? Bash is at 1/100th what I know this machine can do. X E.
 
I guess I can start with setter. Here is what I have so far but puting things in array does not work.
Bash:
#!/bin/bash

#12/28/2023, 9:26PM, start, X E. 9:55PM, done for today. X E. 9:59PM, like done for today, X E.
# Specify the file path
file_path="$(pwd)/propsXE.txt"
#12/30/2023, 4:19PM, started work today.
echo "input directory inside origin to restore from"
read origin
# Check if the file exists
if [ -f "$file_path" ]; then
    # Open the file for reading
    exec 3< "$file_path"
    index=0;
    res=();
    # Read each line of the file
    while IFS= read -r line <&3; do
        if [ $index -eq 0 ]; then
            res+=("$("$line" | sed "0,/$origin/{s//$origin/}")")
        else
            if [ $index -eq 9 ]; then
                index=$index;
            else
                res+=($line);
            fi
        fi
        # Process the line
        index=$((index+1));
        if [ $index -eq 10 ]; then
            echo $res;
            index=0;
            res=();
            exec 3<&-
            exit 1;
       fi
    done
  # Close the file
  exec 3<&-
else
  echo "File not found: $file_path"
fi
#X E.
Trying for all in an array now but I can't seem to get it to work. I am trying for $origin removed from start. Can you help? X E.
 
I guess I can start with setter. Here is what I have so far but puting things in array does not work.
Bash:
#!/bin/bash

#12/28/2023, 9:26PM, start, X E. 9:55PM, done for today. X E. 9:59PM, like done for today, X E.
# Specify the file path
file_path="$(pwd)/propsXE.txt"
#12/30/2023, 4:19PM, started work today.
echo "input directory inside origin to restore from"
read origin
# Check if the file exists
if [ -f "$file_path" ]; then
    # Open the file for reading
    exec 3< "$file_path"
    index=0;
    res=();
    # Read each line of the file
    while IFS= read -r line <&3; do
        if [ $index -eq 0 ]; then
            res+=("$("$line" | sed "0,/$origin/{s//$origin/}")")
        else
            if [ $index -eq 9 ]; then
                index=$index;
            else
                res+=($line);
            fi
        fi
        # Process the line
        index=$((index+1));
        if [ $index -eq 10 ]; then
            echo $res;
            index=0;
            res=();
            exec 3<&-
            exit 1;
       fi
    done
  # Close the file
  exec 3<&-
else
  echo "File not found: $file_path"
fi
#X E.
Trying for all in an array now but I can't seem to get it to work. I am trying for $origin removed from start. Can you help? X E.
Let's take a step back. What is it that you are storing in arrays?
 
Lines in order what is path relative, created at, last modified, last accessed, last changed, permissions, owner, group. These from that file I created. X E.
 
Lines in order what is path relative, created at, last modified, last accessed, last changed, permissions, owner, group. These from that file I created. X E.
Do you need to store that in an array, or could you just aggregate into a variable from calling specific commands? In other words, would calling the commands to get that info inline and appending the data to a variable like you would with a string, be more beneficial than having to store in array?
 
I was thinking of like just a variable for path to use and with each iteration do a command. Making it all one string seems destructive to like my purposes, I saved it on each line to get data one by one. X E.
 
It is harder to get data and getting straight array again would destroy paths with spaces. Here is what I have now and it works.
Bash:
#!/bin/bash

#12/28/2023, 9:26PM, start, X E. 9:55PM, done for today. X E. 9:59PM, like done for today, X E.
# Specify the file path
file_path="$(pwd)/propsXE.txt"
#12/30/2023, 4:19PM, started work today.
echo "input directory inside origin to restore from"
read origin
if [ -z "$origin" ]; then
    origin=""
fi
# Check if the file exists
if [ -f "$file_path" ]; then
    # Open the file for reading
    exec 3< "$file_path"
    index=0;
    result="";
    res=();
    # Read each line of the file
    while IFS= read -r line <&3; do
        if [ $index -eq 0 ]; then
            if [[ -n $origin ]]; then
                result="${line##$origin}"
            else
                result=$line;
            fi
        elif [ $index -ne 9 ]; then
            if [ $index -eq 1 ]; then
                echo "created"
            elif [ $index -eq 2 ]; then
                echo "last modified"
            elif [ $index -eq 3 ]; then
                echo "last access"
            elif [ $index -eq 4 ]; then
                echo "change"
            elif [ $index -eq 5 ]; then
                echo "permissions"
            elif [ $index -eq 6 ]; then
                echo "owner"
            elif [ $index -eq 7 ]; then
                echo "group"
            else
                echo "inode $line"
            fi
        fi
        # Process the line
        index=$((index+1));
        if [ $index -eq 10 ]; then
            echo $result;
            index=0;
            res=();
       fi
    done
  # Close the file
  exec 3<&-
else
  echo "File not found: $file_path"
fi
#X E.
Anyway, now I just need to map to like my preferred destination. For this I will be testing on a USB. X E.
 
It is harder to get data and getting straight array again would destroy paths with spaces. Here is what I have now and it works.
Bash:
#!/bin/bash

#12/28/2023, 9:26PM, start, X E. 9:55PM, done for today. X E. 9:59PM, like done for today, X E.
# Specify the file path
file_path="$(pwd)/propsXE.txt"
#12/30/2023, 4:19PM, started work today.
echo "input directory inside origin to restore from"
read origin
if [ -z "$origin" ]; then
    origin=""
fi
# Check if the file exists
if [ -f "$file_path" ]; then
    # Open the file for reading
    exec 3< "$file_path"
    index=0;
    result="";
    res=();
    # Read each line of the file
    while IFS= read -r line <&3; do
        if [ $index -eq 0 ]; then
            if [[ -n $origin ]]; then
                result="${line##$origin}"
            else
                result=$line;
            fi
        elif [ $index -ne 9 ]; then
            if [ $index -eq 1 ]; then
                echo "created"
            elif [ $index -eq 2 ]; then
                echo "last modified"
            elif [ $index -eq 3 ]; then
                echo "last access"
            elif [ $index -eq 4 ]; then
                echo "change"
            elif [ $index -eq 5 ]; then
                echo "permissions"
            elif [ $index -eq 6 ]; then
                echo "owner"
            elif [ $index -eq 7 ]; then
                echo "group"
            else
                echo "inode $line"
            fi
        fi
        # Process the line
        index=$((index+1));
        if [ $index -eq 10 ]; then
            echo $result;
            index=0;
            res=();
       fi
    done
  # Close the file
  exec 3<&-
else
  echo "File not found: $file_path"
fi
#X E.
Anyway, now I just need to map to like my preferred destination. For this I will be testing on a USB. X E.
how's the speed?
 
Okay, now I have a mapping mechanism. Now just how to set stuff correctly.
Bash:
#!/bin/bash

#12/28/2023, 9:26PM, start, X E. 9:55PM, done for today. X E. 9:59PM, like done for today, X E.
# Specify the file path
file_path="$(pwd)/propsXE.txt"
#12/30/2023, 4:19PM, started work today.
echo "input directory inside origin to restore from"
read origin
if [ -z "$origin" ]; then
    origin=""
fi
echo "input directory to move to"
read dest
if [ -z "$dest" ]; then
    dest=""
fi
if [[ "$dest" != */ ]]; then
    dest="$dest/"
fi
# Check if the file exists
if [ -f "$file_path" ]; then
    # Open the file for reading
    exec 3< "$file_path"
    index=0;
    result="";
    res=();
    # Read each line of the file
    while IFS= read -r line <&3; do
        if [ $index -eq 0 ]; then
            if [[ -n $origin ]]; then
                result="${line##$origin}"
            else
                result=$line;
            fi
            if [[ "$result" == /* ]]; then
                result="$dest${result##/}";
            else
                result="$dest$result";
            fi
        elif [ $index -ne 9 ]; then
            if [ $index -eq 1 ]; then
                echo "created"
            elif [ $index -eq 2 ]; then
                echo "last modified"
            elif [ $index -eq 3 ]; then
                echo "last access"
            elif [ $index -eq 4 ]; then
                echo "change"
            elif [ $index -eq 5 ]; then
                echo "permissions"
            elif [ $index -eq 6 ]; then
                echo "owner"
            elif [ $index -eq 7 ]; then
                echo "group"
            else
                echo "inode $line"
            fi
        fi
        # Process the line
        index=$((index+1));
        if [ $index -eq 10 ]; then
            echo $result;
            index=0;
            res=();
       fi
    done
  # Close the file
  exec 3<&-
else
  echo "File not found: $file_path"
fi
#X E.
I assume use touch but I do not know how. With this one it is fast, like a couple of seconds and less than a minute to display everything. X E.
 
Okay, now I have a mapping mechanism. Now just how to set stuff correctly.
Bash:
#!/bin/bash

#12/28/2023, 9:26PM, start, X E. 9:55PM, done for today. X E. 9:59PM, like done for today, X E.
# Specify the file path
file_path="$(pwd)/propsXE.txt"
#12/30/2023, 4:19PM, started work today.
echo "input directory inside origin to restore from"
read origin
if [ -z "$origin" ]; then
    origin=""
fi
echo "input directory to move to"
read dest
if [ -z "$dest" ]; then
    dest=""
fi
if [[ "$dest" != */ ]]; then
    dest="$dest/"
fi
# Check if the file exists
if [ -f "$file_path" ]; then
    # Open the file for reading
    exec 3< "$file_path"
    index=0;
    result="";
    res=();
    # Read each line of the file
    while IFS= read -r line <&3; do
        if [ $index -eq 0 ]; then
            if [[ -n $origin ]]; then
                result="${line##$origin}"
            else
                result=$line;
            fi
            if [[ "$result" == /* ]]; then
                result="$dest${result##/}";
            else
                result="$dest$result";
            fi
        elif [ $index -ne 9 ]; then
            if [ $index -eq 1 ]; then
                echo "created"
            elif [ $index -eq 2 ]; then
                echo "last modified"
            elif [ $index -eq 3 ]; then
                echo "last access"
            elif [ $index -eq 4 ]; then
                echo "change"
            elif [ $index -eq 5 ]; then
                echo "permissions"
            elif [ $index -eq 6 ]; then
                echo "owner"
            elif [ $index -eq 7 ]; then
                echo "group"
            else
                echo "inode $line"
            fi
        fi
        # Process the line
        index=$((index+1));
        if [ $index -eq 10 ]; then
            echo $result;
            index=0;
            res=();
       fi
    done
  # Close the file
  exec 3<&-
else
  echo "File not found: $file_path"
fi
#X E.
I assume use touch but I do not know how. With this one it is fast, like a couple of seconds and less than a minute to display everything. X E.
If you recall my commentary a few posts ago... if you don't have touch installed... it will error out
 
Like my new code which has comments for what I think should set what. It checks for touch install.
Bash:
#!/bin/bash
if ! command -v touch &> /dev/null; then
    echo "The 'touch' command is not installed. Exiting..."
    exit 1
fi
#12/28/2023, 9:26PM, start, X E. 9:55PM, done for today. X E. 9:59PM, like done for today, X E.
# Specify the file path
file_path="$(pwd)/propsXE.txt"
#12/30/2023, 4:19PM, started work today.
echo "input directory inside origin to restore from"
read origin
if [ -z "$origin" ]; then
    origin=""
fi
echo "input directory to move to"
read dest
if [ -z "$dest" ]; then
    dest=""
fi
if [[ "$dest" != */ ]]; then
    dest="$dest/"
fi
# Check if the file exists
if [ -f "$file_path" ]; then
    # Open the file for reading
    exec 3< "$file_path"
    index=0;
    result="";
    change="";
    access="";
    # Read each line of the file
    while IFS= read -r line <&3; do
        if [ $index -eq 0 ]; then
            if [[ -n $origin ]]; then
                result="${line##$origin}"
            else
                result=$line;
            fi
            if [[ "$result" == /* ]]; then
                result="$dest${result##/}";
            else
                result="$dest$result";
            fi
            if [ ! -f $result ]; then
                echo "Error $result does not exist."
            fi
        elif [ $index -ne 9 ] && [ -f $result ]; then
            if [ $index -eq 1 ]; then
                #touch -c -t "$line" "$result"
                echo "created"
            elif [ $index -eq 2 ]; then
                #touch -m -t "$line" "$result"
                echo "last modified"
            elif [ $index -eq 3 ]; then
                #echo "last access"
                access=$line
                #touch -a -t "$line" "$result"
            elif [ $index -eq 4 ]; then
                #echo "change"
                change=$line;
            elif [ $index -eq 5 ]; then
                echo "permissions"
                #chmod $line $result
            elif [ $index -eq 6 ]; then
                echo "owner"
                #chown $line $result
            elif [ $index -eq 7 ]; then
                echo "group"
                #chgrp $line $result
            else
                echo "inode $line"
            fi
        fi
        # Process the line
        index=$((index+1));
        if [ $index -eq 10 ]; then
            #touch -a -t "$change" "$result"
            #touch -a -t "$access" "$result"
            index=0;
       fi
    done
  # Close the file
  exec 3<&-
else
  echo "File not found: $file_path"
fi
#X E.
X E.
 
Like my new code which has comments for what I think should set what. It checks for touch install.
Bash:
#!/bin/bash
if ! command -v touch &> /dev/null; then
    echo "The 'touch' command is not installed. Exiting..."
    exit 1
fi
#12/28/2023, 9:26PM, start, X E. 9:55PM, done for today. X E. 9:59PM, like done for today, X E.
# Specify the file path
file_path="$(pwd)/propsXE.txt"
#12/30/2023, 4:19PM, started work today.
echo "input directory inside origin to restore from"
read origin
if [ -z "$origin" ]; then
    origin=""
fi
echo "input directory to move to"
read dest
if [ -z "$dest" ]; then
    dest=""
fi
if [[ "$dest" != */ ]]; then
    dest="$dest/"
fi
# Check if the file exists
if [ -f "$file_path" ]; then
    # Open the file for reading
    exec 3< "$file_path"
    index=0;
    result="";
    change="";
    access="";
    # Read each line of the file
    while IFS= read -r line <&3; do
        if [ $index -eq 0 ]; then
            if [[ -n $origin ]]; then
                result="${line##$origin}"
            else
                result=$line;
            fi
            if [[ "$result" == /* ]]; then
                result="$dest${result##/}";
            else
                result="$dest$result";
            fi
            if [ ! -f $result ]; then
                echo "Error $result does not exist."
            fi
        elif [ $index -ne 9 ] && [ -f $result ]; then
            if [ $index -eq 1 ]; then
                #touch -c -t "$line" "$result"
                echo "created"
            elif [ $index -eq 2 ]; then
                #touch -m -t "$line" "$result"
                echo "last modified"
            elif [ $index -eq 3 ]; then
                #echo "last access"
                access=$line
                #touch -a -t "$line" "$result"
            elif [ $index -eq 4 ]; then
                #echo "change"
                change=$line;
            elif [ $index -eq 5 ]; then
                echo "permissions"
                #chmod $line $result
            elif [ $index -eq 6 ]; then
                echo "owner"
                #chown $line $result
            elif [ $index -eq 7 ]; then
                echo "group"
                #chgrp $line $result
            else
                echo "inode $line"
            fi
        fi
        # Process the line
        index=$((index+1));
        if [ $index -eq 10 ]; then
            #touch -a -t "$change" "$result"
            #touch -a -t "$access" "$result"
            index=0;
       fi
    done
  # Close the file
  exec 3<&-
else
  echo "File not found: $file_path"
fi
#X E.
X E.
walk me through your thought process here
 
Did I really just do all that to find I need something else to set created at? I am unsure. Anyway, thought process: start by checking for touch, get inputs, start reading file. In reading of file start by getting path to use as $result. Each line after except last one holds something to use. That commented out thing near each one is how I would do it. That ending with 10 thing changes changed at then accessed at. $result should set itself again and again. each time going through checking if files exist and if they do then set properties. Times are in 2023-12-30 16:07:33.079360339 -0500 format and I am unsure if I need to format them or not. That is basically all. X E.
 
Did I really just do all that to find I need something else to set created at? I am unsure. Anyway, thought process: start by checking for touch, get inputs, start reading file. In reading of file start by getting path to use as $result. Each line after except last one holds something to use. That commented out thing near each one is how I would do it. That ending with 10 thing changes changed at then accessed at. $result should set itself again and again. each time going through checking if files exist and if they do then set properties. Times are in 2023-12-30 16:07:33.079360339 -0500 format and I am unsure if I need to format them or not. That is basically all. X E.
Ok, what files are you reading in?
 
I would be with a complete backup read to, reading in a file made by like my previous script, and I would want editors like touch so I can set timestamps of files from backup of properties file. I am trying to restore created at from a backup. X E.
 
I would be with a complete backup read to, reading in a file made by like my previous script, and I would want editors like touch so I can set timestamps of files from backup of properties file. I am trying to restore created at from a backup. X E.
I do hope you realize that this step of using a editor to edit timestamp is also adding to the decline in performance...if you're really worried about performance, you would be better off echoing a formatted string to the file itself... something like

echo "$formatted data" >> manifest.log
 
Back
Top Bottom