0

I'm strugling with a script to backup config files from root folder that copies the file inside a folder in myuser directory, reproducing the original path structure, adds the file to a git repo and commits changes. The script take the path of config file to be backed up as argument:

BU.sh /etc/apt/sources.list

Apparently the first run of program work correctly on a freshly cloned repo with only README.md, but, in the following runs, the git commands seems to have no effects.

The script is this(with some debug info added):

#! /bin/bash
#
set -x -e -u
export GIT_DIR=/home/myuser/config-BU/.git
export GIT_WORK_TREE=/home/myuser/
        #echo "file NOT in USER DIR, is NOT symlink";
        ls -l config-BU
        mkdir -p /home/myuser/config-BU/"${1%/*}";
        ls -l config-BU
        sudo install -o myuser -g myuser -m 660 "$1" "/home/myuser/config-BU$(realpath $1)";
        ls -l config-BU
        sudo -k;
        git status
        git ls-files
        git add "/home/myuser/config-BU$(realpath $1)";
        git ls-files
        git commit -am "adding file inside BU autom: file NOT in user folder" -m "$(realpath $1)";
        git log

Two runs of the command result in:

MYPROMPT>:~$ ./cp2bu2_badpart.sh /var/log/faillog
+ export GIT_DIR=/home/myuser/config-BU/.git
+ GIT_DIR=/home/myuser/config-BU/.git
+ export GIT_WORK_TREE=/home/myuser/
+ GIT_WORK_TREE=/home/myuser/
+ ls -l config-BU
total 4
-rw-r--r-- 1 myuser myuser 27 Jul  7 19:40 README.md
+ mkdir -p /home/myuser/config-BU//var/log
+ ls -l config-BU
total 8
-rw-r--r-- 1 myuser myuser   27 Jul  7 19:40 README.md
drwxr-xr-x 3 myuser myuser 4096 Jul  7 22:25 var
++ realpath /var/log/faillog
+ sudo install -o myuser -g myuser -m 660 /var/log/faillog /home/myuser/config-BU/var/log/faillog
[sudo] password for myuser:
+ ls -l config-BU
total 8
-rw-r--r-- 1 myuser myuser   27 Jul  7 19:40 README.md
drwxr-xr-x 3 myuser myuser 4096 Jul  7 22:25 var
+ sudo -k
+ git status
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
deleted:    README.md

Untracked files:
  (use "git add <file>..." to include in what will be committed)

==LIST OF FILES IN MY HOME DIR==
no changes added to commit (use "git add" and/or "git commit -a")
+ git ls-files
README.md
++ realpath /var/log/faillog
+ git add /home/myuser/config-BU/var/log/faillog
+ git ls-files
README.md
++ realpath /var/log/faillog
+ git commit -am 'adding file inside BU autom: file NOT in user folder' -m /var/log/faillog
[main 2487ec3] adding file inside BU autom: file NOT in user folder
 1 file changed, 2 deletions(-)
 delete mode 100644 README.md
+ git log
commit 2487ec3857d355313971f2a45ba7d318870bc219 (HEAD -> main)
Author: myuser <myemail>
Date:   Fri Jul 7 22:26:41 2023 +0200

    adding file inside BU autom: file NOT in user folder
   
    /var/log/faillog

commit 84112df2c48137f247af78497e416182f0e0c24c (origin/main, origin/HEAD)
Author: Pippetta87 <[email protected]>
Date:   Thu Oct 13 03:59:13 2022 +0200

    Create README.md

MYPROMPT>:~$ ./cp2bu2_badpart.sh /etc/X11/rgb.txt
+ export GIT_DIR=/home/myuser/config-BU/.git
+ GIT_DIR=/home/myuser/config-BU/.git
+ export GIT_WORK_TREE=/home/myuser/
+ GIT_WORK_TREE=/home/myuser/
+ ls -l config-BU
total 8
-rw-r--r-- 1 myuser myuser   27 Jul  7 19:40 README.md
drwxr-xr-x 3 myuser myuser 4096 Jul  7 22:25 var
+ mkdir -p /home/myuser/config-BU//etc/X11
+ ls -l config-BU
total 12
-rw-r--r-- 1 myuser myuser   27 Jul  7 19:40 README.md
drwxr-xr-x 3 myuser myuser 4096 Jul  7 22:27 etc
drwxr-xr-x 3 myuser myuser 4096 Jul  7 22:25 var
++ realpath /etc/X11/rgb.txt
+ sudo install -o myuser -g myuser -m 660 /etc/X11/rgb.txt /home/myuser/config-BU/etc/X11/rgb.txt
[sudo] password for myuser:
+ ls -l config-BU
total 12
-rw-r--r-- 1 myuser myuser   27 Jul  7 19:40 README.md
drwxr-xr-x 3 myuser myuser 4096 Jul  7 22:27 etc
drwxr-xr-x 3 myuser myuser 4096 Jul  7 22:25 var
+ sudo -k
+ git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)

==LIST OF FILES IN MY HOME DIR==

nothing added to commit but untracked files present (use "git add" to track)
+ git ls-files
++ realpath /etc/X11/rgb.txt
+ git add /home/myuser/config-BU/etc/X11/rgb.txt
+ git ls-files
++ realpath /etc/X11/rgb.txt
+ git commit -am 'adding file inside BU autom: file NOT in user folder' -m /etc/X11/rgb.txt
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)

==LIST OF FILES IN MY HOME DIR==

nothing added to commit but untracked files present (use "git add" to track)

As you can see the second run do not add file to git repo and last git log is not executed: the git commands after git status seems to be ignored.

Best Regards

1 Answer 1

0

You start your script with:

export GIT_DIR=/home/myuser/config-BU/.git
export GIT_WORK_TREE=/home/myuser/

Your working dir should actually be /home/myuser/config-BU.

That's why when you run git status you see:

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
deleted:    README.md

It can't find README.md in your GIT_WORK_TREE (/home/myuser/README.md), so it thinks that this file was deleted.

Then when you commit your change, you see again that it deletes README.md from the index:

git commit -am 'adding file inside BU autom: file NOT in user folder' -m /var/log/faillog
[main 2487ec3] adding file inside BU autom: file NOT in user folder
 1 file changed, 2 deletions(-)
 delete mode 100644 README.md
1
  • I'm using user directory as GIT_WORK_TREE since in the full script I just add to git repository configuration files (without copying them inside config-BU): I need to fix that part ... may just need to git clone with GIT_WORK_TREE sets!? Commented Jul 13, 2023 at 13:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .