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:
- Django: Venv under FreeBSD
- Django: MVT, Apps and URLs
- Django: Database Models
- Django: Administration Interface
- Django: DTL(Django Template Language)
- Django: Debug Toolbar
- Django: User Registration and Authentication
- Django: Webpack
- Django: Bootstrap with WebPack
- Django: Project in Production
- Django: Translations
- Django: Administrative Commands
- Django: Updates
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:
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:
#! /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:
Now we can run the script:
We can see the file in the correct directory:
-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:
00 00 * * * /root/.scripts/backup.sh >/dev/null 2>&1