sshtools module
- class sshtools.SSH(*args, **kwargs)[source]
Bases:
MountControl
This is a backend for the mount API
mount.MountControl
. This will mount the remote path withsshfs
, prepare the remote path and check that everything is set up correctly for Back In Time to run snapshots through SSH.This class will only mount the remote path. The real takeSnapshot process will use rsync over ssh. Other commands run remote over ssh.
- Parameters:
cfg (config.Config) – current config (handled by inherited
mount.MountControl
)user (str) – User name on remote host
host (str) – Name or IP Address of remote host
port (int) – Port used by SSHd on remote host
path (str) – remote path where snapshots are stored. Can be either relative from remote users homedir or an absolute path
cipher (str) – Cipher used to encrypt the network transfer
private_key_file (str) – Private key which is able to log on with Public/Private Key-Method on remote host
nice (bool) – use
nice -n 19
to run commands with low CPU priority on remote hostionice (bool) – use
ionice -c2 -n7
to run commands with low IO priority on remote hostnocache (bool) – use
nocache
to deactivate RAM caching of files on remote hostpassword (str) – password to unlock the private key
profile_id (str) – profile ID that should be used (handled by inherited
mount.MountControl
)hash_id (str) – crc32 hash used to identify identical mountpoints (handled by inherited
mount.MountControl
)tmp_mount (bool) – if
True
mount to a temporary destination (handled by inheritedmount.MountControl
)parent (QWidget) – parent widget for QDialogs or
None
if there is no parent (handled by inheritedmount.MountControl
)symlink (bool) – if
True
set symlink to mountpoint (handled by inheritedmount.MountControl
)mode (str) – one of
local
,local_encfs
,ssh
orssh_encfs
(handled by inheritedmount.MountControl
)hash_collision (int) – global value used to prevent hash collisions on mountpoints (handled by inherited
mount.MountControl
)
Note
All Arguments are optional. Default values will be fetched from
config.Config
. But after changing Settings we need to test the new values before storing them intoconfig.Config
. This is why all values will be added as arguments.- _mount()[source]
Backend mount method. This will call
sshfs
to mount the remote path.- Raises:
exceptions.MountException – if mount wasn’t successful
- benchmarkCipher(size=40)[source]
Rudimental benchmark to compare transfer speed of all available ciphers.
- Parameters:
size (int) – size of the testfile in MiB
- checkCipher()[source]
Try to login to remote host with the chosen cipher. This should make sure both localhost and the remote host support the chosen cipher.
- Raises:
exceptions.MountException – if login with the cipher failed
- checkKnownHosts()[source]
Check if the remote host is in current users
known_hosts
file.- Raises:
exceptions.KnownHost – if the remote host wasn’t found in
known_hosts
file
- checkLogin()[source]
Try to login to remote host with public/private-key-method (passwordless).
- Raises:
exceptions.NoPubKeyLogin – if login failed
- checkPingHost()[source]
Check if the remote host is online. Other than methods name may let suppose this does not use Ping (
ICMP
) but try to open a connection to the configured port on the remote host. In this way it will even work on remote hosts which haveICMP
disabled.If connection failed it will retry five times before failing.
- Raises:
exceptions.MountException – if connection failed most probably because remote host is offline
- checkRemoteCommands(retry=False)[source]
Try out all relevant commands used by Back In Time on the remote host to make sure snapshots will be successful with the remote host. This will also check that hard-links are supported on the remote host. This check can be disabled with
config.Config.sshCheckCommands()
- Parameters:
retry (bool) – retry to run the commands if it failed because the command string was to long
- Raises:
exceptions.MountException – if a command is not supported on remote host or if hard-links are not supported
- checkRemoteFolder()[source]
Check the remote path. If the remote path doesn’t exist this will create it. If it already exist this will check, that it is a folder and has correct permissions.
- Raises:
exceptions.MountException – if remote path couldn’t be created or doesn’t have correct permissions.
- preMountCheck(first_run=False)[source]
Check that everything is prepared and ready for successfully mount the remote path. Default is to run a light version of checks which will only make sure the remote host is online,
sshfs
is installed and the remote folder is available.After changing settings this should be run with
first_run = True
to run a full check with all tests.- Parameters:
first_run (bool) – run a full test with all checks
- Raises:
exceptions.MountException – if one test failed an we can not mount the remote path
- startSshAgent()[source]
Start a new
ssh-agent
if it is not already running.- Raises:
exceptions.MountException – if starting
ssh-agent
failed
- unlockSshAgent(force=False)[source]
Unlock the private key in
ssh-agent
which will provide it for all other commands. The password to unlock the key will be provided bybackintime-askpass
.- Parameters:
force (bool) – force to unlock the key by removing it first and add it again to make sure, the given values are correct
- Raises:
exceptions.MountException – if unlock failed
- sshtools.sshCopyId(pubkey, user, host, port='22', proxy_user=None, proxy_host=None, proxy_port=None, askPass='backintime-askpass', cipher=None)[source]
Copy SSH public key
pubkey
to remotehost
.
- sshtools.sshCopyIdCommand(pubkey, user, host, port='22', proxy_user=None, proxy_host=None, proxy_port='22', cipher=None)[source]
Generate a ssh-copy-id command to copy the given public ssh-key to a remote host.
- Parameters:
- Returns:
The ssh-copy-id command as a list.
- Return type:
- Raises:
FileNotFoundError – If public key file not exist.