Search code examples
linuxgitversioningsymlink

Versioning linux folder with symlinks and them content


My use-case is versioning application changes on server because sometimes I need to rollback previous version before update, or before configuration changes.

This is my list of content:

dasper@debian:/usr/share/otrs$
drwxr-xr-x   6 root root   4096 Sep 18 16:56 .
drwxr-xr-x 109 root root   4096 Sep 18 13:13 ..
lrwxrwxrwx   1 root root     21 May  9 11:06 ARCHIVE -> /var/lib/otrs/ARCHIVE
drwxr-xr-x   4 root root   4096 Sep 11 15:53 bin
-rwx------   1 root root 212450 Sep 18 16:06 .etckeeper
-rw-r--r--   1 root root   1125 Jan 11  2019 .fetchmailrc.dist
drwx------   8 root root   4096 Sep 18 16:06 .git
-rw-r--r--   1 root root    932 Sep 18 15:40 .gitignore
drwxr-xr-x   9 root root   4096 Sep 11 15:53 Kernel
-rw-r--r--   1 root root    104 Jan 12  2019 RELEASE
drwxr-xr-x   7 root root   4096 Sep 11 15:53 scripts
-rw-r--r--   1 root root      0 Sep 18 15:36 testfile.txt
lrwxrwxrwx   1 root root     13 May  9 11:06 var -> /var/lib/otrs

I was trying etckeeper to auto commit after package manager make some changes, but git will as defaulte create only empty folder var/ and ARCHIVE/.

I was trying submodules, but then i don't know about changes in symlink folders (git add .) and submodule content can't lead to symlink folder.

My biggest issue is how to store these folders in git and how to clone/checkout in same structure.


Solution

  • Solution was easy & clean, i hope :)

    apt-get install etckeeper
    

    Make directory for repository. This is mounted to other disk

    mkdir /BACKUP
    cd /BACKUP
    

    Initialize git repo & set worktree to root /. Now i can tracking whole system changes, not only /etc

    git init 
    git config --path core.worktree /
    

    My .gitignore inside /BACKUP:

    # Ignore everything ~ whitelist
    *
    
    # But descend into directories
    !*/
    
    # Add /usr/share/otrs/ - main directory
    !/usr/share/otrs/**
    
    # Add /var/lib/otrs - symlink # Add /usr/share/otrs/var -> /var/lib/otrs 
    !/var/lib/otrs/**
    
    ...
    
    # Add myself
    !/.gitignore
    
    # Add etckeeper staff
    !/etc/.etckeeper/**
    

    Add new line at the end of /etc/etckeeper/etckeeper.conf with path to repository:

    ETCKEEPER_DIR=/BACKUP
    

    At the end, remove unused repo initialized by etckeeper after installation:

    cd /etc
    rm -rf .git
    rm .gitignore
    

    Now you can make first inital commit: etckeeper commit