<h1 id="mysqlautomatedbackuponlinux">MySQL automated backup on Linux</h1> <hr /> <h2 id="1preparationbeforehand">1. Preparation beforehand</h2> <p>First of all, let’s create a source folder for your backup plan where you will put the dump file of MySQL databases. In this article, it will be a folder with the name <strong>mysqldump</strong> which will be located in the <strong>media</strong> folder. Just bear in mind that the folder you create should be reachable by any users on your Linux.</p> <pre><code>sudo mkdir /media/mysqldump </code></pre> <h2 id="2script">2. Script</h2> <p>Now let’s make a little script <strong>mysqldump.sh</strong> in recently created folder.</p> <pre><code>sudo gedit /media/mysqldump/mysqldump.sh </code></pre> <p>In the opened editor type this command with a correct username and a password. This user should have enough permissions to backup all your MySQL databases. As a result you will have one file with the current data in its name with all your MySQL databases.</p> <pre><code>mysqldump -u USER_NAME -p 'PASSWORD' --all-databases &gt; /media/mysqldump/backupall_'/usr/bin/date +\%Y\%m\%d\%H\%M' </code></pre> <p><img src="/content/images/aa0cfd44-aa06-4993-8c09-0563ebf3b639.png" alt="" /></p> <p>If you would like to keep a certain number of versions you should add this command to your script. Just change 10 to the desired number. The command below deletes the backup which is older than 10 days.</p> <pre><code>find /media/mysqldump -mtime +10 -type f -delete </code></pre> <p>Then save the changes and close the editor.</p> <h2 id="3crontab">3. Crontab</h2> <p>To schedule this script to run everyday run <strong>crontab</strong></p> <pre><code>sudo crontab -e </code></pre> <p>You will see the crontab window where you should configure the time when you will dump all your MySQL databases. Press "i" (for “insert”) to begin editing the file and type the command below. In this example the dump will run everyday at 12:55 PM. </p> <pre><code>55 12 * * * /media/mysqldump/mysqldump.sh </code></pre> <p><img src="/content/images/9b0ba348-543a-4b17-b7e6-475158cb8ffa.png" alt="" /></p> <p>Press ESC and type <strong>:wq</strong> to save the changes and quit.</p> <h2 id="4backupplan">4. Backup plan</h2> <p>Now you can create a backup plan and configure it to backup all files in the folder <strong>/media/mysqldump</strong> with the recurring option <strong>Everyday at 1.20 PM</strong></p>