This page looks best with JavaScript enabled

Django Backups

 ·  ๐ŸŽƒ kr0m

Our Django application consists only of a PostgreSQL database, the code of the application itself, some Nginx configuration files, and the Daphne RC script. In this article, we will program a small script with which we will make backups of the previously mentioned parts.

Before starting, it is recommended to read the previous articles on Django as they are the previous steps to this article:


In my case, it is a Jail under Iocage , therefore I am going to make available the directory where to make the backups by editing the fstab of the Jail in question.

We run on the parent host:

mkdir /storage/backups/rxWod
iocage console rxWod
mkdir -p /storage/backups/rxWod
exit
iocage fstab -a rxWod “/storage/backups/rxWod /storage/backups/rxWod nullfs rw 0 0”

Now we can proceed with the backup script:

vi .scripts/backup.sh

#! /usr/local/bin/bash

DATE=$(date +%d_%m_%Y)
SHORT_HOSTNAME=$(hostname -s)
BACKUPSDIR="/storage/backups/$SHORT_HOSTNAME/BACKUP"

rm -rf $BACKUPSDIR 2>/dev/null
mkdir -p $BACKUPSDIR

# -- Common data --
cp -rp /root/.scripts/ $BACKUPSDIR/scripts
crontab -l > $BACKUPSDIR/crontab
cp /etc/hosts $BACKUPSDIR/hosts
cp /etc/rc.conf $BACKUPSDIR/rc.conf
# -----------------

# -- Custom data --
cd /home/kr0m/rxWod/
tar czf rxWodProject.tar.gz --exclude=rxWodProject/node_modules rxWodProject
mv rxWodProject.tar.gz $BACKUPSDIR/

pg_dump -U postgres rxwod > $BACKUPSDIR/rxwod.psql

cp /usr/local/etc/nginx/nginx.conf $BACKUPSDIR/
cp /usr/local/etc/nginx/rxwod.conf $BACKUPSDIR/
cp /usr/local/etc/rc.d/daphne $BACKUPSDIR/usr_local_etc_rc.d_daphne
# ------------------

DIRNAME=$(dirname $BACKUPSDIR)
cd $DIRNAME
tar czf backup_"$DATE".tar.gz BACKUP
chmod 600 backup_"$DATE".tar.gz

# Sleep 5s to let disk sync data and get correct backup size
sleep 5
BACKUPSIZE=$(du backup_"$DATE".tar.gz|awk '{print$1}')
BACKUPSIZEHUMAN=$(du -h backup_"$DATE".tar.gz|awk '{print$1}')
# 574276 -> 575 MB
if [ $BACKUPSIZE -lt 574276 ]; then
	python3.7 /root/.scripts/tg.py "$SHORT_HOSTNAME: Abnormal backup size -> /storage/backups/$SHORT_HOSTNAME/backup_$DATE.tar.gz $BACKUPSIZEHUMAN"
else
	python3.7 /root/.scripts/tg.py "$SHORT_HOSTNAME: Backup successfully -> /storage/backups/$SHORT_HOSTNAME/backup_$DATE.tar.gz $BACKUPSIZEHUMAN"
fi

rm -rf BACKUP

NOTE: As we can see, the script uses a second Python script to send notifications in case the backup is smaller than usual. This script is based on this previous article .

We assign the necessary permissions:

chmod 700 .scripts/backup.sh

Now we can run the script:

.scripts/backup.sh

We can see the file in the correct directory:

ls -la /storage/backups/rxWod/backup_27_03_2021.tar.gz

-rw------- ย 1 root ย wheel ย 230827 Mar 27 19:43 /storage/backups/rxWod/backup_27_03_2021.tar.gz

Now we just need to schedule it to run daily:

crontab -e

00 00 * * * /root/.scripts/backup.sh >/dev/null 2>&1
If you liked the article, you can treat me to a RedBull here