From e186fdf7dbdbeb3956a44b9dfb46c6222208cebc Mon Sep 17 00:00:00 2001 From: tosu Date: Sat, 22 Jun 2024 20:20:43 +0200 Subject: [PATCH] Add backup scripts (require root) --- .bash_xinitrc | 17 +++--- .bashrc | 113 +++++++++++++++++++++++++--------------- .local/sbin/backup_dir | 18 +++++++ .local/sbin/backup_file | 18 +++++++ 4 files changed, 119 insertions(+), 47 deletions(-) mode change 100755 => 100644 .bashrc create mode 100755 .local/sbin/backup_dir create mode 100755 .local/sbin/backup_file diff --git a/.bash_xinitrc b/.bash_xinitrc index f2ae84a..6f7eb62 100644 --- a/.bash_xinitrc +++ b/.bash_xinitrc @@ -169,21 +169,26 @@ export MAIL="timo42@proton.me" export GOPATH="$HOME/go" -ld_lib_path_append "${LD_LIBRARY_PATH}:${HOME}/.local/lib" -export LD_LIBRARY_PATH +# ld_lib_path_append \ +# "${HOME}/.local/lib" +# export LD_LIBRARY_PATH -path_prepend "/bin" \ +path_prepend \ + "/bin" \ "/sbin" \ "/usr/bin" \ "/usr/sbin" \ "/usr/local/bin" \ - "/usr/lcoal/sbin" \ - "/usr/local/games" \ + "/usr/local/sbin" \ "/usr/games" \ + "/usr/local/games" \ "/snap/bin" \ + +path_append \ "${HOME}/bin" \ + "${HOME}/.bin" \ "${HOME}/.local/bin" \ - "${HOME}/.local/include" \ + "${HOME}/.local/sbin" \ "${HOME}/.brew/bin" \ "${GOPATH}/bin" export PATH diff --git a/.bashrc b/.bashrc old mode 100755 new mode 100644 index fb197df..aaecbdc --- a/.bashrc +++ b/.bashrc @@ -1,6 +1,8 @@ #! /bin/bash -- # ex: set ts=4 sw=4 ft=sh +# TODO: Test on solaris (and other unices). Many tools will fail since we're not considering /usr/xpg4/bin + # Exit when noninteractive. This is more portable than checking PS1. [ "${-#*i}" = "${-}" ] && return @@ -52,6 +54,73 @@ hash -r unalias -a unset POSIXLY_CORRECT +################################# ENVIRONMENT ################################## +export EDITOR="$({ type -P nvim || + type -P vim || + type -P vi || + type -P nvi || + type -P hx || + type -P nano || + type -P ex || + type -P ed ; } 2>/dev/null)" +export GIT_SSH_COMMAND='ssh -oIdentitiesOnly=yes -F"${HOME-}"/.ssh/config' +export LANG='en_US.UTF-8' +export USER="${USER:-$(whoami)}" +export VISUAL="${EDITOR-}" +export SUDO_EDITOR="${EDITOR-}" +export XDG_RUNTIME_DIR="/run/user/$(id -u)" +export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/ssh-agent.socket" +export GIT_CONFIG_GLOBAL="$HOME/.gitconfig" +export GPG_TTY="$(tty)" +export _JAVA_AWT_WM_NONREPARENTING="1" +export USER42="tischmid" +export EMAIL42="timo42@proton.me" +export MAIL="timo42@proton.me" +export GOPATH="$HOME/go" +export MANPAGER='nvim +Man!' +# export MANPAGER='less -X' +export BAT_THEME='gruvbox-dark' +# export BAT_THEME='gruvbox-light' +export PATH +export LD_LIBRARY_PATH + +if ! [ -n "${USER-}" ] ; then +if ! USER="$(2>/dev/null ps -o user= -p "${$}" | awk '{print $1}')" ; then +if ! USER="$(2>/dev/null whoami)" ; then +if ! USER="$(2>/dev/null id -u -n)"; then +if ! USER="$(basename -- "$(HOME=~ && printf %s "${HOME}")")" ; then +if ! USER="$(2>/dev/null logname)" ; then +if USER="${LOGNAME-}" ; [ -z "${USER}" ] ; then +unset USER +fi; fi; fi; fi; fi; fi; fi + +if ! [ -n "${HOME-}" ] ; then +if ! HOME="$(getent passwd "$(id -u "${USER}")" | cut -d: -f6)" ; then +if ! HOME="$(getent passwd "${UID}" | cut -d: -f6)" ; then +if ! HOME="$(awk -v FS=':' -v user="${USER}" '($1==user) {print $6}' "/etc/passwd")" ; then +unset HOME +HOME=~ +if [ "${HOME}" = "~" ] ; then +if ! mkdir "/tmp/${USER}" && HOME="/tmp/${USER}" ; then +unset HOME +fi; fi ; fi ; fi ; fi; fi + +[ -n "${DISPLAY-}" ] || log.warn 'DISPLAY not set' + +if [ ! "${TERM-}" = "linux" ] ; then + if [ -f '/usr/share/terminfo/x/xterm-256color' ] ; then + export TERM='xterm-256color' + elif [ -f '/usr/share/terminfo/x/xterm-color' ] ; then + export TERM='xterm-color' + elif [ -f '/usr/share/terminfo/x/xterm' ] ; then + export TERM='xterm' + elif [ -f '/usr/share/terminfo/s/screen-256color' ] ; then + export TERM='screen-256color' + elif [ -f '/usr/share/terminfo/s/screen' ] ; then + export TERM='screen' + fi +fi + ######################## path append & prepend (posix) ######################### pathvarprepend () { # prepending paths to pathvar denoted by the expansion of the PATHVAR parameter @@ -207,14 +276,15 @@ readonly BASH_SESSION_NAME="${$}" HISTSIZE='-1' HISTFILESIZE='-1' REAL_HISTFILE="${HOME}/.better_bash_history/.bash_history_$(printf "%(%Y-%m-%d)T")_daily" +REAL_HISTDIR="$(dirname -- "${REAL_HISTFILE}")" HISTFILE="${HOME}/.better_bash_history/.bash_history_$(printf '%(%Y-%m-%d-%H-%M-%S)T')_${BASH_SESSION_NAME}" -HISTDIR="$(dirname -- "$HISTFILE")" +HISTDIR="$(dirname -- "${HISTFILE}")" HISTTIMEFORMAT=$'\033[m%F %T: ' HISTCONTROL='ignoreboth' history -c history -r -- "${REAL_HISTFILE}" write_history () { - [ -d "${REAL_HISTFILE}" ] || { rm -f -- "$(dirname -- "${REAL_HISTFILE}")" && mkdir -p -- "$(dirname -- "${REAL_HISTFILE}")" ; } + [ -d "${REAL_HISTDIR}" ] || { rm -f -- "${REAL_HISTDIR}" && mkdir -p -- "${REAL_HISTDIR}" ; } [ -f "${HISTFILE}" ] && [ -r "${HISTFILE}" ] && <"${HISTFILE}" 1>/dev/null tee -a -- "${REAL_HISTFILE}" && @@ -532,45 +602,6 @@ clone42 () { } || { printf '%s\n' "Could not clone repo!" ; } } -################################# ENVIRONMENT ################################## -export GIT_SSH_COMMAND='ssh -oIdentitiesOnly=yes -F"${HOME-}"/.ssh/config' - -export LANG='en_US.UTF-8' -export USER="${USER:-$(whoami)}" - -export EDITOR="$({ type -P nvim || - type -P vim || - type -P vi || - type -P nvi || - type -P hx || - type -P nano || - type -P ex || - type -P ed ; } 2>/dev/null)" -export VISUAL="${EDITOR-}" -export SUDO_EDITOR="${EDITOR-}" - -export MANPAGER='nvim +Man!' -# export MANPAGER='less -X' - -export BAT_THEME='gruvbox-dark' -# export BAT_THEME='gruvbox-light' - -[ -n "${DISPLAY-}" ] || log.warn 'DISPLAY not set' - -if [ ! "${TERM-}" = "linux" ] ; then - if [ -f '/usr/share/terminfo/x/xterm-256color' ] ; then - export TERM='xterm-256color' - elif [ -f '/usr/share/terminfo/x/xterm-color' ] ; then - export TERM='xterm-color' - elif [ -f '/usr/share/terminfo/x/xterm' ] ; then - export TERM='xterm' - elif [ -f '/usr/share/terminfo/s/screen-256color' ] ; then - export TERM='screen-256color' - elif [ -f '/usr/share/terminfo/s/screen' ] ; then - export TERM='screen' - fi -fi - ################################ BASH PRE-EXEC ################################# if [ ! -f "${HOME}"/.bash-preexec.sh ] ; then curl -sfkSL "https://raw.githubusercontent.com/rcaloras/bash-preexec/master/bash-preexec.sh" -o "${HOME}"/.bash-preexec.sh diff --git a/.local/sbin/backup_dir b/.local/sbin/backup_dir new file mode 100755 index 0000000..00ba454 --- /dev/null +++ b/.local/sbin/backup_dir @@ -0,0 +1,18 @@ +#! /bin/sh - +# Depends on GNU cp, basename, log.err + +[ "${#}" -ne "2" ] && { log.err "Usage: backup_dir BACKUP_DIR DIR" ; exit 1 ; } + +backupdir="${1:-${HOME}/backups}" +srcdir="${2}" +backupname="$(basename -- "$srcdir")" + +[ -d "${srcdir}" ] || { log.err "Directory '${srcdir}' does not exist" ; exit 2 ; } +[ -d "${backupdir}" ] || mkdir -p -- "${backupdir}" || { log.err "Backup Directory '${backupdir}' couldn't be created" ; exit 3 ; } + +if [ -e "${backupdir}/${backupname}" ]; then + chattr -i "${backupdir}/${backupname}" || { log.err "Can't chattr -i '${backupdir}/${backupname}'" ; exit 4 ; } +fi +tar --backup=t -cvzf "${backupdir}/${backupname}" "${srcdir}" || { log.err "Can't tar --backup=t -cvzf '${backupdir}/${backupname}' '${srcdir}'" ; exit 5 ; } + +find "${backupdir}" -mindepth 1 -exec chattr +i {} + || { log.err "Can't chattr +i some files" ; exit 6 ; } diff --git a/.local/sbin/backup_file b/.local/sbin/backup_file new file mode 100755 index 0000000..a930d85 --- /dev/null +++ b/.local/sbin/backup_file @@ -0,0 +1,18 @@ +#! /bin/sh - +# Depends on GNU cp, basename, log.err + +[ "${#}" -ne "2" ] && { log.err "Usage: backup_dir BACKUP_DIR FILE" ; exit 1 ; } + +backupdir="${1:-${HOME}/backups}" +srcfile="${2}" +backupname="$(basename -- "$srcfile")" + +[ -f "${srcfile}" ] || { log.err "Regular File '${srcfile}' does not exist" ; exit 2 ; } +[ -d "${backupdir}" ] || mkdir -p -- "${backupdir}" || { log.err "Backup Directory '${backupdir}' couldn't be created" ; exit 3 ; } + +if [ -e "${backupdir}/${backupname}" ]; then + chattr -i "${backupdir}/${backupname}" || { log.err "Can't chattr -i '${backupdir}/${backupname}'" ; exit 4 ; } +fi +cp --backup=t "${srcfile}" "${backupdir}/${backupname}" || { log.err "Can't cp --backup=t '${srcfile}' '${backupdir}/${backupname}'" ; exit 5 ; } + +find "${backupdir}" -mindepth 1 -exec chattr +i {} + || { log.err "Can't chattr +i some files" ; exit 6 ; }