Articles on: Object storage

S3cmd: back up databases on your Leviia object storage space (S3 compatible)

Prerequisites


Have created your identifier
Have configured and installed s3cmd


Command table


StepPostgreSQLMariaDBMySQL
Backuppg_dump -U <username> -d <database> > backup.sqlmysqldump -u <username> -p <database> > backup.sqlmysqldump -u <username> -p <database> > backup.sql
Send to S3s3cmd put backup.sql s3://<bucket-name>/backup.sqls3cmd put backup.sql s3://<bucket-name>/backup.sqls3cmd put backup.sql s3://<bucket-name>/backup.sql
S3 recoverys3cmd get s3://<bucket-name>/backup.sql backup.sqls3cmd get s3://<bucket-name>/backup.sql backup.sqls3cmd get s3://<bucket-name>/backup.sql backup.sql
psql -U <username> -d <database> < backup.sqlmysql -u <username> -p <database> < backup.sqlmysql -u <username> -p <database> < backup.sql



Script for automation


PostgreSQL


Backup and upload to S3
#!/bin/bash

# Variables to modify
username="your_username"
database="your_database"
bucket_name="your_bucket_name"

# Backup PostgreSQL database
pg_dump -U $username -d $database > backup.sql

# Send to S3
s3cmd put backup.sql s3://$bucket_name/backup.sql

Retrieve from S3 and restore
#!/bin/bash

# Variables to modify
bucket_name="your_bucket_name"
username="your_username"
database="your_database"

# Recovery from S3
s3cmd get s3://$bucket_name/backup.sql /tmp/backup.sql

# Restore PostgreSQL database
psql -U $username -d $database < /tmp/backup.sql


MariaDB


Backup and upload to S3
#!/bin/bash

# Variables to modify
username="your_username"
database="your_database"
bucket_name="your_bucket_name"

# Backup MariaDB database
mysqldump -u $username -p $database > backup.sql

# Send to S3
s3cmd put backup.sql s3://$bucket_name/backup.sql

# Delete backup file
rm backup.sql

Recover from S3 and restore
#!/bin/bash

# Variables to modify
bucket_name="your_bucket_name"
username="your_username"
database="your_database"

# Retrieve from S3
s3cmd get s3://$bucket_name/backup.sql /tmp/backup.sql

# Restore MariaDB database
mysql -u $username -p $database < /tmp/backup.sql

MySQL


Backup and upload to S3
#!/bin/bash

# Variables to modify
username="your_username"
database="your_database"
bucket_name="your_bucket_name"

# MySQL database backup
mysqldump -u $username -p $database > backup.sql

# Send to S3
s3cmd put backup.sql s3://$bucket_name/backup.sql

Retrieve from S3 and restore
#!/bin/bash

# Variables to modify
bucket_name="your_bucket_name"
username="your_username"
database="your_database"

# Retrieve from S3
s3cmd get s3://$bucket_name/backup.sql /tmp/backup.sql

# Restore MySQL database
mysql -u $username -p $database < /tmp/backup.sql

Updated on: 05/01/2024

Was this article helpful?

Share your feedback

Cancel

Thank you!