davup.sh (2321B) - raw


      1 #!/bin/sh
      2 # Copyright (C) 2020 Oscar Benedito <oscar@oscarbenedito.com>
      3 # License: Affero General Public License version 3 or later
      4 
      5 # DAVup: back up calendars and contacts from a DAV server (CalDAV and CardDAV).
      6 
      7 domain="<DAV server address>"       # example: https://dav.mailbox.org (no trailing "/")
      8 user="<username>"
      9 pass="<password>"
     10 
     11 get_cal() {
     12     curl -s -X "PROPFIND" -u "${user}:${pass}" -H "Content-Type: text/xml" -H "Depth: 1" \
     13         --data "<propfind xmlns='DAV:'><prop><calendar-data xmlns='urn:ietf:params:xml:ns:caldav'/></prop></propfind>" \
     14         "${domain}/${resource}"
     15 }
     16 
     17 get_card() {
     18     curl -s -X "PROPFIND" -u "${user}:${pass}" -H "Content-Type: text/xml" -H "Depth: 1" \
     19         --data "<propfind xmlns='DAV:'><prop><address-data xmlns=\"urn:ietf:params:xml:ns:carddav\"/></prop></propfind>" \
     20         "${domain}/${resource}"
     21 }
     22 
     23 process_cal() {
     24     intro="BEGIN:VCALENDAR"
     25     preevent="BEGIN:VEVENT"
     26     postevent="END:VEVENT"
     27     pretodo="BEGIN:VTODO"
     28     posttodo="END:VTODO"
     29     step="0"
     30     while read -r line; do
     31         line="${line%}"
     32         case $step in
     33             0) echo "${line}" | grep -q "${intro}" && step="1" && echo "${intro}" ;;
     34             1) echo "${line}"; \
     35                 if [ "${line}" = "${preevent}" ]; then step="2"; \
     36                 elif [ "${line}" = "${pretodo}" ]; then step="3"; fi ;;
     37             2) echo "${line}" && [ "${line}" = "${postevent}" ] && step="4" ;;
     38             3) echo "${line}" && [ "${line}" = "${posttodo}" ] && step="4" ;;
     39             4) if [ "${line}" = "${preevent}" ]; then step="2"; echo "${line}"; \
     40                 elif [ "${line}" = "${pretodo}" ]; then step="3"; echo "${line}"; fi ;;
     41         esac
     42     done
     43     echo "END:VCALENDAR"
     44 }
     45 
     46 process_card() {
     47     pre="BEGIN:VCARD"
     48     post="END:VCARD"
     49     step="1"
     50     while read -r line; do
     51         line="${line%}"
     52         case $step in
     53             1) echo "${line}" | grep -q "${pre}" && step="2" && echo "${pre}" ;;
     54             2) echo "${line}" | grep -q "${post}" && step="1" && echo "${post}" \
     55                 || echo "${line}" ;;
     56         esac
     57     done
     58 }
     59 
     60 # examples (resource address will be "${domain}/${resource}"):
     61 #     resource="caldav/mycal" && get_cal | process_cal > calendar_and_todos.ics
     62 #     resource="carddav/mycard" && get_card | process_card > contacts.vcf