-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6b47519
commit 40ee146
Showing
9 changed files
with
174 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
AWS_ACCESS_KEY_ID=YOUR_AWS_ACCESS_KEY_ID | ||
AWS_SECRET_ACCESS_KEY=YOUR_AWS_SECRET_ACCESS_KEY | ||
AWS_DEFAULT_REGION=YOUR_AWS_DEFAULT_REGION |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import structlog | ||
from rest_framework.decorators import action | ||
from rest_framework.request import Request | ||
from rest_framework.response import Response | ||
from rest_framework.viewsets import GenericViewSet | ||
from housewatch.clickhouse import backups | ||
|
||
|
||
logger = structlog.get_logger(__name__) | ||
|
||
|
||
class BackupViewset(GenericViewSet): | ||
def list(self, request: Request) -> Response: | ||
cluster = request.query_params.get("cluster") | ||
return Response(backups.get_backups(cluster=cluster)) | ||
|
||
def retrieve(self, request: Request, pk: str) -> Response: | ||
cluster = request.query_params.get("cluster") | ||
return Response(backups.get_backup(pk, cluster=cluster)) | ||
|
||
@action(detail=True, methods=["post"]) | ||
def restore(self, request: Request, pk: str) -> Response: | ||
backups.restore_backup(pk) | ||
return Response() | ||
|
||
def create(self, request: Request) -> Response: | ||
database = request.data.get("database") | ||
table = request.data.get("table") | ||
bucket = request.data.get("bucket") | ||
path = request.data.get("path") | ||
if table: | ||
res = backups.create_table_backup(database, table, bucket, path) | ||
else: | ||
res = backups.create_database_backup(database, bucket, path) | ||
return Response(res) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from collections import defaultdict | ||
from housewatch.clickhouse.client import run_query | ||
|
||
from django.conf import settings | ||
|
||
|
||
def get_backups(cluster=None): | ||
if cluster: | ||
QUERY = """SELECT * FROM clusterAllReplicas(%(cluster)s, system.backups)""" | ||
else: | ||
QUERY = """SELECT * FROM system.backups""" | ||
res = run_query(QUERY, {"cluster": cluster}) | ||
return res | ||
|
||
|
||
def get_backup(backup, cluster=None): | ||
if cluster: | ||
QUERY = """Select * FROM clusterAllReplicas(%(cluster)s, system.backups) WHERE id = '%(uuid)s' """ | ||
return run_query(QUERY, {"cluster": cluster, "uuid": backup}) | ||
else: | ||
QUERY = """Select * FROM system.backups WHERE id = '%(uuid)s' """ | ||
return run_query(QUERY, {"uuid": backup}) | ||
|
||
|
||
def create_table_backup(database, table, bucket, path, aws_key=None, aws_secret=None): | ||
if aws_key is None or aws_secret is None: | ||
aws_key = settings.AWS_ACCESS_KEY_ID | ||
aws_secret = settings.AWS_SECRET_ACCESS_KEY | ||
QUERY = """BACKUP TABLE %(database)s.%(table)s | ||
TO S3('https://%(bucket)s.s3.amazonaws.com/%(path)s', '%(aws_key)s', '%(aws_secret)s') | ||
ASYNC""" | ||
return run_query( | ||
QUERY, | ||
{ | ||
"database": database, | ||
"table": table, | ||
"bucket": bucket, | ||
"path": path, | ||
"aws_key": aws_key, | ||
"aws_secret": aws_secret, | ||
}, | ||
) | ||
|
||
|
||
def create_database_backup(database, bucket, path, aws_key=None, aws_secret=None): | ||
if aws_key is None or aws_secret is None: | ||
aws_key = settings.AWS_ACCESS_KEY_ID | ||
aws_secret = settings.AWS_SECRET_ACCESS_KEY | ||
QUERY = """BACKUP DATABASE %(database)s | ||
TO S3('https://%(bucket)s.s3.amazonaws.com/%(path)s', '%(aws_key)s', '%(aws_secret)s') | ||
ASYNC""" | ||
return run_query( | ||
QUERY, | ||
{ | ||
"database": database, | ||
"bucket": bucket, | ||
"path": path, | ||
"aws_key": aws_key, | ||
"aws_secret": aws_secret, | ||
}, | ||
) | ||
|
||
|
||
def restore_backup(backup): | ||
pass |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
CREATE TABLE test_backup ( | ||
id UUID DEFAULT generateUUIDv4(), | ||
name String, | ||
timestamp DateTime DEFAULT now() | ||
) ENGINE = MergeTree() | ||
ORDER BY id; | ||
INSERT INTO test_backup (name) | ||
SELECT substring(toString(rand() * 1000000000), 1, 5) AS random_string | ||
FROM numbers(100); |
This file contains 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