S3cmd: backing up a Mysql/MariaDB database
Installing and configuring S3cmd
To back up a Mysql or MariaDB database on "Leviia object storage", first install and configure S3cmd as described here.
Installing Mysqldump
Once configured, make sure you have installed mysqldump on your server.
You can do this by installing mysql-client on debian/ubuntu:
apt install mysql-client
Create a backup
Then all you have to do is run this command to make the backup:
mysqldump -h [db_hostname] -u [db_user] -p[db_passwd] --all-databases | s3cmd put - s3://[s3_bucketname]/[mysqldump_filename]
If you are root, the variables [db_hostname], [db_user] and [db_passwd] are not required.
For example, for us with a testmariadb bucket:
mysqldump --all-databases | s3cmd put - s3://testmariadb/db_$(date +"%m-%d-%Y_%H-%M-%S")
You can ensure that everything is operational with an ls:
# s3cmd ls s3://testmariadb
2023-05-14 13:49 2525083 s3://testmariadb/05-14-2023_15-49-13
You should now see your backups.
Automating backups with Cronjob
You can automate this task using a "cronjob".
Here's an example of a cronjob to back up your databases at 1 a.m. every day:
0 1 * * * mysqldump --all-databases | s3cmd put - s3://testmariadb/db_$(date +"%m-%d-%Y_%H-%M-%S")
Restoring a backup
To restore your database, simply perform the reverse operation.
Start by retrieving your backup:
s3cmd get s3://testmariadb/db_05-14-2023_15-58-10
Then use the "mysql" command with your backup to restore it.
mysql < db_05-14-2023_15-58-10
You should find your database as it was at the time of the backup.
Updated on: 05/01/2024
Thank you!