{"id":22493,"date":"2023-10-15T04:56:42","date_gmt":"2023-10-15T04:56:42","guid":{"rendered":"https:\/\/linuxways.net\/?p=22493"},"modified":"2023-10-19T02:25:34","modified_gmt":"2023-10-19T02:25:34","slug":"backup-restore-mysql-mariadb-using-automysqlbackup","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/scripting\/backup-restore-mysql-mariadb-using-automysqlbackup\/","title":{"rendered":"How to Back Up and Restore MySQL and MariaDB Using Automysqlbackup"},"content":{"rendered":"<p>Data loss is a terrible scenario for any organization that relies on databases to hold a crucial information. Whether it&#8217;s due to an unintentional loss, hardware failure, or software corruption, a backup solution is essential to ensure the data integrity and speedy recovery. MySQL and MariaDB offer robust data storing and retrieval capabilities. AutoMySQLBackup provides a dependable and effective solution to automate the backup and restore process to protect our databases.<\/p>\n<p>Let us discuss how to use the AutoMySQLBackup to create the automated backups of our MySQL and MariaDB databases, as well as restore them when necessary.<\/p>\n<h2><strong>Creating the Database and Table in MySQL<\/strong><\/h2>\n<p>Before doing anything, we have to install MySQL in our Linux system. After that, follow the further steps.<\/p>\n<p>We can create the database in MySQL or MariaDB. Here, we create the database in MySQL.<\/p>\n<p>Create the database first:<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"width:100%;\"><div class=\"text codecolorer\">CREATE DATABASE maria_db;<\/div><\/div>\n<p>Now, let us make a table named \u201clipi_tbl\u201d.<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"width:100%;\"><div class=\"text codecolorer\">CREATE TABLE lipi_tbl (UserID INT AUTO_INCREMENT PRIMARY KEY,<br \/>\n<br \/>\n-&gt; UserName VARCHAR(50),<br \/>\n<br \/>\n-&gt; IsActive BOOL);<\/div><\/div>\n<p>After creating the table, we insert the values into the column and then select the table name for show.<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"width:100%;height:100%;\"><div class=\"text codecolorer\">mysql&gt; CREATE TABLE lipi_tbl (UserID INT AUTO_INCREMENT PRIMARY KEY,<br \/>\n<br \/>\n-&gt; UserName VARCHAR(50),<br \/>\n<br \/>\n-&gt; IsActive BOOL);<br \/>\n<br \/>\nmysql&gt; INSERT INTO lipi_tbl (username,isactive) VALUES ('Lipi', 1);<br \/>\n<br \/>\nQuery OK, 1 row affected (0.01 sec)<br \/>\n<br \/>\nmysql&gt; INSERT INTO lipi_tbl (username,isactive) VALUES ('Ram', 2);<br \/>\n<br \/>\nQuery OK, 1 row affected (0.15 sec)<br \/>\n<br \/>\nmysql&gt; select * from lipi_tbl;<br \/>\n<br \/>\n+--------+----------+----------+<br \/>\n<br \/>\n| UserID | UserName | IsActive |<br \/>\n<br \/>\n+--------+----------+----------+<br \/>\n<br \/>\n| 1 | Lipi | 1 |<br \/>\n<br \/>\n| 2 | Ram | 2 |<br \/>\n<br \/>\n+--------+----------+----------+<br \/>\n<br \/>\n2 rows in set (0.00 sec)<\/div><\/div>\n<h2><strong>Installing AutoMySQLBackup in our Linux System<\/strong><\/h2>\n<p>Let us install AutoMySQLBackup at first.<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"width:100%;\"><div class=\"text codecolorer\"># aptitude install automysqlbackup<\/div><\/div>\n<p>In order to download and run the installation script, let&#8217;s start by making a working directory inside of \u201c\/opt\u201d:<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"width:100%;\"><div class=\"text codecolorer\"># mkdir \/opt\/automysqlbackup<br \/>\n<br \/>\n# cd \/opt\/automysqlbackup<br \/>\n<br \/>\n# wget http:\/\/ufpr.dl.sourceforge.net\/project\/automysqlbackup\/AutoMySQLBackup\/AutoMySQLBackup%20VER%203.0\/automysqlbackup-v3.0_rc6.tar.gz<br \/>\n<br \/>\n# tar zxf automysqlbackup-v3.0_rc6.tar.gz<br \/>\n<br \/>\n# .\/install.sh<\/div><\/div>\n<p>The AutoMySQLBackup configuration file named \u201cmyserver.conf\u201d is found at \u201c\/etc\/automysqlbackup\u201d. Let&#8217;s look at the most important configuration directives:<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"width:100%;\"><div class=\"text codecolorer\"># cd \/etc\/automysqlbackup\/<br \/>\n<br \/>\n# ls<br \/>\n<br \/>\nautomysqlbackup.conf '*.conf_converted' LICENSE myserver.conf README<br \/>\n<br \/>\n# vim myserver.conf<\/div><\/div>\n<p>After that, start configuring AutoMySQLBackup as per our need. Once we are complete with the configuration, just save it. It is strongly recommended to read the README file located in \u201c\/etc\/automysqlbackup\/README\u201d.<\/p>\n<h2><strong>Backup of MySQL Database<\/strong><\/h2>\n<p>When we&#8217;re ready, execute the program with the configuration file as an argument:<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"width:100%;\"><div class=\"text codecolorer\"># automysqlbackup \/etc\/automysqlbackup\/myserver.conf<\/div><\/div>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"width:100%;\"><div class=\"text codecolorer\"># automysqlbackup \/etc\/automysqlbackup\/myserver.conf<br \/>\n<br \/>\nParsed config file &quot; \/etc\/automysqlbackup\/automysqlbackup.conf&quot;<br \/>\n<br \/>\n# Checking for permissions to write to folders:<br \/>\n<br \/>\nbase folder \/var ... exists ... ok.<br \/>\n<br \/>\nbackup folder \/var\/backup ... creating ... success.<\/div><\/div>\n<p>Now, navigate to the &#8220;\/var\/backup&#8221; backup directory to inspect the backup file.<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"width:100%;\"><div class=\"text codecolorer\"># cd \/var\/backup\/db\/automysqlbackup<\/div><\/div>\n<p>A quick look at the daily directory reveals that AutoMySQLBackup is successful:<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"width:100%;\"><div class=\"text codecolorer\"># pwd<br \/>\n<br \/>\n# ls -lR daily<\/div><\/div>\n<p>Set the crontab entry as shown in the following to take an AutoMySQLBackup every day at a certain time:<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"width:100%;\"><div class=\"text codecolorer\">30 05 * * * \/usr\/local\/bin\/automysqlbackup \/etc\/automysqlbackup\/myserver.conf<\/div><\/div>\n<h2><strong>Restore or Bring Back a MySQL Backup<\/strong><\/h2>\n<p>Now, let&#8217;s intentionally delete the \u201cmariadb_db\u201d database:<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"width:100%;height:100%;\"><div class=\"text codecolorer\"># mysql -u root -p<br \/>\n<br \/>\nEnter password:<br \/>\n<br \/>\nMariaDB [(none)]&gt; show databases<br \/>\n<br \/>\n+--------------------+<br \/>\n<br \/>\n| Database |<br \/>\n<br \/>\n+--------------------+<br \/>\n<br \/>\n| information_schema |<br \/>\n<br \/>\n| maria_ |<br \/>\n<br \/>\n| maria_db |<br \/>\n<br \/>\n| mysql |<br \/>\n<br \/>\n| postgresql_db |<br \/>\n<br \/>\n+--------------------+<br \/>\n<br \/>\n6 rows in set (0.00 sec)<br \/>\n<br \/>\nMariaDB [(none)]&gt; DROP DATABASE maria_db<br \/>\n<br \/>\nQuery OK, 1 row affected (0.06 sec)<br \/>\n<br \/>\nMariaDB [(none)]&gt; show databases<br \/>\n<br \/>\n+--------------------+<br \/>\n<br \/>\n| Database |<br \/>\n<br \/>\n+--------------------+<br \/>\n<br \/>\n| information_schema |<br \/>\n<br \/>\n| maria_ |<br \/>\n<br \/>\n| mysql |<br \/>\n<br \/>\n| postgresql_db |<br \/>\n<br \/>\n+--------------------+<br \/>\n<br \/>\n5 rows in set (0.00 sec)<\/div><\/div>\n<p>Let us recreate it and restore the backup. Enter the following command into the MariaDB prompt:<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"width:100%;\"><div class=\"text codecolorer\">MariaDB [(none)]&gt; CREATE DATABASE maria_db<br \/>\n<br \/>\nQuery OK, 1 row affected (0.00 sec)<\/div><\/div>\n<p>Then, specify where the backup must be restored.<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"width:100%;\"><div class=\"text codecolorer\"># cd \/var\/backup\/daily\/maria_db<br \/>\n<br \/>\n# ls<br \/>\n<br \/>\ndaily_maria_db_2023-06-11_11h08m_Sunday.sql.gz<\/div><\/div>\n<p>The backup file should then be restored into the \u201cmaria_db\u201d database.<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"width:100%;\"><div class=\"text codecolorer\">MariaDB [(none)]&gt; USE maria_db;<br \/>\n<br \/>\nMariaDB [(none)]&gt; SELECT * FROM lipi_tb1;<\/div><\/div>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"width:100%;height:100%;\"><div class=\"text codecolorer\"># mysql -u root -p maria_db &lt; daily_maria_db_2023-06-11_11h08m_Sunday.sql.gz<br \/>\n<br \/>\n\/\/Enter your Password<br \/>\n<br \/>\nEnter password:<br \/>\n<br \/>\nMariaDB [(none)]&gt; show databases<br \/>\n<br \/>\n+--------------------+<br \/>\n<br \/>\n| Database |<br \/>\n<br \/>\n+--------------------+<br \/>\n<br \/>\n| information_schema |<br \/>\n<br \/>\n| maria_ |<br \/>\n<br \/>\n| maria_db |<br \/>\n<br \/>\n| mysql |<br \/>\n<br \/>\n| postgresql_db |<br \/>\n<br \/>\n+--------------------+<br \/>\n<br \/>\n6 rows in set (0.00 sec)<br \/>\n<br \/>\nMariaDB [(none)]&gt; use maria_db<br \/>\n<br \/>\nDatabase changed<br \/>\n<br \/>\n\/\/Showing our table which we created before<br \/>\n<br \/>\nMariaDB [maria_db]&gt; select * from lipi_tbl;<br \/>\n<br \/>\n+--------+----------+----------+<br \/>\n<br \/>\n| UserID | UserName | IsActive |<br \/>\n<br \/>\n| 1 | Lipi | 1 |<\/div><\/div>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>AutoMySQLBackup automates the backup and restore, ensuring the data availability while minimizing the human error. We can protect our MySQL and MariaDB databases with this excellent approach. Integrate AutoMySQLBackup into our routine to improve the data protection and lower the chance of data loss. Secure our databases right away to ensure that our critical data is safely backed up and is rapidly recoverable.<\/p>","protected":false},"excerpt":{"rendered":"<p>Comprehensive tutorial on how to use the AutoMySQLBackup to create the automated backups of our MySQL and MariaDB databases and restore them when necessary.<\/p>","protected":false},"author":111,"featured_media":22496,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[168],"tags":[],"class_list":["post-22493","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/22493","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/111"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=22493"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/22493\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/22496"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=22493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=22493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=22493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}