{"id":15518,"date":"2024-10-21T07:19:28","date_gmt":"2024-10-21T07:19:28","guid":{"rendered":"https:\/\/truehost.com\/support\/?post_type=docs&#038;p=15518"},"modified":"2024-10-21T10:57:39","modified_gmt":"2024-10-21T10:57:39","password":"","slug":"how-to-back-up-mysql-databases-automatically-with-cron","status":"publish","type":"docs","link":"https:\/\/truehost.com\/support\/knowledge-base\/how-to-back-up-mysql-databases-automatically-with-cron\/","title":{"rendered":"How to Back Up MySQL Databases Automatically with Cron"},"content":{"rendered":"\n<p>Backing up MySQL databases regularly is crucial to protect your data from unexpected loss due to server crashes, hardware failures, or accidental deletion. You can automate this process using a cron job, which runs a scheduled script to create backups at regular intervals. This guide will walk you through the steps to automatically back up your MySQL databases using cron.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Prerequisites:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A server running MySQL or MariaDB.<\/li>\n\n\n\n<li>Access to a user account with the necessary MySQL permissions. If you want to back up all databases, you could need a user like root and the root mysql database password<\/li>\n\n\n\n<li>You need your database name, database user and password<\/li>\n\n\n\n<li>Access to the terminal (with sudo privileges).<\/li>\n<\/ul>\n\n\n\n<p>This can be done in various ways. We will explain how to do it using these two methods<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>By Including MySQL login information in the cron job command<\/li>\n\n\n\n<li>Using a Backup Script<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Method 1: By Including MySQL login information in the cron job command<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First start by creating the directory where you want to store the backup files.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>I will keep mine in <strong>\/home\/dbbackup<\/strong><\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full is-style-zoooom\"><img loading=\"lazy\" decoding=\"async\" width=\"599\" height=\"47\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-427.png\" alt=\"\" class=\"wp-image-15521\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-427.png 599w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-427-300x24.png 300w\" sizes=\"auto, (max-width: 599px) 100vw, 599px\" \/><\/figure>\n\n\n\n<p>You can then schedule your MySQL database backups by creating a cron job that run the backup command below;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\/usr\/bin\/mysqldump --routines -u <em>dbusername<\/em> -p'<em>dbpassword<\/em>' <em>dbname<\/em> > \/<em>path<\/em>\/to\/backup.sql<\/strong><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In the above command, replace <strong><em>dbusername<\/em><\/strong>  with the name of the database user, <strong><em>dbusername<\/em><\/strong> with the name of the database that you are backing up and <strong>\/<em>path\/to <\/em><\/strong>with the name of the directory where you want to put the backup file.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You first need to run the command manually on terminal to see if it outputs the desired result. Once you verify that, you can now automate it with a command.<\/li>\n<\/ul>\n\n\n\n<p>I will back up a database called <strong>fbtdl_demodb<\/strong> to a directory called <strong>\/home\/dbbackup<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\/usr\/bin\/mysqldump --routines -u fbtdl_demodbu -p'qsPGoGJLkwDiyux' fbtdl_demodb > \/home\/<em>dbbackup<\/em>\/fbtdl_demodb.sql<\/strong><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large is-style-zoooom\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"74\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-428-1024x74.png\" alt=\"\" class=\"wp-image-15522\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-428-1024x74.png 1024w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-428-300x22.png 300w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-428-768x56.png 768w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-428.png 1156w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Our backup is successful as in the image above and we can use the ls command to see that the backup is there<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-zoooom\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"55\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-429-1024x55.png\" alt=\"\" class=\"wp-image-15523\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-429-1024x55.png 1024w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-429-300x16.png 300w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-429-768x41.png 768w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-429.png 1152w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We will then create a cronjob that runs the command every day at midnight. You can use <strong><a href=\"https:\/\/crontab.guru\/\" target=\"_blank\" rel=\"noreferrer noopener\">this tool <\/a><\/strong>to know the right values for your cron.<\/li>\n\n\n\n<li>For the command to run every midnight, I will set the command as below <\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>0 0 * * *<\/strong> <strong>\/usr\/bin\/mysqldump --routines -u fbtdl_demodbu -p'qsPGoGJLkwDiyux' fbtdl_demodb > \/home\/<em>dbbackup<\/em>\/fbtdl_demodb.sql<\/strong><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Note that in the above, this <strong>0 0 * * *<\/strong>  is what tells the command to run every midnight<\/li>\n\n\n\n<li>You can use <strong><a href=\"https:\/\/crontab.guru\/\" target=\"_blank\" rel=\"noreferrer noopener\">crontab guru<\/a><\/strong> to set your desired values that correspond to your desired timings, then put then in crontab<\/li>\n\n\n\n<li>Go to your server&#8217;s terminal and type the command below, to open crontab, as in in in the image below<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>crontab -e<\/strong><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1008\" height=\"358\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-430.png\" alt=\"\" class=\"wp-image-15526\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-430.png 1008w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-430-300x107.png 300w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-430-768x273.png 768w\" sizes=\"auto, (max-width: 1008px) 100vw, 1008px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If the crontab opened with vi editor, press letter <strong>&#8220;i&#8221;<\/strong> to enter insert mode, then add your command somewhere in the crontab<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"158\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-431-1024x158.png\" alt=\"\" class=\"wp-image-15529\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-431-1024x158.png 1024w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-431-300x46.png 300w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-431-768x118.png 768w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-431.png 1207w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You then press <strong>ESC<\/strong> to exit the insert node, then <strong>:wq<\/strong> followed by <strong>Enter<\/strong>, to save<\/li>\n\n\n\n<li>You have set the cron to do backups on daily basis<\/li>\n<\/ul>\n\n\n\n<p>Note that that backups could be taking the server space, thus you will need to monitor and remove those that need to be removed. You could use automation scripts to do thi.<\/p>\n\n\n\n<p>Note that Truehost support does not give support on how to do these custom configurations. However, if you have a question that relates to your databases, you can contact the <strong><a href=\"https:\/\/wa.me\/19726743814\" target=\"_blank\" rel=\"noreferrer noopener\">support team <\/a><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Method 2: Using a Backup Script<\/strong><\/h2>\n\n\n\n<p>You can also use a script to do these automation<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Step 1: Create a Backup Directory<\/strong><\/h5>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, you need a directory to store your MySQL backups. Create a directory in your server\u2019s filesystem to hold the backup files.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo mkdir -p \/home\/<em>dbbackup<\/em>\/\n\nsudo chmod 700 \/home\/<em>dbbackup<\/em>\/<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-zoooom\"><img loading=\"lazy\" decoding=\"async\" width=\"744\" height=\"336\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-432.png\" alt=\"\" class=\"wp-image-15550\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-432.png 744w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-432-300x135.png 300w\" sizes=\"auto, (max-width: 744px) 100vw, 744px\" \/><\/figure>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Step 2: Create a MySQL Backup Script<\/strong><\/h5>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Next, you\u2019ll create a shell script that will handle the backup process. You can write this script in any directory. I would sugegst that you use  <code>\/usr\/local\/bin\/<\/code>  for system-wide scripts.<\/li>\n\n\n\n<li>Open a text editor to create the script:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>sudo vi \/usr\/local\/bin\/mysql_backup.sh<\/strong><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-zoooom\"><img loading=\"lazy\" decoding=\"async\" width=\"625\" height=\"143\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-435.png\" alt=\"\" class=\"wp-image-15555\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-435.png 625w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-435-300x69.png 300w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Add the following code to the script:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\n# MySQL credentials\nUSER=\"<strong>your_mysql_user<\/strong>\"\nPASSWORD=\"<strong>your_mysql_password<\/strong>\"\nHOST=\"localhost\"\nBACKUP_DIR=\"\/home\/dbbackup\/\"\nDATE=$(date +\"%Y%m%d_%H%M%S\")\n\n# Create backup filename with a timestamp\nBACKUP_FILE=\"$BACKUP_DIR\/mysql_backup_$DATE.sql\"\n\n# Dump all databases to the backup file\nmysqldump --user=$USER --password=$PASSWORD --host=$HOST --all-databases > $BACKUP_FILE\n\n# Optional: Remove backups older than 7 days to save space\nfind $BACKUP_DIR -type f -name \"*.sql\" -mtime +7 -exec rm {} \\;\n\n# Log the result\necho \"MySQL backup completed at $DATE\" >> \/home\/dbbackup\/backup.log<\/code><\/pre>\n\n\n\n<p>Note that you need to replace the following in the above script;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>your_mysql_user<\/strong> with your databse user (for all databases, user root)<\/li>\n\n\n\n<li><strong>your_mysql_password<\/strong> with your database password (password for the root user)<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full is-style-zoooom\"><img loading=\"lazy\" decoding=\"async\" width=\"953\" height=\"515\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-434.png\" alt=\"\" class=\"wp-image-15554\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-434.png 953w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-434-300x162.png 300w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-434-768x415.png 768w\" sizes=\"auto, (max-width: 953px) 100vw, 953px\" \/><\/figure>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Step 3: Make the Script Executable<\/strong><\/h5>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You need to make the script executable to ensure it runs correctly.<\/li>\n<\/ul>\n\n\n\n<p><strong>sudo chmod +x \/usr\/local\/bin\/mysql_backup.sh<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-zoooom\"><img loading=\"lazy\" decoding=\"async\" width=\"858\" height=\"206\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-436.png\" alt=\"\" class=\"wp-image-15557\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-436.png 858w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-436-300x72.png 300w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-436-768x184.png 768w\" sizes=\"auto, (max-width: 858px) 100vw, 858px\" \/><\/figure>\n\n\n\n<p><strong>Step 4: Test the Backup Script<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Before automating it with cron, test the script to make sure it works:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>sudo \/usr\/local\/bin\/mysql_backup.sh<\/strong><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-zoooom\"><img loading=\"lazy\" decoding=\"async\" width=\"818\" height=\"174\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-437.png\" alt=\"\" class=\"wp-image-15558\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-437.png 818w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-437-300x64.png 300w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-437-768x163.png 768w\" sizes=\"auto, (max-width: 818px) 100vw, 818px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full is-style-zoooom\"><img loading=\"lazy\" decoding=\"async\" width=\"693\" height=\"104\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-438.png\" alt=\"\" class=\"wp-image-15560\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-438.png 693w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-438-300x45.png 300w\" sizes=\"auto, (max-width: 693px) 100vw, 693px\" \/><\/figure>\n\n\n\n<p>After running this command, check the <code><strong>\/home\/dbbackup\/<\/strong><\/code> directory for the backup file.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Step 5: Set Up a Cron Job<\/strong><\/h5>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Now that your script works, you can set up a cron job to automate the backup process.<\/li>\n\n\n\n<li>Open the crontab configuration file:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>sudo crontab -e<\/strong><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-zoooom\"><img loading=\"lazy\" decoding=\"async\" width=\"774\" height=\"172\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-440.png\" alt=\"\" class=\"wp-image-15564\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-440.png 774w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-440-300x67.png 300w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-440-768x171.png 768w\" sizes=\"auto, (max-width: 774px) 100vw, 774px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Add the following cron job to run the backup script daily at 12:00 AM:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>0 0 * * * \/usr\/local\/bin\/mysql_backup.sh<\/strong><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"971\" height=\"304\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-439.png\" alt=\"\" class=\"wp-image-15562\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-439.png 971w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-439-300x94.png 300w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-439-768x240.png 768w\" sizes=\"auto, (max-width: 971px) 100vw, 971px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>This sets the cron job to run every day at 12:00 AM. You can adjust the time and frequency as needed.<\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Step 6: Verify the Cron Job<\/strong><\/h5>\n\n\n\n<p>Cron jobs are logged in the system log file. You can verify if the cron job runs successfully by checking the logs.  Run this command to view the cron logs:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>grep CRON \/var\/log\/syslog<\/strong><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"521\" src=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-441-1024x521.png\" alt=\"\" class=\"wp-image-15567\" srcset=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-441-1024x521.png 1024w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-441-300x153.png 300w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-441-768x391.png 768w, https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-441.png 1257w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>With this setup, your MySQL databases will be backed up automatically every day at 12:00 AM, and backups older than 7 days will be deleted to save disk space. <\/p>\n\n\n\n<p>You can modify the backup schedule or retention policy by adjusting the cron job or backup script. Regularly check your backups to ensure everything works as expected.<\/p>\n\n\n\n<p>Also this is a sample working script. If you know bash scripting, you can modify the script logic to suite your needs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Backing up MySQL databases regularly is crucial to protect your data from unexpected loss due to server crashes, hardware failures, or accidental deletion. You can automate this process using a cron job, which runs a scheduled script to create backups at regular intervals. This guide will walk you through the steps to automatically back up [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_eb_attr":"","_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"doc_category":[1820,1824,1833,1879,2128],"doc_tag":[],"class_list":["post-15518","docs","type-docs","status-publish","hentry","doc_category-cloud-servers-in-kenya","doc_category-dedicated-servers","doc_category-mysql","doc_category-servers","doc_category-vps-servers"],"year_month":"2026-07","word_count":1051,"total_views":"0","reactions":{"happy":"24","normal":"0","sad":"0"},"author_info":{"name":"Eugene","author_nicename":"eugene","author_url":"https:\/\/truehost.com\/support\/author\/eugene\/"},"doc_category_info":[{"term_name":"Cloud servers in Kenya","term_url":"https:\/\/truehost.com\/support\/docs-category\/cloud-servers-in-kenya\/"},{"term_name":"dedicated servers","term_url":"https:\/\/truehost.com\/support\/docs-category\/dedicated-servers\/"},{"term_name":"MySQL","term_url":"https:\/\/truehost.com\/support\/docs-category\/mysql\/"},{"term_name":"Servers","term_url":"https:\/\/truehost.com\/support\/docs-category\/servers\/"},{"term_name":"VPS-Servers","term_url":"https:\/\/truehost.com\/support\/docs-category\/vps-servers\/"}],"doc_tag_info":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Back Up MySQL Databases Automatically with Cron -<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/truehost.com\/support\/knowledge-base\/how-to-back-up-mysql-databases-automatically-with-cron\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Back Up MySQL Databases Automatically with Cron -\" \/>\n<meta property=\"og:description\" content=\"Backing up MySQL databases regularly is crucial to protect your data from unexpected loss due to server crashes, hardware failures, or accidental deletion. You can automate this process using a cron job, which runs a scheduled script to create backups at regular intervals. This guide will walk you through the steps to automatically back up [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/truehost.com\/support\/knowledge-base\/how-to-back-up-mysql-databases-automatically-with-cron\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-21T10:57:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-427.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/how-to-back-up-mysql-databases-automatically-with-cron\\\/\",\"url\":\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/how-to-back-up-mysql-databases-automatically-with-cron\\\/\",\"name\":\"How to Back Up MySQL Databases Automatically with Cron -\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/how-to-back-up-mysql-databases-automatically-with-cron\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/how-to-back-up-mysql-databases-automatically-with-cron\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/truehost.com\\\/support\\\/wp-content\\\/uploads\\\/2024\\\/10\\\/image-427.png\",\"datePublished\":\"2024-10-21T07:19:28+00:00\",\"dateModified\":\"2024-10-21T10:57:39+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/how-to-back-up-mysql-databases-automatically-with-cron\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/how-to-back-up-mysql-databases-automatically-with-cron\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/how-to-back-up-mysql-databases-automatically-with-cron\\\/#primaryimage\",\"url\":\"https:\\\/\\\/truehost.com\\\/support\\\/wp-content\\\/uploads\\\/2024\\\/10\\\/image-427.png\",\"contentUrl\":\"https:\\\/\\\/truehost.com\\\/support\\\/wp-content\\\/uploads\\\/2024\\\/10\\\/image-427.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/how-to-back-up-mysql-databases-automatically-with-cron\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/truehost.com\\\/support\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Back Up MySQL Databases Automatically with Cron\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/#website\",\"url\":\"https:\\\/\\\/truehost.com\\\/support\\\/\",\"name\":\"\",\"description\":\"Help In a Click\",\"publisher\":{\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/truehost.com\\\/support\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/#organization\",\"name\":\"Truehost Kenya\",\"url\":\"https:\\\/\\\/truehost.com\\\/support\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/truehost.com\\\/support\\\/wp-content\\\/uploads\\\/2026\\\/04\\\/cropped-image_2026-04-16_174808866.png\",\"contentUrl\":\"https:\\\/\\\/truehost.com\\\/support\\\/wp-content\\\/uploads\\\/2026\\\/04\\\/cropped-image_2026-04-16_174808866.png\",\"width\":240,\"height\":48,\"caption\":\"Truehost Kenya\"},\"image\":{\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/#\\\/schema\\\/logo\\\/image\\\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Back Up MySQL Databases Automatically with Cron -","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/truehost.com\/support\/knowledge-base\/how-to-back-up-mysql-databases-automatically-with-cron\/","og_locale":"en_US","og_type":"article","og_title":"How to Back Up MySQL Databases Automatically with Cron -","og_description":"Backing up MySQL databases regularly is crucial to protect your data from unexpected loss due to server crashes, hardware failures, or accidental deletion. You can automate this process using a cron job, which runs a scheduled script to create backups at regular intervals. This guide will walk you through the steps to automatically back up [&hellip;]","og_url":"https:\/\/truehost.com\/support\/knowledge-base\/how-to-back-up-mysql-databases-automatically-with-cron\/","article_modified_time":"2024-10-21T10:57:39+00:00","og_image":[{"url":"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-427.png","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/truehost.com\/support\/knowledge-base\/how-to-back-up-mysql-databases-automatically-with-cron\/","url":"https:\/\/truehost.com\/support\/knowledge-base\/how-to-back-up-mysql-databases-automatically-with-cron\/","name":"How to Back Up MySQL Databases Automatically with Cron -","isPartOf":{"@id":"https:\/\/truehost.com\/support\/#website"},"primaryImageOfPage":{"@id":"https:\/\/truehost.com\/support\/knowledge-base\/how-to-back-up-mysql-databases-automatically-with-cron\/#primaryimage"},"image":{"@id":"https:\/\/truehost.com\/support\/knowledge-base\/how-to-back-up-mysql-databases-automatically-with-cron\/#primaryimage"},"thumbnailUrl":"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-427.png","datePublished":"2024-10-21T07:19:28+00:00","dateModified":"2024-10-21T10:57:39+00:00","breadcrumb":{"@id":"https:\/\/truehost.com\/support\/knowledge-base\/how-to-back-up-mysql-databases-automatically-with-cron\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/truehost.com\/support\/knowledge-base\/how-to-back-up-mysql-databases-automatically-with-cron\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/truehost.com\/support\/knowledge-base\/how-to-back-up-mysql-databases-automatically-with-cron\/#primaryimage","url":"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-427.png","contentUrl":"https:\/\/truehost.com\/support\/wp-content\/uploads\/2024\/10\/image-427.png"},{"@type":"BreadcrumbList","@id":"https:\/\/truehost.com\/support\/knowledge-base\/how-to-back-up-mysql-databases-automatically-with-cron\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/truehost.com\/support\/"},{"@type":"ListItem","position":2,"name":"How to Back Up MySQL Databases Automatically with Cron"}]},{"@type":"WebSite","@id":"https:\/\/truehost.com\/support\/#website","url":"https:\/\/truehost.com\/support\/","name":"","description":"Help In a Click","publisher":{"@id":"https:\/\/truehost.com\/support\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/truehost.com\/support\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/truehost.com\/support\/#organization","name":"Truehost Kenya","url":"https:\/\/truehost.com\/support\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/truehost.com\/support\/#\/schema\/logo\/image\/","url":"https:\/\/truehost.com\/support\/wp-content\/uploads\/2026\/04\/cropped-image_2026-04-16_174808866.png","contentUrl":"https:\/\/truehost.com\/support\/wp-content\/uploads\/2026\/04\/cropped-image_2026-04-16_174808866.png","width":240,"height":48,"caption":"Truehost Kenya"},"image":{"@id":"https:\/\/truehost.com\/support\/#\/schema\/logo\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/docs\/15518","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/comments?post=15518"}],"version-history":[{"count":21,"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/docs\/15518\/revisions"}],"predecessor-version":[{"id":15570,"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/docs\/15518\/revisions\/15570"}],"wp:attachment":[{"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/media?parent=15518"}],"wp:term":[{"taxonomy":"doc_category","embeddable":true,"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/doc_category?post=15518"},{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/doc_tag?post=15518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}