forked from chidoziemanagwu/virtual_nutritionist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.py
53 lines (45 loc) · 2.16 KB
/
backup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
import zipfile
from datetime import datetime
# Define paths
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
DB_FILE = os.path.join(BASE_DIR, 'db.sqlite3') # Path to the database file
BACKUP_DIR = os.path.join(BASE_DIR, 'backups') # Directory to store backups
# Ensure the backup directory exists
if not os.path.exists(BACKUP_DIR):
os.makedirs(BACKUP_DIR)
# Create a timestamp for the backup file
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
backup_file = os.path.join(BACKUP_DIR, f'backup_{timestamp}.zip')
# Function to backup the SQLite database and other important files
def backup_data():
try:
# Create a zip file to store the backup
with zipfile.ZipFile(backup_file, 'w') as backup_zip:
# Add the SQLite database to the backup zip
if os.path.exists(DB_FILE):
backup_zip.write(DB_FILE, os.path.basename(DB_FILE))
print(f'Added {DB_FILE} to backup.')
else:
print('No database file found.')
# Backup the 'media' directory if it exists
media_folder = os.path.join(BASE_DIR, 'media')
if os.path.exists(media_folder):
for foldername, subfolders, filenames in os.walk(media_folder):
for filename in filenames:
file_path = os.path.join(foldername, filename)
backup_zip.write(file_path, os.path.relpath(file_path, BASE_DIR))
print(f'Added media files to backup.')
# Backup the 'static' directory if it exists
static_folder = os.path.join(BASE_DIR, 'static')
if os.path.exists(static_folder):
for foldername, subfolders, filenames in os.walk(static_folder):
for filename in filenames:
file_path = os.path.join(foldername, filename)
backup_zip.write(file_path, os.path.relpath(file_path, BASE_DIR))
print(f'Added static files to backup.')
print(f'Backup successful: {backup_file}')
except Exception as e:
print(f'Error during backup: {e}')
if __name__ == '__main__':
backup_data()