#+TITLE: Scripts for admin #+AUTHOR: Marius Peter #+DATE: <2022-01-23 Sun> #+begin_abstract The code blocks contained in this ~Smart Document~ are Bash and Python scripts that automate certain sysadmin tasks on my local box. #+end_abstract * Helper functions These helper functions were adapted from the Guix project. #+NAME: helper-functions #+BEGIN_SRC sh :results verbatim timestamp() { date --rfc-3339=s; } _err() { # All errors go to stderr. printf "[%s]: %s\n" "$(date +%s.%3N)" "$1" } _msg() { # Default message to stdout. printf "[%s]: %s\n" "$(date --rfc-3339=s)" "$1" } _debug() { if [ "${DEBUG}" = '1' ]; then printf "[%s]: %s\n" "$(date +%s.%3N)" "$1" fi } # Return true if user answered yes, false otherwise. # $1: The prompt question. prompt_yes_no() { while true; do read -rp "$1 " yn case $yn in [Yy]*) return 0;; [Nn]*) return 1;; ,*) _msg "Please answer yes or no." esac done } #+END_SRC * Backup This scripts #+BEGIN_SRC bash :noweb yes :results verbatim <> bkp_dirs=("~/Desktop" \ "~/Documents" \ "~/git" \ "~/Music/" \ "~/org/" \ "~/.emacs.d") _msg "Preparing backup..." _msg "List of directories to backup:" # printf "%s\n" ${bkp_dirs[*]} for file in $(ls) do rsync -a $file bkp/ printf "Successfully sent file %s\n" $file done rsync -a ./ ./bkp _msg "Backup was completed successfully!" #+END_SRC #+RESULTS: #+begin_example [2022-01-23 16:30:36+01:00]: Preparing backup... [2022-01-23 16:30:36+01:00]: List of directories to backup: Successfully sent file Makefile Successfully sent file README.org Successfully sent file bashrc.org Successfully sent file bkp Successfully sent file herbstluftwm-autostart.org Successfully sent file panel.sh Successfully sent file scripts.org Successfully sent file test.ml Successfully sent file xinitrc.org [2022-01-23 16:30:36+01:00]: Backup was completed successfully! #+end_example