· 6 years ago · Aug 22, 2019, 11:38 AM
1import os
2import datetime
3import shutil
4import boto3
5from config import REGION, ENDPOINT, ACCESS_ID, SECRET_KEY, SPACE
6
7
8# DigitalOcean Spaces session setup
9session = boto3.session.Session()
10client = session.client('s3',
11 region_name=REGION,
12 endpoint_url=ENDPOINT,
13 aws_access_key_id=ACCESS_ID,
14 aws_secret_access_key=SECRET_KEY)
15
16
17# Generate datetime.now timestamp for backup file
18date = datetime.datetime.now().strftime("%d-%B-%Y-%H-%M")
19
20# Describe our backup filename
21file = 'example.com-www-backup-' + date
22
23# Path to directory for backup
24path = os.path.join(os.path.expanduser('~'), 'apps', 'example.com')
25
26# Destination path on DigitalOcean Space
27save_as = 'backups/example.com/' + file + '.tar.gz'
28
29shutil.make_archive(file, 'gztar', path) # Backup project directory
30client.upload_file(file + '.tar.gz', SPACE, save_as) # Upload backup to Spaces
31os.remove(file + '.tar.gz') # Remove temp backup archive
32
33# config.py
34
35# ACCESS_ID = 'DIGITALOCEAN SPACES ACCESS ID'
36# SECRET_KEY = 'DIGITALOCEAN SPACES SECRET KEY'
37# REGION = 'DIGITALOCEAN SPACES REGION',
38# ENDPOINT = 'DIGITALOCEAN SPACES ENDPOINT URL'
39# SPACE = 'DIGITALOCEAN SPACES SLUG'