Last active
August 29, 2015 14:06
-
-
Save schakrava/85c691807c1d8cad8bdd to your computer and use it in GitHub Desktop.
Simple script to backup home directory to Rockstor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import requests | |
import json | |
import base64 | |
import sys | |
import subprocess | |
import datetime | |
RSYNC = '/usr/bin/rsync' | |
EXCLUDE = '/home/suman/backup.exclude' # file containing patterns of files and directories to exclude from being backed up. | |
SRC = '/home/suman/' # full path of backup source, possibly your home directory. | |
KEY = '8@jjiFWfQrfcfDpit@8ZcdM!T4e?6iWAV0uBlOTi' # get it from the web-ui (System -> Users -> Access Keys) | |
SECRET = 'VdIu!C5@TecMuVAHyaJrgs1Fmjn?00V-2NYe0guB;hZmKh;N7c;R1qGQ7AAh;KWEtjLfJf7jqrDVHs1MfLGsn=UZacJYgyS5JpNmCwGcT8oXx0r@:Q.sUq1FF9x3w:=a' # get it from same place as the KEY | |
HOST = '192.168.1.49' # ip address of your Rockstor box | |
SHARE = 'learnix_backup' # name of the share that hosts the backup | |
USER = 'backupuser' # user that owns the share | |
BASE_URL = 'https://%s/api/shares/%s/snapshots/' % (HOST, SHARE) | |
token_request_data = { | |
'grant_type': 'client_credentials', | |
'client_id': KEY, | |
'client_secret': SECRET, | |
} | |
user_pass = '{0}:{1}'.format(KEY, SECRET) | |
auth_string = base64.b64encode(user_pass.encode('utf-8')) | |
auth_headers = {'HTTP_AUTHORIZATION': 'Basic ' + auth_string.decode("utf-8"), } | |
def exec_cmd(cmd): | |
p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE) | |
out, err = p.communicate() | |
if (p.returncode != 0): | |
sys.exit('error executing command: %s. output: %s error: %s' % | |
(cmd, out, err)) | |
def snap_exists(snapshot, headers): | |
response = requests.get('%s/%s' % (BASE_URL, snapshot), | |
headers=headers, verify=False) | |
if (response.status_code == 200): | |
return True | |
return False | |
def delete_snap(snapshot, headers): | |
return requests.delete('%s/%s' % (BASE_URL, snapshot), | |
headers=headers, verify=False) | |
def api_headers(): | |
response = requests.post('https://%s/o/token/' % HOST, | |
data=token_request_data, headers=auth_headers, | |
verify=False) | |
content = json.loads(response.content.decode("utf-8")) | |
return {'Authorization': 'Bearer ' + content['access_token'], | |
'Content-type': 'application/json'} | |
def main(): | |
exec_cmd([RSYNC, '-azl', '--delete', | |
'--exclude-from=%s' % EXCLUDE, SRC, | |
'%s@%s:/%s/' % (USER, HOST, SHARE)]) | |
print 'Rsync Finished' | |
headers = api_headers() | |
snap_name = 'snapshot-%s' % datetime.datetime.today().strftime('%A') | |
if (snap_exists(snap_name, headers)): | |
delete_snap(snap_name, headers) | |
print 'Old snapshot: %s deleted' % snap_name | |
response = requests.post('%s/%s' % (BASE_URL, snap_name), | |
data=json.dumps({'uvisible': True, }), | |
headers=headers, verify=False) | |
if (response.status_code == 200): | |
print ('New Snapshot: %s created' % snap_name) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment