Though Magento has an in-built tool for the backup. I would recommend using a shell script based backup:
- Linux native command is always faster than PHP.
- There is no enabling and disabling of maintenance mode while running the backup script. So there is no downtime.
- To have more Freedom. Magento2 console script is limited. What if you want to backup in Amazon S3, Google Drive, etc.? If you have an independent script you can play as you like without worrying about breaking the Magento2 system.
Here is the link to such bash script which creates a backup for your Magento2 code files & database on demand or via Cron:
You can simply use the command as
./mage2-backup.sh --backup-db --backup-code --skip-media --src-dir=/path/to/magento2/root --dest-dir=/path/to/destination
If you want to schedule via Cron, just add the following line in your Crontab entry
0 0 * * * /path/to/mage2-backup.sh --backup-db --backup-code --skip-media --src-dir=/path/to/magento2/root --dest-dir=/path/to/destination > /dev/null 2>&1
0 0 * * * expression means the command will run run at every midnight.