To backup all IMAP mailboxes for a user account, first download Rick Sander’s IMAP tools and save them somewhere on your system. Next, create a cron job and have it execute the following command:
mkdir -p /tmp/IMAP_$(date +%Y-%m-%d) && /path/to/imapdump/imapdump.pl -S example.com:993/user/password -f /tmp/IMAP_$(date +%Y-%m-%d) && cd /tmp/IMAP_$(date +%Y-%m-%d) && zip -r /backup/path/user@example.com_$(date +'%m-%d-%Y').zip user@example.com && chown user:user /backup/path/user@example.com_$(date +'%m-%d-%Y').zip && rm -Rf /tmp/IMAP_$(date +%Y-%m-%d)/user@example.com && find /backup/path -type f -mtime +90 -delete
To backup Nextcloud calendars and contacts, create a cron job running as root and have it execute the following command:
wget --directory-prefix=/tmp/nc-backup --user=ncuser --password=ncpassword --timestamping https://example.com/remote.php/dav/calendars/k4hjd9n3-k3ns-9385-9k2n-l3kjs9fk4b9z/personal-1?export https://example.com/remote.php/dav/addressbooks/users/k4hjd9n3-k3ns-9385-9k2n-l3kjs9fk4b9z/contacts?export && mv /tmp/nc-backup/personal-1?export /backup/path/calendar_$(date +'%m-%d-%Y').ics && mv /tmp/nc-backup/contacts?export /backup/path/contacts_$(date +'%m-%d-%Y').vcf && find /backup/path -type f -mtime +90 -delete
For Notes, do the same thing as above but execute the following instead:
mkdir -p /tmp/nc-backup && cd /nextcloud/path/k4hjd9n3-k3ns-9385-9k2n-l3kjs9fk4b9z/files && zip -r /tmp/nc-backup/notes_$(date +'%m-%d-%Y').zip Notes && chown user:user /tmp/nc-backup/notes_$(date +'%m-%d-%Y').zip && mv /tmp/nc-backup/notes_$(date +'%m-%d-%Y').zip /backup/path