~tardypad/dotfiles

ref: 2a7e6d9677363fb74cfc6d736ce3ac5239718540 dotfiles/files/scripts/shell/backup -rwxr-xr-x 4.1 KiB
2a7e6d96Damien Tardy-Panis Add missing backup-dir in backup script for quickmarks a month 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
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/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"

  SOURCE='laptop'
  TARGET='hdd'
  ITEM=
}

parse_command_line() {
  while getopts s:t: OPT; do
    case "${OPT}" in
      s) SOURCE="${OPTARG}" ;;
      t) TARGET="${OPTARG}" ;;
      ?) exit_error ;;
    esac
  done

  shift $(( OPTIND - 1 ))

  ITEM="$1"
}

validate_command_line() {
  if [ "${SOURCE}" != 'laptop' ]; then
    exit_error "Invalid source '${SOURCE}'"
  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 [ "${SOURCE}" = 'laptop' ]; then
    if [ "${TARGET}" = 'hdd' ]; then
      if [ "${ITEM}" = 'time_tracker_logs' ]; then
        exit_error "Cannot backup ${ITEM} from ${SOURCE} to ${TARGET}"
      fi
    fi

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

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

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

  rsync -az \
    --backup-dir="${HDD_BACKUP_DIR}/Bookmarks" \
    "${LOCAL_BROWSER_DIR}/quickmarks" \
    "${HDD_BOOKMARKS_DIR}/quickmarks"
}

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

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

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

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

backup_laptop_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_laptop_server_contacts() {
  rsync -azb --delete --exclude=".*" \
    "${LOCAL_CONTACTS_DIR}/" \
    "${SERVER_HOST}:${SERVER_CONTACTS_DIR}"
}

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

backup_laptop_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_${SOURCE}_${TARGET}_${ITEM}"