summaryrefslogtreecommitdiff
path: root/scripts.org
blob: b179385b174a26d1178d9164da48c521321750b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#+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
  <<helper-functions>>
  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
Copyright 2019--2024 Marius PETER