-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.py
More file actions
70 lines (50 loc) · 1.66 KB
/
Copy pathhandler.py
File metadata and controls
70 lines (50 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import os
import paramiko
ssh_key = os.environ.get('ssh_key')
hostname = os.environ.get('hostname')
username = os.environ.get('username')
backup_command = os.environ.get('backup_command')
if False:
try:
from dev_secrets import *
except ModuleNotFoundError:
pass
printer = lambda msg : print('=' * 10, msg)
def connect_ssh():
ssh_key_file_path = '/tmp/mykey.ssh'
if os.path.exists(ssh_key_file_path):
os.remove(ssh_key_file_path)
with open(ssh_key_file_path, 'w') as f:
f.write(ssh_key)
if not os.path.exists(ssh_key_file_path):
printer('could NOT write SSH key')
return {'statusCode': 500, 'body': 'failed, could NOT write SSH key'}
ssh = paramiko.SSHClient()
cert = paramiko.RSAKey.from_private_key_file(ssh_key_file_path)
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=hostname, username=username, pkey=cert)
return ssh
def create_backup_on_server_and_copy_it(ssh):
printer('running: ' + backup_command)
stdin, stdout, errors = ssh.exec_command(backup_command)
printer('outputs')
stdout.channel.recv_exit_status()
for line in stdout.readlines():
print(line)
printer('errors')
errors.channel.recv_exit_status()
lines = errors.readlines()
for line in lines:
print(line)
def main(event, context):
printer('connecting ssh...')
ssh = connect_ssh()
printer('creating backup...')
create_backup_on_server_and_copy_it(ssh)
printer('closing ssh...')
ssh.close()
response = {
'statusCode': 200,
'body': 'Successfully uploaded backup'
}
return response