Skip to content

Instantly share code, notes, and snippets.

@cc32d9
Created October 10, 2021 13:17
Show Gist options
  • Select an option

  • Save cc32d9/fbd7ddd668f03c5abcecd0b4e9b3bd50 to your computer and use it in GitHub Desktop.

Select an option

Save cc32d9/fbd7ddd668f03c5abcecd0b4e9b3bd50 to your computer and use it in GitHub Desktop.

Revisions

  1. cc32d9 created this gist Oct 10, 2021.
    93 changes: 93 additions & 0 deletions backup_configs.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,93 @@
    #!/bin/sh

    ## This simple script is copying configuration files and directories
    ## from your server to your private Git repository. make sure that the
    ## Git repository is private and under your control.
    ##
    ## Put your directory and file names into /etc/backup_configs and run
    ## the script. It will create a local Git working directory, copy
    ## files into it, and push the update to your Git repository.
    ##
    ## It uses two branches: the one named after your hostname contains
    ## copies of configuration files, and the branch "addresses" contains
    ## a mapping between the host names and public IP addresses.



    REPO=git@MYPRIVATEGITHOST:backup_configs
    BRANCH=`hostname -f`
    MYHOST=`hostname -f`
    CFG=/etc/backup_configs
    LIBDIR=/var/lib/backup_configs
    CONFIGSDIR=${LIBDIR}/configs

    ADDRESSESDIR=${LIBDIR}/addresses
    ADDRBRANCH=addresses

    if [ ! -r ${CFG} ]; then
    echo Cannot read ${CFG} 1>&2
    exit 1
    fi

    if [ ! -d ${CONFIGSDIR} ]; then
    mkdir -p ${CONFIGSDIR}
    if [ $? != 0 ]; then
    echo Error creating ${CONFIGSDIR} 1>&2
    exit 1
    fi
    cd ${CONFIGSDIR}
    git init
    git config --local user.name backup_configs
    git config --local user.email root@${MYHOST}
    git checkout -b ${BRANCH}
    git remote add origin ${REPO}
    fi

    for src in `cat ${CFG}`; do
    cp -a --parents ${src} ${CONFIGSDIR}
    if [ $? != 0 ]; then
    echo Error copying ${src} 1>&2
    exit 1
    fi
    done

    cd ${CONFIGSDIR}
    git add --all
    git commit -m `date --iso-8601=seconds --utc`
    git push -u origin ${BRANCH}
    if [ $? != 0 ]; then
    echo Error pushing to git repo 1>&2
    exit 1
    fi

    MYADDR=`curl -4 --silent https://ifconfig.co`
    if [ x${MYADDR} != x ]; then

    if [ ! -d ${ADDRESSESDIR} ]; then
    mkdir -p ${ADDRESSESDIR}
    if [ $? != 0 ]; then
    echo Error creating ${ADDRESSESDIR} 1>&2
    exit 1
    fi
    cd ${ADDRESSESDIR}
    git init
    git config --local user.name backup_configs
    git config --local user.email root@${MYHOST}
    git checkout -b ${ADDRBRANCH}
    git remote add origin ${REPO}
    fi

    cd ${ADDRESSESDIR}
    git pull
    echo ${MYHOST} > ${MYADDR}
    echo ${MYADDR} > ${MYHOST}
    git add --all
    git commit -m `date --iso-8601=seconds --utc`
    git push -u origin ${ADDRBRANCH}
    if [ $? != 0 ]; then
    echo Error pushing to git repo 1>&2
    exit 1
    fi
    fi