Skip to content

Commit 728125d

Browse files
authored
Added ability to specify a subfolder in the GCS bucket (#264)
1 parent 1e14e49 commit 728125d

File tree

7 files changed

+18
-7
lines changed

7 files changed

+18
-7
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ RUN pip3 --no-cache-dir install .
1818

1919
RUN chown -R 1337:1337 /opt/grafana-backup-tool
2020
USER 1337
21-
CMD sh -c 'if [ "$RESTORE" = true ]; then if [ ! -z "$AWS_S3_BUCKET_NAME" ] || [ ! -z "$AZURE_STORAGE_CONTAINER_NAME" ]; then grafana-backup restore $ARCHIVE_FILE; else grafana-backup restore _OUTPUT_/$ARCHIVE_FILE; fi else grafana-backup save; fi'
21+
CMD sh -c 'if [ "$RESTORE" = true ]; then if [ ! -z "$AWS_S3_BUCKET_NAME" ] || [ ! -z "$AZURE_STORAGE_CONTAINER_NAME" ] || [ ! -z "$GCS_BUCKET_NAME" ]; then grafana-backup restore $ARCHIVE_FILE; else grafana-backup restore _OUTPUT_/$ARCHIVE_FILE; fi else grafana-backup save; fi'

DockerfileSlim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ RUN echo "@edge http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk
3737
&& apk del build-deps
3838

3939
USER ${UID}
40-
CMD sh -c 'if [ "$RESTORE" = true ]; then if [ ! -z "$AWS_S3_BUCKET_NAME" ] || [ ! -z "$AZURE_STORAGE_CONTAINER_NAME" ]; then grafana-backup restore $ARCHIVE_FILE; else grafana-backup restore _OUTPUT_/$ARCHIVE_FILE; fi else grafana-backup save; fi'
40+
CMD sh -c 'if [ "$RESTORE" = true ]; then if [ ! -z "$AWS_S3_BUCKET_NAME" ] || [ ! -z "$AZURE_STORAGE_CONTAINER_NAME" ] || [ ! -z "$GCS_BUCKET_NAME" ]; then grafana-backup restore $ARCHIVE_FILE; else grafana-backup restore _OUTPUT_/$ARCHIVE_FILE; fi else grafana-backup save; fi'

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ docker run --user $(id -u):$(id -g) --rm --name grafana-backup-tool \
173173

174174
***GCS Example:*** Set GCS configurations in `-e` or `grafanaSettings.json`([example](https://github.com/ysde/grafana-backup-tool/blob/master/examples/grafana-backup.example.json))
175175
```
176-
-e GCS_BUCKET_NAME="bucket-name" \
176+
-e GCS_BUCKET_NAME="backups-bucket-name" \
177+
-e GCS_BUCKET_PATH="grafana-backup-folder" \
177178
-e GCLOUD_PROJECT="gcp-project-name" \
178179
-e GOOGLE_APPLICATION_CREDENTIALS="credential-file-path"
179180
```

examples/grafanaSettings.example.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
},
2626
"gcp": {
2727
"gcs_bucket_name": "bucket_name",
28+
"gcs_bucket_path": "grafana-backup",
2829
"google_application_credentials": "google_credential_file_path"
2930
},
3031
"influxdb": {

grafana_backup/gcs_download.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ def main(args, settings):
77
arg_archive_file = args.get('<archive_file>', None)
88

99
bucket_name = settings.get('GCS_BUCKET_NAME')
10+
bucket_path = settings.get('GCS_BUCKET_PATH').strip('/')
1011

1112
storage_client = storage.Client()
13+
14+
gcs_blob_name = arg_archive_file if bucket_path == '' else '{0}/{1}'.format(bucket_path, arg_archive_file)
15+
1216
bucket = storage_client.bucket(bucket_name)
1317

14-
blob = bucket.blob(arg_archive_file)
18+
blob = bucket.blob(gcs_blob_name)
1519

1620
try:
1721
gcs_data = io.BytesIO(blob.download_as_bytes())
@@ -23,7 +27,7 @@ def main(args, settings):
2327
print("Permission denied: {0}, please grant `Storage Admin` to service account you used".format(str(e)))
2428
return False
2529
except api_core.exceptions.NotFound:
26-
print("The file: {0} or gcs bucket: {1} doesn't exist".format(arg_archive_file, bucket_name))
30+
print("The file: {0} or gcs bucket: {1} doesn't exist".format(gcs_blob_name, bucket_name))
2731
return False
2832
except Exception as e:
2933
print("Exception: {0}".format(str(e)))

grafana_backup/gcs_upload.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,25 @@
44

55
def main(args, settings):
66
bucket_name = settings.get('GCS_BUCKET_NAME')
7+
bucket_path = settings.get('GCS_BUCKET_PATH').strip('/')
78
backup_dir = settings.get('BACKUP_DIR')
89
timestamp = settings.get('TIMESTAMP')
910

1011
storage_client = storage.Client()
1112

1213
gcs_file_name = '{0}.tar.gz'.format(timestamp)
1314
archive_file = '{0}/{1}'.format(backup_dir, gcs_file_name)
15+
gcs_blob_name = gcs_file_name if bucket_path == '' else '{0}/{1}'.format(bucket_path, gcs_file_name)
1416

1517
try:
1618
bucket = storage_client.bucket(bucket_name)
1719

18-
blob = bucket.blob(gcs_file_name)
20+
blob = bucket.blob(gcs_blob_name)
1921
blob.upload_from_filename(archive_file)
2022

2123
print("Upload to gcs: was successful")
2224
except FileNotFoundError: # noqa: F821
23-
print("The file: {0} was not found".format(gcs_file_name))
25+
print("The file: {0} was not found".format(archive_file))
2426
return False
2527
except api_core.exceptions.Forbidden as e:
2628
print("Permission denied: {0}, please grant `Storage Admin` to service account you used".format(str(e)))

grafana_backup/grafanaSettings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def main(config_path):
4343
# Cloud storage settings - GCP
4444
gcp_config = config.get('gcp', {})
4545
gcs_bucket_name = gcp_config.get('gcs_bucket_name', '')
46+
gcs_bucket_path = gcp_config.get('gcs_bucket_path', '')
4647
google_application_credentials = gcp_config.get('google_application_credentials', '')
4748

4849
influxdb_measurement = config.get('influxdb', {}).get('measurement', 'grafana_backup')
@@ -72,6 +73,7 @@ def main(config_path):
7273
AZURE_STORAGE_CONNECTION_STRING = os.getenv('AZURE_STORAGE_CONNECTION_STRING', azure_storage_connection_string)
7374

7475
GCS_BUCKET_NAME = os.getenv('GCS_BUCKET_NAME', gcs_bucket_name)
76+
GCS_BUCKET_PATH = os.getenv('GCS_BUCKET_PATH', gcs_bucket_path)
7577
if not os.getenv('GOOGLE_APPLICATION_CREDENTIALS') and google_application_credentials:
7678
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = google_application_credentials
7779

@@ -177,6 +179,7 @@ def main(config_path):
177179
config_dict['AZURE_STORAGE_CONTAINER_NAME'] = AZURE_STORAGE_CONTAINER_NAME
178180
config_dict['AZURE_STORAGE_CONNECTION_STRING'] = AZURE_STORAGE_CONNECTION_STRING
179181
config_dict['GCS_BUCKET_NAME'] = GCS_BUCKET_NAME
182+
config_dict['GCS_BUCKET_PATH'] = GCS_BUCKET_PATH
180183
config_dict['INFLUXDB_MEASUREMENT'] = INFLUXDB_MEASUREMENT
181184
config_dict['INFLUXDB_HOST'] = INFLUXDB_HOST
182185
config_dict['INFLUXDB_PORT'] = INFLUXDB_PORT

0 commit comments

Comments
 (0)