~tardypad/dotfiles

ref: 00c0c349a1b8960152233f11caa9ea1a40e395a3 dotfiles/files/scripts/shell/backup -rwxr-xr-x 3.7 KiB
00c0c349 — Damien Tardy-Panis Add presentation mode to mako to hide notifications 4 months ago
                                                                                
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/bin/sh

init_variables() {
  COMMAND=${0##*/}

  LOCAL_BROWSER_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/qutebrowser"
  LOCAL_BOOKS_DIR="${XDG_BOOKS_DIR:-$HOME/Books}"
  LOCAL_CONTACTS_DIR="${XDG_CONTACTS_DIR:-$HOME/Contacts}"
  LOCAL_NOTES_DIR="${XDG_NOTES_DIR:-$HOME/Notes}"
  LOCAL_TIME_TRACKER_LOGS_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/time-tracker/logs"
  LOCAL_VIDEOS_DIR="${XDG_VIDEOS_DIR:-$HOME/Videos}"

  HDD_BASE_DIR='/mnt/lacie_data'
  HDD_BACKUP_DIR="${HDD_BASE_DIR}/Backup"

  HDD_BOOKMARKS_DIR="${HDD_BASE_DIR}/Bookmarks"
  HDD_BOOKS_DIR="${HDD_BASE_DIR}/Books"
  HDD_CONTACTS_DIR="${HDD_BASE_DIR}/Contacts"
  HDD_NOTES_DIR="${HDD_BASE_DIR}/Notes"
  HDD_VIDEOS_DIR="${HDD_BASE_DIR}/Videos"

  SERVER_HOST='surg'
  SERVER_BASE_DIR="backup/$( uname -n )"

  SERVER_BOOKMARKS_DIR="${SERVER_BASE_DIR}/bookmarks"
  SERVER_CONTACTS_DIR="${SERVER_BASE_DIR}/contacts"
  SERVER_NOTES_DIR="${SERVER_BASE_DIR}/notes"
  SERVER_TIME_TRACKER_LOGS_DIR="${SERVER_BASE_DIR}/time_tracker_logs"

  TARGET=
  ITEM=
}

parse_command_line() {
  TARGET="$1"
  ITEM="$2"
}

validate_command_line() {
  if [ -z "${TARGET}" ]; then
    exit_error 'missing target'
  fi

  if [ "${TARGET}" != 'hdd' ] \
     && [ "${TARGET}" != 'server' ]; then
    exit_error "Invalid target '${TARGET}'"
  fi

  if [ -z "${ITEM}" ]; then
    exit_error 'missing item'
  fi

  if [ "${ITEM}" != 'bookmarks' ] \
     && [ "${ITEM}" != 'books' ] \
     && [ "${ITEM}" != 'contacts' ] \
     && [ "${ITEM}" != 'notes' ] \
     && [ "${ITEM}" != 'time_tracker_logs' ] \
     && [ "${ITEM}" != 'videos' ]; then
    exit_error "Invalid item '${ITEM}'"
  fi

  if [ "${TARGET}" = 'hdd' ]; then
    if [ "${ITEM}" = 'time_tracker_logs' ]; then
      exit_error "Cannot backup ${ITEM} on ${TARGET}"
    fi
  fi

  if [ "${TARGET}" = 'server' ]; then
    if [ "${ITEM}" = 'books' ] || [ "${ITEM}" = 'videos' ]; then
      exit_error "Cannot backup ${ITEM} on ${TARGET}"
    fi
  fi
}

exit_error() {
  [ -z "$1" ] || echo "${COMMAND}: $1"
  exit 1
} >&2

backup_hdd_bookmarks() {
  rsync -az \
    --backup-dir="${HDD_BACKUP_DIR}/Bookmarks" \
    "${LOCAL_BROWSER_DIR}/bookmarks/urls" \
    "${HDD_BOOKMARKS_DIR}/urls"

  rsync -az \
    "${LOCAL_BROWSER_DIR}/quickmarks" \
    "${HDD_BOOKMARKS_DIR}/quickmarks"
}

backup_hdd_books() {
  rsync -az --delete \
    --backup-dir="${HDD_BACKUP_DIR}/Books" \
    "${LOCAL_BOOKS_DIR}/" \
    "${HDD_BOOKS_DIR}"
}

backup_hdd_contacts() {
  rsync -az --delete --exclude=".*" \
    --backup-dir="${HDD_BACKUP_DIR}/Contacts" \
    "${LOCAL_CONTACTS_DIR}/" \
    "${HDD_CONTACTS_DIR}"
}

backup_hdd_notes() {
  rsync -az --delete --exclude=".*" \
    --backup-dir="${HDD_BACKUP_DIR}/Notes" \
    "${LOCAL_NOTES_DIR}/" \
    "${HDD_NOTES_DIR}"
}

backup_hdd_videos() {
  rsync -az \
    "${LOCAL_VIDEOS_DIR}/" \
    "${HDD_VIDEOS_DIR}"
}

backup_server_bookmarks() {
  rsync -azb \
    "${LOCAL_BROWSER_DIR}/bookmarks/urls" \
    "${SERVER_HOST}:${SERVER_BOOKMARKS_DIR}/urls"

  rsync -azb \
    "${LOCAL_BROWSER_DIR}/quickmarks" \
    "${SERVER_HOST}:${SERVER_BOOKMARKS_DIR}/quickmarks"
}

backup_server_contacts() {
  rsync -azb --delete --exclude=".*" \
    "${LOCAL_CONTACTS_DIR}/" \
    "${SERVER_HOST}:${SERVER_CONTACTS_DIR}"
}

backup_server_notes() {
  rsync -azb --delete --exclude=".*" \
    "${LOCAL_NOTES_DIR}/" \
    "${SERVER_HOST}:${SERVER_NOTES_DIR}"
}

backup_server_time_tracker_logs() {
  rsync -azb --delete --exclude=".*" \
    "${LOCAL_TIME_TRACKER_LOGS_DIR}/" \
    "${SERVER_HOST}:${SERVER_TIME_TRACKER_LOGS_DIR}"
}

init_variables
parse_command_line "$@"
validate_command_line

if [ "${TARGET}" = 'hdd' ] && [ ! -f "${HDD_BASE_DIR}/.hdd" ]; then
  exit_error "HDD is not connected"
fi

"backup_${TARGET}_${ITEM}"