
MySQL databases need timely backups to keep recovery data available in case of unplanned database server failures or data corruption events. Available MySQL database backup strategies include SQL statement recreation with tools such as mysqldump and mysqlbackup, replication, and point-in-time recovery methods such as binary logging. Cloud Storage such as object storage decentralizes your MySQL database backups for smooth data recoveries in case the original database or server fails.
This guide explains how to automatically back up MySQL databases with object storage using mysqldump and Cron utilities on a Vultr Linux server. You will schedule weekly database backup operations and ensure the existence of uploaded files to support your database recovery methods.
Deploy Vultr Block Storage.
Deploy a fresh Vultr Debian Server to use as the automation system.
Access the server using SSH and log in as a non-root user with sudo privileges.
Install the MySQL or MariaDB database server package.
This guide uses a Vultr Debian server as the automation system for testing purposes. Ensure to use your existing MySQL database server to automate database backup operations with Vultr Object Storage.
MySQL Databases require special access privileges such as SELECT to enable successful backups and recoveries. Follow the steps below to create a dedicated MySQL backup user with all necessary privileges to perform backups on the database server.
Log in to your MySQL database server as an administrator.
Create a new sample database exampledb.
Create another sample database sampledb.
Create a new MySQL user with a strong password to perform backup operations on the server. For example, exampleuser.
Grant the new user full privileges to all databases on the server.
To grant the MySQL user specific access to a target database such as exampledb instead of global database server permissions, run the following command instead.
Assigning the MySQL user full privileges to all databases allows the user to perform all types of backup tasks on the server with any supported options such as LOCK TABLES and --single-transaction that may not be available with a single class of privileges.
Flush the MySQL privileges table to apply the new user changes.
Exit the MySQL database console.
Vultr Object Storage is an S3-compatible cloud storage service that supports data transfer tools such as Cyberduck, S3 Browser, and S3cmd to securely store and share files. Follow the steps below to set up Vultr Object Storage, create a new bucket, and upload test data files from the server using S3cmd.
Open the Vultr Console.
Expand Cloud Storage and click Object Storage from the list of menu options.
Select your Object Storage instance to view the access credentials.
View and copy your Vultr Object Storage Hostname, Secret, and Access keys to use with s3cmd.
Switch to your server SSH session.
Update the server packages.
Install the s3cmd file transfer application on your server.
Run s3cmd with the --configure option to set up Vultr Object Storage as the default provider.
Output:
Paste your Vultr Object Storage access key and press Enter to save the new access key.
Paste your secret key and press Enter.
Press Enter to use the default S3 region US.
Enter your Vultr Object Storage hostname in the S3 Endpoint prompt. For example, ew1.vultrobjects.com.
Enter the bucket DNS-style template to use with Vultr Object Storage. For example %(bucket)s.ewr1.vultrobjects.com.
Enter a strong encryption password to protect your Vultr Object Storage connection from unauthorized users on the server.
Press Enter to accept the default GPG program path /usr/bin/gpg to encrypt objects during transfer to Vultr Object Storage.
Press Enter to use the HTTPS protocol with Vultr Object Storage.
Press Enter to use Vultr Object Storage without an HTTP Proxy connection. If your network requires a proxy, enter the server URL to use with s3cmd.
Review your object transfer configuration and enter Y to test access to your Vultr Object Storage.
Press Y then Enter to save the s3cmd configuration when the test is successful.
View the s3cmd configuration file .s3cfg in your user home directory to verify your Vultr Object Storage connection settings.
Create a new Vultr Object Storage bucket to store your MySQL backup files using s3cmd. For example mysql-backups.
Output:
Follow the Vultr Object Storage naming requirements when creating new buckets to avoid any errors. Use lowercase, unique and only allowed characters to successfully create a new bucket.
List all available Vultr Object Storage buckets to verify your new storage resource.
Create a new sample text file mysql-test.txt to upload to the Vultr Object Storage bucket.
Transfer the sample text file to your Vultr Object Storage bucket.
List all objects in the bucket to verify that s3cmd correctly uploads files to your Vultr Object Storage.
Output:
Access your Vultr Object Storage instance page, click Buckets and select your new mysql-backups bucket. View the bucket contents to verify that the sample text file is available similar to your s3cmd output.
The mysqldump database backup utility performs logical MySQL backups by creating a set of SQL statements that reproduce the original database contents when executed. mysqldump supports exports to multiple file types such as .sql, .sh, .csv, .xml, or .txt depending on your desired backup naming scheme. Follow the steps below to back up your MySQL databases using the mysqldump utility.
Create a new directory to store the MySQL backup files on the server. For example, mysql-backups.
Switch to the directory.
Back up the sample MySQL database you created earlier exampledb to a file such as exampledb.sql using mysqldump.
Enter your MySQL backup user password to log in and start the backup process.
Long list the directory files and verify that a new exampledb.sql file is available with the correct file size.
Output:
If your total MySQL database backup file size is zero bytes, verify that your backup user has enough privileges and mysqldump runs without any errors to avoid data corruption. The database backup file size depends on the MySQL database content in which a big database results in a big backup file. As a result, a backup file with zero bytes is a result of an incomplete backup or a corrupted database.
Back up the second sample database sampledb with the --lock-tables option to prevent database modifications during the backup process.
Back up all available databases on the MySQL database server to create a full backup.
The full MySQL backup creates a single file with all existing databases, unlike the single database backup method that contains only specific databases.
Back up all MySQL database user tables to back up the existing users and associated privileges.
List files to verify all the available MySQL backup files in your working directory.
Output:
Create a new directory to organize and store your database backup files. For example, test-backups.
Move all MySQL backup files .sql to the directory for upload to your Vultr Object Storage bucket.
Upload all files in the test backup files directory to your object storage bucket.
Output:
List your Vultr Object Storage bucket objects and verify that all MySQL backup files are available.
Output:
You have transferred the MySQL backup files to your Vultr Object Storage bucket. Create separate directories for each MySQL backup operation to organize your backup files for smooth recoveries at a specific point in time. For example, format the database backup files with calendar dates such as w1-04-2024 or 10-04 to represent a specific backup date or timeframe for identification purposes.
Create a new data directory such as w1-04-2024 to organize your MySQL backup directory by date.
Copy all existing MySQL backup files from the test-backups directory to the new directory.
Upload the new MySQL backup directory to your object storage bucket.
View the object storage bucket data and verify that the newly added directory files.
Output:
Automating MySQL database backups involves creating a script that runs at a specific point in time such as once every 7 days. You can create the file using scripting tools such as Bash, Zsh, PowerShell, or Python. Follow the steps below to create a sample bash script that automates the mysqldump backup tasks, creates a unique backup directory based on the server time, and transfers the new backup files directory to your Vultr Object Storage bucket.
Print your working directory to verify your MySQL backup directory path.
Output:
Create a new bash script file mysql-backup-script.sh using a text editor such as Vim.
Add the following contents to the file.
Save and close the file.
The above script creates a new MySQL database backup directory based on the server date week and month value. Then, the script creates a new MySQL backup file that includes all databases and uploads the entire backup files directory to the mysql-backups Vultr Object Storage bucket. In addition, the generated directory and files are then deleted to free up the server space and only keep the remote object storage files before stopping the script with a Transfer Complete!!! message.
Run the database backup script using the Bash interpreter.
Monitor the process and verify that the script completes successfully with the following output:
List your Vultr Object Storage bucket objects and verify that a new MySQL backup directory is available.
Output:
Open the Crontab editor to create a new Cron job and automate the MySQL database backup script.
Add the following task at the end of the file to run the MySQL script once every week at 5:00 AM.
Save and close the file.
The above Cron job runs the MySQL database backup script once every 7 days on Sunday at 5:00 AM based on the expression 0 5 * * 0:
0: Represents the minute (0 to 59) to run the specified command.5: Represents the hour (0 to 23).*: Sets the day of the month (1-31). The value * runs the command every day or week.*: Sets the month in a calendar year (1 to 12). The value * runs the command every month.0: Sets the day of the week (0-6) to run the command. The value 0 represents Sunday. In addition, the Cron job logs all command outputs to the file automatic-backups.log using the >> /home/user/mysqlbackups/automatic-backups.log 2>&1 command.
Create the MySQL database backup log file referenced by your Cron job.
Long list your MySQL backup directory and verify that your user owns all directory files.
Output:
All active Cron jobs on the server run with your user profile. Therefore, verify that your user has enough privileges to the directory to avoid incomplete or corrupted backups.
Test your Cron job by running the full task command to verify if it works correctly.
The task should not return any output similar to the Cron job. View the Log file to confirm the available script information.
Output:
You have enabled automatic MySQL database backups with Vultr Object Storage on a Debian database server. To ensure data consistency, set up multiple scripts depending on your MySQL database backup operations to store different copies of your databases at a time. You can use automatic backups with production web applications or large databases that require consistent and close monitoring to recover your MySQL database or migrate to a new or existing server.
0 Comments
Be the first to comment and share your perspective with the community.