You don’t need to sudo in the bash command. docker exec -u www-data nextcloud php occ ... will do the job.
You have to create a cronjob for root or any user on your host that can run docker commands to call your backup script. crontab -e
The cron syntax you’ll find on Google.