Commit bcf5c8e8 authored by Björn's avatar Björn

adding

parent a6a9522b
import getpass
#
#
#
......@@ -10,10 +12,112 @@ class Storagebox:
#
def __init__(self):
# define options
parser.add_argument('-c', '--config', help='full path to config-file')
# parse arguments
self._args = parser.parse_args()
if (self._args.config is None):
config = '.config'
else:
config = self._args.config
self._remote_port = self._config.get('default', 'remote_port', fallback='22')
self._namespace = platform.uname()[1])
#
# call subprocess
#
# @param command
#
def _command(self, command):
process = subprocess.run(command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
check=True,
shell=True)
return process
#
#
#
#
def _home(self):
if (getpass.getuser() == 'root'):
home = '/root/'
else:
home = '/home/' + getpass.getuser() + '/'
return home
#
#
#
#
def _clean(self):
authorized_keys_file = elf._home() + '/authorized_keys'
if os.path.exists(authorized_keys_file) and os.path.file(authorized_keys_file):
os.delete(authorized_keys_file)
def add(self):
#
# create ssh-key with current hostname
#
#
def _create_ssh_key(self):
ssh_file = self._home() + '/.ssh/backup_' + self._namespace
if not os.path.exists(ssh_file):
self._command("ssh-keygen -f " + ssh_file + " -t rsa -b 4096 -N ''") # generate ssh-key without passphrase
self._command("cat " + ssh_file + ".pub >> " + self._home() + "/authorized_keys") # authorize key
else:
print('ssh-key already exists!')
exit()
#
# create command for sftp
#
# @param command
#
def _sftp_command(self, command):
return "echo \"" + command + "\" | sftp -P " + self._remote_port + " " + self._config.get('default', 'remote_host')
#
# get authorized_keys from storagebox
#
#
def _download(self):
command = 'sftp -P ' + self._config.get('default', 'remote_port', 22) + ' ' + self._config.get('default', 'remote_host') + ':/.ssh/authorized_keys ' + self._home()
process = self._command(command)
if (process.stderr != ''):
print(process.stderr)
exit()
else:
print('download authorized_keys')
#
#
#
#
def _upload(self):
self._sftp_command("mkdir .ssh \n chmod 700 .ssh \n put " + self._home() + "/authorized_keys .ssh/authorized_keys \n chmod 600 .ssh/authorized_keys")
print('upload authorized_keys')
#
#
#
#
def run(self):
self._download()
self._create_ssh_key()
self._upload()
Storagebox()
storagebox = Storagebox()
storagebox.run()
......@@ -75,7 +75,7 @@ class Backup:
#
def _log(self, message):
if (self._args.sendmail == True):
self._logs += message
self._logs += message + '\n'
else:
print(message)
......@@ -109,10 +109,10 @@ class Backup:
else:
self._log('Error: "' + source + '" not found!')
# if includes is empty
if (includes == ''):
self._log('Error: no sources are found!')
self._log('No Backup! Exit Script!')
self._sendmail()
exit()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment