From 78df76cd59ee7ae75a8e35fa0750ac0d76f8bd25 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Tue, 18 Dec 2012 19:58:50 -0500 Subject: [PATCH] Add backup code --- archiver/archive.py | 23 +++++++++++++++++++++++ archiver/song.py | 1 - 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/archiver/archive.py b/archiver/archive.py index f94a709..0bda85e 100644 --- a/archiver/archive.py +++ b/archiver/archive.py @@ -41,6 +41,11 @@ class Archive (models.Model): #And a reference to the songs in this archive songs = models.ManyToManyField(Song) + #Backup settings + backup_location = models.CharField(max_length = 255) + backup_frequency = models.IntegerField() + last_backup = models.DateTimeField() + def _scan_filesystem(self, progress_callback = lambda x: None): "Scan the archive's root filesystem and add any new songs" #This method is implemented since the other scan methods all need to use the same code @@ -136,3 +141,21 @@ class Archive (models.Model): #Make sure to add any new songs as well self._scan_filesystem() + def _needs_backup(self): + "Check if the current archive is due for a backup" + import datetime + + prev_backup_time = self.last_backup + current_time = datetime.datetime.now() + + delta = current_time - prev_backup_time + if delta > datetime.timedelta(seconds = self.backup_frequency): + return True + else: + return False + + def run_backup(self, force_backup = False): + "Backup the current archive" + if force_backup or self._needs_backup(): + import subprocess + subprocess.call(['rsync', '-av', self.root_folder, self.backup_location]) diff --git a/archiver/song.py b/archiver/song.py index 8a6d564..6144cba 100644 --- a/archiver/song.py +++ b/archiver/song.py @@ -78,4 +78,3 @@ class Song (models.Model): from Melodia.melodia_settings import HASH_FUNCTION as hash f = open(self.url, 'rb') self.file_hash = hash(f.read()) -