Exemple #1
0
def get_backup_subdir():
    '''
        Get subdir name if its in the data directory.

        >>> from blockchain_backup.bitcoin.preferences import get_preferences, save_preferences
        >>> from blockchain_backup.bitcoin.tests import utils as test_utils
        >>> test_utils.init_database()
        >>> get_backup_subdir()
        'backups'
        >>> prefs = get_preferences()
        >>> prefs.backup_dir = '/tmp/bitcoin/backups'
        >>> save_preferences(prefs)
        >>> get_backup_subdir() is None
        True
    '''
    from blockchain_backup.bitcoin.preferences import get_backup_dir, get_data_dir

    data_dir = get_data_dir()
    backup_dir = get_backup_dir()

    # get the name of the subdirectory of the backup
    # if its in the data directory
    index = backup_dir.find(data_dir)
    if index >= 0:
        backup_subdir = backup_dir[index + len(data_dir):]
        if backup_subdir.startswith(os.sep):
            backup_subdir = backup_subdir[1:]
        if backup_subdir.endswith(os.sep):
            backup_subdir = backup_subdir[:-1]
    else:
        backup_subdir = None

    return backup_subdir
Exemple #2
0
def get_all_backup_dates_and_dirs(backup_dir=None):
    '''
        Get a list of backup dates with their parent directory.

        >>> from blockchain_backup.bitcoin.tests import utils as test_utils
        >>> test_utils.init_database()
        >>> backup_dates = get_all_backup_dates_and_dirs()
        >>> len(backup_dates) > 0
        True
        >>> backup_dates = get_all_backup_dates_and_dirs(backup_dir='/tmp/test')
        >>> backup_dates is None
        True
    '''
    def get_date_and_dir(entry):
        ''' Get the path and date if its a valid backup. '''

        backed_up_time = None

        if entry.is_dir() and entry.name.startswith(
                constants.BACKUPS_LEVEL_PREFIX):
            filenames = os.listdir(entry.path)
            for filename in filenames:
                # save the timestamp if there's a timestamp file
                if filename.startswith(constants.LAST_UPDATED_PREFIX):
                    backed_up_time = filename[len(constants.LAST_UPDATED_PREFIX
                                                  ):]

        return backed_up_time

    backup_dates_with_dirs = []

    if backup_dir is None:
        backup_dir = preferences.get_backup_dir()

    if os.path.exists(backup_dir):
        # scan the backup directories
        entries = os.scandir(backup_dir)
        for entry in entries:
            # look inside each backup level
            backed_up_time = get_date_and_dir(entry)
            if backed_up_time is not None:
                backup_dates_with_dirs.append((entry.path, backed_up_time))

        if not backup_dates_with_dirs:
            log(f'no completed backups in {backup_dir}')
    else:
        backup_dates_with_dirs = None
        log(f'no backup dir in {backup_dir} so no backup dates')

    return backup_dates_with_dirs
Exemple #3
0
    def get_page(self, request):
        # let's see what we know about the environment

        log('getting home page')

        clear_action_updates()

        # last block in django database; may be different from last in blockchain
        last_block_updated = state.get_last_block_updated()
        # ready if blockchain-backup has processed some blockchain data
        bcb_run_already = last_block_updated > 0
        bin_dir_ok = preferences.bin_dir_ok()
        data_dir = preferences.get_data_dir()
        backup_dir_ok, backup_dir_error = preferences.backup_dir_ok()
        backup_dir = preferences.get_backup_dir()
        last_bcb_version = state.get_latest_bcb_version()
        current_core_version = bitcoin_utils.get_bitcoin_version()
        last_core_version = state.get_latest_core_version()

        if bcb_run_already:
            data_dir_ok, __ = preferences.data_dir_ok()
        else:
            if data_dir and os.path.exists(data_dir):
                data_dir_ok, __ = preferences.data_dir_ok()
            else:
                data_dir_ok = False

        bitcoin_utils.check_for_updates()

        params = {
            'data_dir': data_dir,
            'data_dir_ok': data_dir_ok,
            'backup_dir': backup_dir,
            'backup_dir_ok': backup_dir_ok,
            'backup_dir_error': backup_dir_error,
            'last_bcb_version': last_bcb_version,
            'bcb_up_to_date': last_bcb_version >= BLOCKCHAIN_BACKUP_VERSION,
            'last_core_version': last_core_version,
            'core_up_to_date': last_core_version >= current_core_version,
            'need_backup': bitcoin_utils.get_next_backup_time() < now(),
            'last_backed_up_time': state.get_last_backed_up_time(),
        }
        #log('params: {}'.format(params))

        response = get_home_page_response(request, bcb_run_already, bin_dir_ok,
                                          params)

        return response
Exemple #4
0
    def get_backup_dir_and_time(self):
        '''
            Get the oldest backup dir or the one
            that's only been partially backed up.

            >>> from blockchain_backup.bitcoin.tests import utils as test_utils
            >>> test_utils.init_database()
            >>> backup_task = BackupTask()
            >>> backup_task.manager = BitcoinManager(backup_task.log_name)
            >>> backup_dirname, oldest_backup_time = backup_task.get_backup_dir_and_time()
            >>> backup_dirname.startswith('/')
            True
            >>> isinstance(oldest_backup_time, datetime)
            True
        '''
        self.log('getting low level backup dir')

        backup_dirname = None
        oldest_backed_up_time = now() + timedelta(days=1)

        backup_dir = get_backup_dir()
        if os.path.exists(backup_dir):
            backup_dirname, oldest_backed_up_time = self.search_entries(
                backup_dir, oldest_backed_up_time)

        else:
            self.log(f'creating new backup parent: {backup_dir}')
            os.makedirs(backup_dir)
            backup_dirname = os.path.join(
                backup_dir, '{}{}'.format(constants.BACKUPS_LEVEL_PREFIX, '1'))
            oldest_backed_up_time = now()
            self.log('never backed up')

        i = backup_dirname.find(constants.BACKUPS_LEVEL_PREFIX)
        if i > 0:
            self.backup_level = int(
                backup_dirname[i + len(constants.BACKUPS_LEVEL_PREFIX):])

        self.log(f'backup dirname: {backup_dirname}')
        self.log(f'oldest_backed_up_time: {oldest_backed_up_time}')
        return backup_dirname, oldest_backed_up_time