import boto3 import botocore import datetime import os import re import tarfile WIKI_PATH = "/var/www/dokuwiki" BACKUP_PATH = "~/backups" AWS_ACCESS_KEY = "your_access_key_here" AWS_SECRET_KEY = "your_secret_key_here" BUCKET_NAME = "your-bucket-name-here" BUCKET_KEY_PREFIX = "dokuwiki/" TARGET_DIRS = ['conf', 'data/attic', 'data/media', 'data/meta', 'data/pages'] dirs = [WIKI_PATH + '/' + d for d in TARGET_DIRS] datestring = re.sub('[^0-9]', '', datetime.datetime.now().isoformat()) filename = '{}/wiki-{}.tar.gz'.format(BACKUP_PATH, datestring) with tarfile.open(os.path.expanduser(filename), 'w:gz') as tar: for dir in dirs: tar.add(dir, arcname=os.path.basename(dir)) s3 = boto3.resource('s3') bucket = s3.Bucket(BUCKET_NAME) try: s3.Object(BUCKET_NAME, BUCKET_KEY_PREFIX + os.path.basename(filename)).put(Body=open(os.path.expanduser(filename), 'rb')) except botocore.exceptions.ClientError as e: error_code = int(e.response['Error']['Code']) print(error_code)