|
| 1 | +import os |
| 2 | +import json |
| 3 | +import time |
| 4 | +from django.core.management.base import BaseCommand, CommandError |
| 5 | + |
| 6 | +from blog.admin import ( |
| 7 | + AuthorResource, TagResource, PostResource, |
| 8 | + GalleryResource, VisitorResource, PageResource |
| 9 | +) |
| 10 | +from blog.models import Post |
| 11 | + |
| 12 | +''' |
| 13 | + Refferences: |
| 14 | + - http://stackoverflow.com/a/11789141/3445802 |
| 15 | + - http://stackoverflow.com/a/3287063/6396981 |
| 16 | + See alseo: |
| 17 | + - https://docs.djangoproject.com/es/1.9/howto/custom-management-commands/ |
| 18 | +
|
| 19 | + *) If you work with `hosting`, you can setup on `Cron Jobs`, |
| 20 | + and setup your time with command: |
| 21 | + source /path/to/yourenv/bin/activate && cd /path/to/yourenv/yourproject && ./manage.py autobackup yes |
| 22 | +
|
| 23 | +
|
| 24 | + *) But, if you work with VPS or SERVER, please following this command bellow: |
| 25 | + $ sudo crontab -e |
| 26 | +
|
| 27 | + # Setup to daily method. |
| 28 | + [minute] [hour] [date] [month] [year] |
| 29 | +
|
| 30 | + 59 23 * * * source /path/to/yourenv/bin/activate && cd /path/to/yourenv/yourproject && ./manage.py autobackup yes |
| 31 | +
|
| 32 | +''' |
| 33 | + |
| 34 | + |
| 35 | +class Command(BaseCommand): |
| 36 | + help = 'To backup the blog using django-import-export!' |
| 37 | + |
| 38 | + def add_arguments(self, parser): |
| 39 | + parser.add_argument( |
| 40 | + 'backup', |
| 41 | + help='To backup the blog using django-import-export!' |
| 42 | + ) |
| 43 | + |
| 44 | + def handle(self, *args, **options): |
| 45 | + if options['backup'] == 'yes': |
| 46 | + |
| 47 | + # Checking directory backup, exist or yet! |
| 48 | + directory_backup = "autobackupjsondb" |
| 49 | + if os.path.isdir(directory_backup) == False: |
| 50 | + os.makedirs(directory_backup) |
| 51 | + |
| 52 | + def backupMixin(resources_set, fname): |
| 53 | + filepath = '{}/{}.json'.format( |
| 54 | + directory_backup, |
| 55 | + fname |
| 56 | + ) |
| 57 | + with open(filepath, 'w') as outfile: |
| 58 | + json.dump(resources_set.json, outfile) |
| 59 | + |
| 60 | + flastbackup = directory_backup + \ |
| 61 | + '/last_backup_date_{}'.format(time.strftime("%d-%m-%Y")) |
| 62 | + with open(flastbackup, 'w') as f: |
| 63 | + f.write('Just for date info!') |
| 64 | + f.close() |
| 65 | + |
| 66 | + authorset = AuthorResource().export() |
| 67 | + backupMixin(authorset, 'author') |
| 68 | + |
| 69 | + tagset = TagResource().export() |
| 70 | + backupMixin(tagset, 'tag') |
| 71 | + |
| 72 | + postset = PostResource().export() |
| 73 | + backupMixin(postset, 'post') |
| 74 | + |
| 75 | + galleryset = GalleryResource().export() |
| 76 | + backupMixin(galleryset, 'gallery') |
| 77 | + |
| 78 | + visitorset = VisitorResource().export() |
| 79 | + backupMixin(visitorset, 'visitor') |
| 80 | + |
| 81 | + pageset = PageResource().export() |
| 82 | + backupMixin(pageset, 'page') |
| 83 | + |
| 84 | + self.stdout.write(self.style.SUCCESS('[+] Successfully backup blog!')) |
| 85 | + else: |
| 86 | + self.stdout.write(self.style.WARNING('[-] Can not backup blog!')) |
0 commit comments