How to Set Up a Cron Job for Automated Backups on DigitalOcean - Django
Oct. 16, 2024, 9:51 p.m.
In any web project, maintaining regular backups is crucial to safeguard your data. Automating these backups can save you time and effort, ensuring that your information is always safe. If you’re running a Django project on a DigitalOcean droplet, you can easily set up cron jobs to automate tasks like backups. This guide will walk you through how to configure a cron job to back up your database and media files every night at 2 AM.
What Is a Cron Job?
A cron job is a Linux utility that schedules commands or scripts to run automatically at specific times and dates. This is particularly useful for tasks like backups, maintenance, or regular updates.
1. Write a Django Management Command for Backups
Before setting up the cron job, make sure you have a management command in Django that performs the backup. If you haven’t written one yet, here’s a simple version of a backup command:
The name of the file where the code resides is: create_backup.py
project/
├── my_app/
├── management/
├── commands/
├── create_backup.py # This is where the provided code is stored