Please follow the below steps to setup backup of mysql database from server to Amazon s3.

  1. Login to AWS console, create IAM user, download the credentials and attach AmazonS3Full access policy to the IAM user.
  2. Install AWS CLI and configure by following the below commands.
    apt install awscli -y
    aws configure
  3. It will ask for Access Id and secret key. Provide the same which we downloaded in step-1 and proceed.
  4. Verify AWS S3 list by following the below command.
    aws s3 ls
  5. Install S3fs and Mount S3 Bucket by following the below commands.
    apt install s3fs
    echo ACCESS_KEY_ID:SECRET_ACCESS_KEY > /home/ubuntu/.s3fs- creds
  6. Your file will be saved at /home/ubuntu/.s3fs-creds.
  7. chmod 600 /home/ubuntu/.s3fs-creds
  8. Create a new folder from your home directory.
    mkdir /home/ubuntu/s3_uploads
  9. Create an Automation Backup Script for MySQLdump
    vim /home/ubuntu/.config.cnf
  10. Add the following below:
    [client]
    user = dbuser
    password = dbpassword
    host = localhost

    Save and close the file.
  11. create an automation backup script for MySQLdump
    vim /home/ubuntu/mysqldump.sh
  12. Add the following script
    !/bin/bash
    DBNAME=yourdbname
    BACKUP=/home/ubuntu/s3_uploads
    Backup date
    date=$(date +’%Y%m%d%H%M%S’);
    Start exec backup..
    mysqldump –defaults-extra-file=/home/ubuntu/.config.cnf $DBNAME > $BACKUP/mysqldump_$date.sql
  13. Save and close the file.
  14. To make sure the file is executable, run the below command.
    chmod +x /home/ubuntu/mysqldump.sh
  15. To automate your backup, use to command below to assign the mysql dump script on cron scheduling jobs.
    crontab -e
  16. If your first time to open the crontab, you can choose the vim editor on the screen and then add the following command to the bottom line.
    @daily sh /home/ubuntu/mysqldump.sh
  17. Save and close the file.
  18. Testing MySQLdump to S3 Bucket
  19. Go to the directory where mysqldump.sh is located and run the script.
    ./mysqldump.sh
  20. After the backups completes, open the mounted s3_uploads folder and type the below command.
    ls -lah
  21. On the screen, you can see the files which were uploaded.
  22. Verify the files on S3 Bucket Console.

Leave a Reply