def list_dir(sftp: pysftp.Connection): """sftp.listdir() returns a list containing the names of files in the current (".") directory. Note: It does not print the list, so we must return the list to main for that to be done. """ for entry in sftp.listdir("."): if not entry.startswith('.'): print(entry, end=' ') print()
class Storage(BaseStorage): """ SFTP Storage """ name = 'SFTP' SFTP_HOST = getattr(settings, 'DBBACKUP_SFTP_HOST', None) SFTP_USER = getattr(settings, 'DBBACKUP_SFTP_USER', None) SFTP_PASSWORD = getattr(settings, 'DBBACKUP_SFTP_PASSWORD', None) SFTP_PATH = getattr(settings, 'DBBACKUP_SFTP_PATH', ".") SFTP_PATH = '/%s/' % SFTP_PATH.strip('/') SFTP_PASSIVE_MODE = getattr(settings, 'DBBACKUP_SFTP_PASSIVE_MODE', False) def __init__(self, server_name=None): self._check_settings() self.sftp = Connection( host = self.SFTP_HOST, username = self.SFTP_USER, password = self.SFTP_PASSWORD) def _check_settings(self): """ Check we have all the required settings defined. """ if not self.SFTP_HOST: raise StorageError('%s storage requires DBBACKUP_SFTP_HOST to be defined in settings.' % self.name) ################################### # DBBackup Storage Methods ################################### @property def backup_dir(self): return self.SFTP_PATH def delete_file(self, filepath): """ Delete the specified filepath. """ self.sftp.remove(filepath) def list_directory(self, raw=False): """ List all stored backups for the specified. """ return sorted(self.sftp.listdir(self.SFTP_PATH)) def write_file(self, filehandle, filename): """ Write the specified file. """ filehandle.seek(0) backuppath = os.path.join(self.SFTP_PATH, filename) self.sftp.putfo(filehandle, backuppath) def read_file(self, filepath): """ Read the specified file and return it's handle. """ outputfile = tempfile.SpooledTemporaryFile( max_size=10 * 1024 * 1024, dir=dbbackup_settings.TMP_DIR) self.sftp.getfo(filepath, outputfile) return outputfile
class Storage(BaseStorage): """ SFTP Storage """ name = 'SFTP' SFTP_HOST = getattr(settings, 'DBBACKUP_SFTP_HOST', None) SFTP_USER = getattr(settings, 'DBBACKUP_SFTP_USER', None) SFTP_PASSWORD = getattr(settings, 'DBBACKUP_SFTP_PASSWORD', None) SFTP_PATH = getattr(settings, 'DBBACKUP_SFTP_PATH', ".") SFTP_PATH = '/%s/' % SFTP_PATH.strip('/') SFTP_PASSIVE_MODE = getattr(settings, 'DBBACKUP_SFTP_PASSIVE_MODE', False) def __init__(self, server_name=None): self._check_settings() self.sftp = Connection(host=self.SFTP_HOST, username=self.SFTP_USER, password=self.SFTP_PASSWORD) def _check_settings(self): """ Check we have all the required settings defined. """ if not self.SFTP_HOST: raise StorageError( '%s storage requires DBBACKUP_SFTP_HOST to be defined in settings.' % self.name) ################################### # DBBackup Storage Methods ################################### @property def backup_dir(self): return self.SFTP_PATH def delete_file(self, filepath): """ Delete the specified filepath. """ self.sftp.remove(filepath) def list_directory(self, raw=False): """ List all stored backups for the specified. """ return sorted(self.sftp.listdir(self.SFTP_PATH)) def write_file(self, filehandle, filename): """ Write the specified file. """ filehandle.seek(0) backuppath = os.path.join(self.SFTP_PATH, filename) self.sftp.putfo(filehandle, backuppath) def read_file(self, filepath): """ Read the specified file and return it's handle. """ outputfile = tempfile.SpooledTemporaryFile( max_size=10 * 1024 * 1024, dir=dbbackup_settings.TMP_DIR) self.sftp.getfo(filepath, outputfile) return outputfile
class SFTP: def __init__(self, sftp_server_config): self.server = sftp_server_config['host_address'] self.username = sftp_server_config['user_name'] self.private_key = sftp_server_config['key_path'] self.sftp_folder = sftp_server_config['sftp_folder'] self.connection_opts = CnOpts() self.connection_opts.hostkeys = None self.__connection: Connection = None def connect(self): if not self.__connection: self.__connection = Connection(self.server, self.username, self.private_key, cnopts=self.connection_opts) return self def put(self, file, remote_path): return self.__connection.put(file, remote_path) def remove(self, path): return self.__connection.remove(path) def rmdir(self, path): return self.__connection.rmdir(path) def listdir(self, path): return self.__connection.listdir(path) def is_dir(self, path): return self.__connection.isdir(path) def is_file(self, path): return self.__connection.isfile(path) def close(self): self.__connection.close() self.__connection = None
def _download_them(self, _sftp: pysftp.Connection): """ This method does the hard work downloading files. It also preserves the modified time after downloaded from the SFTP host. The call back lambda function prints the % byes downloaded, which is based on the size of the file. Parameters ---------- _sftp : pysftp.Connection a sftp connection object """ entries = _sftp.listdir(self._srcDir) for entry in entries: if _sftp.isfile(entry) and self._is_file_asked(entry): _sftp.get(os.path.join(self._srcDir, entry), os.path.join(self._destDir, entry), callback=lambda transfered, size: audit_params(operation=Sc.OPERATION_DOWNLOAD, status=Sc.STATUS_COMPLETE, comments="{} {} ({})%".format('>>-->', entry, str("%.2f" % (100*(int(transfered)/int(size)))))), preserve_mtime=True)
def get_remote_filelist(conn: pysftp.Connection) -> list: return conn.listdir()