# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- # # Copyright 2017 Tomas Vondra (Launchpad id: tomas-v) # Copyright 2017 Kenneth Loafman <kenneth@loafman.com> # # This file is part of duplicity. # # Duplicity is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the # Free Software Foundation; either version 2 of the License, or (at your # option) any later version. # # Duplicity is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with duplicity; if not, write to the Free Software Foundation, # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA from __future__ import print_function from future import standard_library standard_library.install_aliases() from builtins import str from builtins import range from builtins import object from duplicity import log from duplicity import util from duplicity.errors import BackendException import duplicity.backend import os import subprocess class MegaBackend(duplicity.backend.Backend): u"""Connect to remote store using Mega.co.nz API""" def __init__(self, parsed_url): duplicity.backend.Backend.__init__(self, parsed_url) # ensure all the necessary megatools binaries exist self._check_binary_exists(u'megals') self._check_binary_exists(u'megamkdir') self._check_binary_exists(u'megaget') self._check_binary_exists(u'megaput') self._check_binary_exists(u'megarm') # store some basic info self._hostname = parsed_url.hostname if parsed_url.password is None: self._megarc = os.getenv(u'HOME') + u'/.megarc' else: self._megarc = False self._username = parsed_url.username self._password = self.get_password() # remote folder (Can we assume /Root prefix?) self._root = u'/Root' self._folder = self._root + u'/' + parsed_url.path[1:] # make sure the remote folder exists (the whole path) self._makedir_recursive(parsed_url.path[1:].split(u'/')) def _check_binary_exists(self, cmd): u'checks that a specified command exists in the current path' try: # ignore the output, we only need the return code subprocess.check_output([u'which', cmd]) except Exception as e: raise BackendException(u"command '%s' not found, make sure megatools are installed" % (cmd,)) def _makedir(self, path): u'creates a remote directory' if self._megarc: cmd = [u'megamkdir', u'--config', self._megarc, path] else: cmd = [u'megamkdir', u'-u', self._username, u'-p', self._password, path] self.subprocess_popen(cmd) def _makedir_recursive(self, path): u'creates a remote directory (recursively the whole path), ingores errors' print(u"mkdir: %s" % (u'/'.join(path),)) p = self._root for folder in path: p = p + u'/' + folder try: self._makedir(p) except: pass def _put(self, source_path, remote_filename): u'uploads file to Mega (deletes it first, to ensure it does not exist)' try: self.delete(remote_filename) except Exception: pass self.upload(local_file=source_path.get_canonical(), remote_file=remote_filename) def _get(self, remote_filename, local_path): u'downloads file from Mega' self.download(remote_file=remote_filename, local_file=local_path.name) def _list(self): u'list files in the backup folder' return self.folder_contents(files_only=True) def _delete(self, filename): u'deletes remote ' self.delete(remote_file=filename) def folder_contents(self, files_only=False): u'lists contents of a folder, optionally ignoring subdirectories' print(u"megals: %s" % (self._folder,)) if self._megarc: cmd = [u'megals', u'--config', self._megarc, self._folder] else: cmd = [u'megals', u'-u', self._username, u'-p', self._password, self._folder] files = subprocess.check_output(cmd) files = files.strip().split(u'\n') # remove the folder name, including the path separator files = [f[len(self._folder) + 1:] for f in files] # optionally ignore entries containing path separator (i.e. not files) if files_only: files = [f for f in files if u'/' not in f] return [util.fsencode(f) for f in files] def download(self, remote_file, local_file): print(u"megaget: %s" % (remote_file,)) if self._megarc: cmd = [u'megaget', u'--config', self._megarc, u'--no-progress', u'--path', local_file, self._folder + u'/' + remote_file] else: cmd = [u'megaget', u'-u', self._username, u'-p', self._password, u'--no-progress', u'--path', local_file, self._folder + u'/' + remote_file] self.subprocess_popen(cmd) def upload(self, local_file, remote_file): print(u"megaput: %s" % (remote_file,)) if self._megarc: cmd = [u'megaput', u'--config', self._megarc, u'--no-progress', u'--path', self._folder + u'/' + remote_file, local_file] else: cmd = [u'megaput', u'-u', self._username, u'-p', self._password, u'--no-progress', u'--path', self._folder + u'/' + remote_file, local_file] self.subprocess_popen(cmd) def delete(self, remote_file): print(u"megarm: %s" % (remote_file,)) if self._megarc: cmd = [u'megarm', u'--config', self._megarc, self._folder + u'/' + remote_file] else: cmd = [u'megarm', u'-u', self._username, u'-p', self._password, self._folder + u'/' + remote_file] self.subprocess_popen(cmd) duplicity.backend.register_backend(u'mega', MegaBackend) duplicity.backend.uses_netloc.extend([u'mega'])
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
__pycache__ | Folder | 0755 |
|
|
pyrax_identity | Folder | 0755 |
|
|
__init__.py | File | 1.07 KB | 0644 |
|
_boto_multi.py | File | 9.52 KB | 0644 |
|
_boto_single.py | File | 14.07 KB | 0644 |
|
_cf_cloudfiles.py | File | 3.81 KB | 0644 |
|
_cf_pyrax.py | File | 5.07 KB | 0644 |
|
adbackend.py | File | 16.97 KB | 0644 |
|
azurebackend.py | File | 7.91 KB | 0644 |
|
b2backend.py | File | 6.77 KB | 0644 |
|
cfbackend.py | File | 1.11 KB | 0644 |
|
dpbxbackend.py | File | 19.86 KB | 0644 |
|
gdocsbackend.py | File | 9.12 KB | 0644 |
|
giobackend.py | File | 8.25 KB | 0644 |
|
hsibackend.py | File | 2.67 KB | 0644 |
|
hubicbackend.py | File | 2.37 KB | 0644 |
|
imapbackend.py | File | 9.83 KB | 0644 |
|
jottacloudbackend.py | File | 5.53 KB | 0644 |
|
lftpbackend.py | File | 9.79 KB | 0644 |
|
localbackend.py | File | 2.65 KB | 0644 |
|
mediafirebackend.py | File | 4.67 KB | 0644 |
|
megabackend.py | File | 6.18 KB | 0644 |
|
multibackend.py | File | 13.84 KB | 0644 |
|
ncftpbackend.py | File | 5.56 KB | 0644 |
|
onedrivebackend.py | File | 13.25 KB | 0644 |
|
par2backend.py | File | 8.14 KB | 0644 |
|
pcabackend.py | File | 8.96 KB | 0644 |
|
pydrivebackend.py | File | 10.68 KB | 0644 |
|
rclonebackend.py | File | 4.16 KB | 0644 |
|
rsyncbackend.py | File | 6.44 KB | 0644 |
|
s3_boto3_backend.py | File | 8.81 KB | 0644 |
|
s3_boto_backend.py | File | 1.48 KB | 0644 |
|
ssh_paramiko_backend.py | File | 18.33 KB | 0644 |
|
ssh_pexpect_backend.py | File | 12.63 KB | 0644 |
|
swiftbackend.py | File | 7.5 KB | 0644 |
|
sxbackend.py | File | 2.3 KB | 0644 |
|
tahoebackend.py | File | 2.61 KB | 0644 |
|
webdavbackend.py | File | 19.73 KB | 0644 |
|