I want backup some config file in a git repo inside my home; earlier I've been using work-tree=/ but i don't like this solution anymore. Now I'm trying to create a script that take a given config file, copy it inside my backup repo, add it to git, and do a commit. My attempt is:

cp --parents $1 /home/myuser/config-BU/
chown myuser "/home/myuser/config-BU/$1"
#exit sudo
su myuser
git --git-dir=/home/myuser/config-BU/.git/ --work-tree=/home/myuser/ add "/home/myuser/config-BU/$1"
git --git-dir=/home/myuser/config-BU/.git/ --work-tree=/home/myuser/ commit -am "adding file inside BU autom" -m "$(realpath $1)"

I call it with:

sudo -u myuser -E bash "/home/myuser/cp2bu.sh /etc/apt/sources.list"

and got this:

error: XDG_RUNTIME_DIR is invalid or not set in the environment.
 [Line 1096] Unable to connect to the compositor. If your compositor is running, check or set the WAYLAND_DISPLAY environment variable.
[1]+  Termine 253             swayidle -w timeout 300 'swaylock -f -c 000000' timeout 600 'swaymsg "output * dpms off"' resume 'swaymsg "output * dpms on"' before-sleep 'swaylock -f -c 000000'

So the big question is: How "exit" sudo after I copied config file? Or how can I re-arrange script to avoid this problem? Should I use sudo -k? But even using the above solution (with sudo bash -c "/home/myuser/cp2bu.sh /etc/apt/sources.list") I don't have the files added into git repo,and don't see the commit in git log.

Any suggestions?

Best regards

1 Answer 1


You easily end up in quoting hell when trying to pass complex commands to something like su or sudo.

In this case I guess the easiest approach is to use sudo within the script only:

#! /bin/bash

[[ $1 =~ / ]] && mkdir -p /home/myuser/config-BU/"${1%/*}"
sudo install -o myuser -m 600 "$1" "/home/myuser/config-BU/$1"

git --git-dir=/home/myuser/config-BU/.git/ --work-tree=/home/myuser/ add "/home/myuser/config-BU/$1"
git --git-dir=/home/myuser/config-BU/.git/ --work-tree=/home/myuser/ commit -am "adding file inside BU autom" -m "$(realpath $1)"
  • Thanks for your answer. I have to check why sometimes the git part don't work: nothing added/no commit ... Commented Jun 20, 2023 at 14:15
  • @AlexanderBeerhoff Obviously when the file has not been changed. You can insert something like cmp --quiet ... && { echo "No file change; aborting"; exit 2; } before the git. Commented Jun 20, 2023 at 18:21
  • I've re-cloned the repo from github to make sure all is as clean as possible (originally there was only README.md and did no push from computer while testing script) but most of the times the git commands do not work (tested adding git log with same git-dir and work-tree, and also with git status the installed directory isn't added); what is strange is that "sometimes" the file is added and "committed". Your comment will be quite usefull at stage when Ill try to schedule something like a weekly comparison between the root-conf files and the config files in the backup! Commented Jun 22, 2023 at 17:12
  • I guess it makes sense to open a separate question about the git problem (making it an obviously pure Git question might attract the right people to answer it). Commented Jun 22, 2023 at 17:41
  • well, the script work as expected only for first commit: switched to env variables GIT_DIR and WORK_TREE, also checking not adding symlink. Very strange! Commented Jul 1, 2023 at 15:34

You must log in to answer this question.

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