Skip to content

Instantly share code, notes, and snippets.

@dchakro
Last active August 24, 2020 13:50
Show Gist options
  • Save dchakro/2bb714db3712f76610fe4f6ee2a82155 to your computer and use it in GitHub Desktop.
Save dchakro/2bb714db3712f76610fe4f6ee2a82155 to your computer and use it in GitHub Desktop.
ConfigGuard - Safeguard your dotfiles and /etc/ configs with bare git repo

How to use ConfigGuard

  1. Grab ConfigGuard from this Gist.

    # For deploying dotGuard
    curl -OJL 'https://gist.githubusercontent.com/dchakro/2bb714db3712f76610fe4f6ee2a82155/raw/deploy_Cerberus.sh'
    
    # For deploying etcGurad
    curl -OJL 'https://gist.githubusercontent.com/dchakro/2bb714db3712f76610fe4f6ee2a82155/raw/deploy_etcGuard.sh'
  2. Make any necessary changes to the ConfigGuard.sh.

    Tip : create a repo for dotGuard/Cerberus (one repo holds all devices as separate branches) and modify deploy_Cerberus.sh. For etcGuard (one repo for one device) and copy the remote URLs.

  3. Run ConfigGuard bash deploy_Cerberus.sh or bash deploy_etcGuard.sh and add remote URLs for your repos (if you plan on using any).

  4. Run initial commit:

    dotGuard add .bashrc .zshrc
    dotGuard commit -m "initial commit"
    dotGuard push -u origin master
    
    # the next step most likely will require sudo
    sudo etcGuard add -A
    etcGuard commit -m "initial commit"
    etcGuard push -u origin master
  5. Enjoy the freedom of customixing your UNIX experience!

    # From the second time onwards
    dotGuard add <file>
    dotGuard commit -m "commit message"
    dotGuard push
    
    sudo etcGuard add <file>
    etcGuard commit -m "commit message"
    etcGuard push
  6. Remember that depending on your rwx permissions you may or may not have to use sudo with etcguard.

N.B. Tested to work on MacOS and Debian linux.

#!/usr/bin/env bash
# deployCerberus sets up a bare repo in: ~/.cerberus.git
# to safeguard your dotfiles and
# Depending on your rwx permissions for /etc/ you might have to:
# - use 'sudo' in line 46
# Define your Cerberus repo here:
CerberusRepo='git@gitlab.com:dotGuard/cerberus.git'
if [ -d $HOME/.cerberus.git ] ; then
echo "[✓]Cerberus - DotGuard exists..."
git fetch origin '*:*'
else
echo ""
echo "[!]Cloning ~/.cerberus.git"
# Cloning my cerberus repo here
git clone --bare ${CerberusRepo}
mv cerberus.git .cerberus.git
fi
echo "Deploying dotGuard"
cd ~/.cerberus.git
echo "Listing branches in Cerbrus"
$(command -v git) --git-dir=$HOME/.cerberus.git/ branch -a
read -r -p "Copy-Paste branch name for this device. (Leave Blank if none) : " branch_choice;
if [ -z "${branch_choice}" ]; then
echo "Create a branch on cerberus and then re-run this script."
exit 1
else
$(command -v git) --git-dir=$HOME/.cerberus.git/ --work-tree=$HOME config --local status.showUntrackedFiles no
# git switch only works on git v2.23 and up.
$(command -v git) --git-dir=$HOME/.cerberus.git/ --work-tree=$HOME switch -f $branch_choice
# fallback on git checkout -f
# Hopefully there is no file with the same name as one of the branches
if [ $? -ne 0 ]; then
$(command -v git) --git-dir=$HOME/.cerberus.git/ --work-tree=$HOME checkout -f $branch_choice
fi
echo "Check that the branch is correct (${branch_choice}) :"
$(command -v git) --git-dir=$HOME/.cerberus.git/ --work-tree=$HOME branch -a
fi
$(command -v git) --git-dir=$HOME/.cerberus.git/ --work-tree=$HOME status -s
cd ~/
echo '$(command -v git) --git-dir=$HOME/.cerberus.git/ --work-tree=$HOME "$@"' >| $HOME/dotGuard
chmod +x $HOME/dotGuard
mv $HOME/dotGuard /usr/local/bin/dotGuard
echo "dotGuard is set up. Opening a new instance of $SHELL to load aliases."
$SHELL
#!/usr/bin/env bash
# ConfigGuard sets up bare repo in: ~/.etcGuard.git
# to safeguard your /etc/ configs
# Depending on your rwx permissions for /etc/ you might have to:
# - add 'sudo' in line 38 as --> 'sudo $(command -v git) --git-dir.....'
# - use 'sudo' in line 40
if cd ~/.etcGuard.git > /dev/null ; then
echo ""
echo "[✓]etcGuard exists..."
else
echo ""
echo "[!]Creating ~/.etcGuard.git"
git init --bare ~/.etcGuard.git
fi
echo ""
echo "Deploying etcGuard"
cd ~/.etcGuard.git
git remote -v | grep "origin" > /dev/null
if [ $? -ne 0 ]; then
read -r -p "Set remote etcGuard (blank = skip) : " git_remote;
if [ -z "${git_remote}" ]; then
echo "Setting Only local instance."
else
$(command -v git) remote add origin $git_remote
fi
else
echo "Remote already mapped"
git remote -v
fi
cd ~/
echo '$(command -v git) --git-dir=$HOME/.etcGuard.git/ --work-tree=/etc/ "$@"' >| $HOME/etcGuard
chmod +x $HOME/etcGuard
mv $HOME/etcGuard /usr/local/bin/etcGuard
echo "etcGuard is set up. Opening a new instance of $SHELL to load aliases."
$SHELL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment