Skip to content

Commit

Permalink
minor update
Browse files Browse the repository at this point in the history
  • Loading branch information
eiparfenov committed Jun 10, 2024
1 parent ee999fa commit 7cffee2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
19 changes: 11 additions & 8 deletions PgDump/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,26 @@ public async Task Execute(IJobExecutionContext context)
.WithArguments(["--host", host, "--port", port, "--dbname", dbname, "-U", user, "-f", "dump.sql"])
.WithEnvironmentVariables(new Dictionary<string, string?>(){{"PGPASSWORD", password}})
.ExecuteAsync();
var (url, accessKeyId, secretAccessKey, bucketName) = GetS3Config();
var (url, accessKeyId, secretAccessKey, bucketName, pathPrefix) = GetS3Config();
using var s3Client = new AmazonS3Client(accessKeyId, secretAccessKey, new AmazonS3Config()
{
ServiceURL = url,
ForcePathStyle = true
});
var key = $"{pathPrefix.TrimEnd('/')}/dump_{TimeProvider.System.GetUtcNow() + TimeSpan.FromHours(3):u}.sql";
await s3Client.PutObjectAsync(new PutObjectRequest()
{
InputStream = new FileStream("dump.sql", FileMode.Open),
BucketName = bucketName,
Key = $"pg_dump/{dbname}/dump_{TimeProvider.System.GetUtcNow() + TimeSpan.FromHours(3):u}.sql"
Key = key
});

await s3Client.PutObjectAsync(new PutObjectRequest()
await s3Client.CopyObjectAsync(new CopyObjectRequest()
{
InputStream = new FileStream("dump.sql", FileMode.Open),
BucketName = bucketName,
Key = $"pg_dump/{dbname}/latest.sql"
DestinationBucket = bucketName,
SourceBucket = bucketName,
SourceKey = key,
DestinationKey = $"{pathPrefix.TrimEnd('/')}/latest.sql"
});
}
(string host, string port, string dbname, string user, string password) ParsePostgresConnectionString()
Expand All @@ -89,13 +91,14 @@ await s3Client.PutObjectAsync(new PutObjectRequest()
parsedConnectionString["password"]
);
}
(string url, string accessKeyId, string secretAccessKey, string bucketName) GetS3Config()
(string url, string accessKeyId, string secretAccessKey, string bucketName, string pathPrefix) GetS3Config()
{
var url = Environment.GetEnvironmentVariable("S3StorageOptions__ServiceUrl")!;
var accessKeyId = Environment.GetEnvironmentVariable("S3StorageOptions__AccessKeyId")!;
var secretAccessKey = Environment.GetEnvironmentVariable("S3StorageOptions__SecretAccessKey")!;
var bucketName = Environment.GetEnvironmentVariable("S3StorageOptions__BucketName")!;
return (url, accessKeyId, secretAccessKey, bucketName);
var pathPrefix = Environment.GetEnvironmentVariable("S3StorageOptions__PathPrefix")!;
return (url, accessKeyId, secretAccessKey, bucketName, pathPrefix);
}
}

18 changes: 18 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
services:
pgdump:
image: 'pg_dump'
environment:
ConnectionStrings__Postgres: "Host=db;Port=5432;Database=minobr;Username=postgres;Password=password"
S3StorageOptions__ServiceUrl: "https://storage.yandexcloud.net"
S3StorageOptions__AccessKeyId: "YCAJEr0_B5ceJcFzMuXYM1lfx"
S3StorageOptions__SecretAccessKey: "YCO1tHMh0pnL_ufC3rsnNzTnaKBqByVTeXyCD82-"
S3StorageOptions__BucketName: "nodots"
S3StorageOptions__PathPrefix: "pgdump/minobr/dev"
S3StorageOptions__ForcePathStyle: false
ScheduleCron: "0 0/1 * * * ?"
db:
image: 'postgres:16.2'
ports:
- 5431:5432
environment:
POSTGRES_PASSWORD: 'password'

0 comments on commit 7cffee2

Please # to comment.