From b311d7d51b3777b6cfc60e100a61bf563dee51ec Mon Sep 17 00:00:00 2001
From: Yohann D'ANELLO <yohann.danello@gmail.com>
Date: Thu, 6 Aug 2020 09:26:16 +0200
Subject: [PATCH] :heavy_plus_sign: Backup database script

---
 shell/backup_db | 11 +++++++++++
 1 file changed, 11 insertions(+)
 create mode 100755 shell/backup_db

diff --git a/shell/backup_db b/shell/backup_db
new file mode 100755
index 0000000..435ef8e
--- /dev/null
+++ b/shell/backup_db
@@ -0,0 +1,11 @@
+#!/bin/bash
+# Create backups directory
+[[ -d /var/www/note_kfet/backups ]] || (mkdir /var/www/note_kfet/backups && chown www-data:www-data /var/www/note_kfet/backups)
+date=$(date +%Y-%m-%d)
+# Backup database and save it as tar archive
+su postgres -c "pg_dump -F t note_db" | tee "/var/www/note_kfet/backups/$date.tar" > /dev/null
+# Compress backup as gzip
+gzip "/var/www/note_kfet/backups/$date.tar"
+chown www-data:www-data "/var/www/note_kfet/backups/$date.tar.gz"
+# Delete backups that have more than 30 days
+find /var/www/note_kfet/backups -type f -mtime +30 -exec rm {} \;
\ No newline at end of file
-- 
GitLab