Alur untuk backup otomatis database langsung ke S3 services, salah satunya ke DigitalOcean (DO) Spaces
Integrasi s3cmd
Download sesuai platform dari https://s3tools.org/s3cmd kemudian jalankan comman untuk konfigurasi :
s3cmd --configure
Lengkapi credentials sehingga dapat terhubung dengan S3 service
Script untuk Dump and Push
Buat sebuah file dengan nama do.sh
berisi perintah berikut (sesuaikan variabel)
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
#!/bin/bash | |
DB_HOST={{db_name}} | |
DB_NAME={{db_name}} | |
DB_USER={{db_user}} | |
DB_PASS={{db_pass}} | |
S3_PATH=path/to/app/ | |
FILE_NAME=mysql-$(date +%Y%m%d-%H_%M_%S) | |
echo "dump db..." | |
mysqldump -h $DB_HOST -u $DB_USER -p$DB_PASS $DB_NAME > $FILE_NAME.sql | |
echo "compress..." | |
tar -czvf $FILE_NAME.tar.gz $FILE_NAME.sql | |
echo "put to s3..." | |
s3cmd put $FILE_NAME.tar.gz s3://$S3_PATH | |
echo "clean up..." | |
rm -f $FILE_NAME* |
Atur Penjadwalan
Buka crontab mode edit dan tambahkan command berikut (waktu bisa disesuaikan) :
0 0 * * * /bin/sh ~/do.sh