<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Isuru Edirisinghe</title>
    <description>The latest articles on DEV Community by Isuru Edirisinghe (@isuru117).</description>
    <link>https://dev.to/isuru117</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F240491%2F4294bdac-936b-4785-bdae-b7ae5d9e7fed.jpeg</url>
      <title>DEV Community: Isuru Edirisinghe</title>
      <link>https://dev.to/isuru117</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/isuru117"/>
    <language>en</language>
    <item>
      <title>Writing an automated Amazon S3 backup script for your MongoDB database using bash and cron in Ubuntu 16.04</title>
      <dc:creator>Isuru Edirisinghe</dc:creator>
      <pubDate>Tue, 01 Oct 2019 19:25:09 +0000</pubDate>
      <link>https://dev.to/isuru117/writing-an-automated-amazon-s3-backup-script-for-your-mongodb-database-using-bash-and-cron-in-ubuntu-16-04-5a11</link>
      <guid>https://dev.to/isuru117/writing-an-automated-amazon-s3-backup-script-for-your-mongodb-database-using-bash-and-cron-in-ubuntu-16-04-5a11</guid>
      <description>&lt;p&gt;While as easy as it may sound to write a simple backup script, there are a few steps that are involved in the process. And might be a tad bit unclear to those who have no prior experience of it. While there may be various methods of taking automated backups of your mongo db, this article aims to outline one such method which I personally prefer myself.
&lt;/p&gt;

&lt;h2&gt;Prerequisites&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;a href="https://hevodata.com/blog/install-mongodb-on-ubuntu/"&gt;properly setup MongoDB server&lt;/a&gt; on top of Ubuntu. The method outlined in this tutorial was &lt;b&gt;tested on Ubuntu 16.04 LTS&lt;/b&gt; though it may work with other versions as well.&lt;/li&gt;
&lt;li&gt;In most cases , &lt;b&gt;cron&lt;/b&gt; comes bundled with the most Linux distros. But if not, it should be pretty straightforward as running `sudo apt-get install cron` in a terminal window.&lt;/li&gt;
&lt;li&gt;In order to backup to Amazon S3 you need to have &lt;b&gt;aws cli&lt;/b&gt; installed and configured. Follow the &lt;a href="https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html"&gt;installation&lt;/a&gt; and &lt;a href="https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html"&gt;configuration&lt;/a&gt; instructions in the AWS documentation to do so.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;
Now that we have everything in place, we can start writing the script!&lt;/p&gt;
&lt;br&gt;

&lt;h2&gt;Writing the script&lt;/h2&gt;

&lt;p&gt;
First things first, you need to &lt;b&gt;setup database user credentials and other necessary variables&lt;/b&gt;. For now let’s provide DB credentials as variables for simplicity’s sake. Alternatively you could look into other ways to not expose credentials in your script. Also, it is advised to setup a separate database user with only backup privileges so that their is a minimum security risk involved in any case.&lt;/p&gt;

&lt;p&gt;
Go ahead and add the credentials and other necessary variables to your script
&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/sh&lt;/span&gt;

&lt;span class="nv"&gt;HOST&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;127.0.0.1 &lt;span class="c"&gt;#change to whatever your host IP is&lt;/span&gt;
&lt;span class="nv"&gt;BUCKET&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;yourbucket &lt;span class="c"&gt;#s3 bucket name&lt;/span&gt;
&lt;span class="nv"&gt;DEST&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$HOME&lt;/span&gt;/tmp &lt;span class="c"&gt;#backup directoryDBUSER=yourusername #the mongo username&lt;/span&gt;
&lt;span class="nv"&gt;DBPWD&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;yourpassword &lt;span class="c"&gt;#the mongo users password&lt;/span&gt;
&lt;span class="nv"&gt;DBNAME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;yourdbname &lt;span class="c"&gt;#the name of the DB to be backup&lt;/span&gt;
&lt;span class="nv"&gt;TIME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt; +%d-%m-%Y-%T&lt;span class="sb"&gt;`&lt;/span&gt; &lt;span class="c"&gt;#the time of the backup&lt;/span&gt;
&lt;span class="nv"&gt;TAR&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$DEST&lt;/span&gt;/../&lt;span class="nv"&gt;$DBNAME&lt;/span&gt;-&lt;span class="nv"&gt;$TIME&lt;/span&gt;.tar.gz &lt;span class="c"&gt;#name and dir of backup archive&lt;/span&gt;
&lt;span class="nb"&gt;set&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="c"&gt;#this stops execution of script if anything fails&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;
Most of the above code is commented and is self-explanatory. So let’s go ahead and continue with writing the rest of the script.
&lt;/p&gt;

&lt;p&gt;
The next step is to write the `mongodump` command, this may differ depending on your mongo db configuration. If you followed the tutorial linked at the beginning of this article on &lt;a href="https://hevodata.com/blog/install-mongodb-on-ubuntu/"&gt;installing and configuring mongodb&lt;/a&gt;, it should look something like the code below (The echo statements are for logging purposes which is needed later. Logs are pretty crucial specially in a production environment, if anything fails these come in handy to find out where things went wrong!)
&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Backing up &lt;/span&gt;&lt;span class="nv"&gt;$HOST&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="nv"&gt;$DBNAME&lt;/span&gt;&lt;span class="s2"&gt; to s3://&lt;/span&gt;&lt;span class="nv"&gt;$BUCKET&lt;/span&gt;&lt;span class="s2"&gt;/ on &lt;/span&gt;&lt;span class="nv"&gt;$TIME&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
mongodump &lt;span class="nt"&gt;-h&lt;/span&gt; &lt;span class="nv"&gt;$HOST&lt;/span&gt; &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="nv"&gt;$DBNAME&lt;/span&gt; &lt;span class="nt"&gt;-u&lt;/span&gt; &lt;span class="nv"&gt;$DBUSER&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; &lt;span class="nv"&gt;$DBPWD&lt;/span&gt; &lt;span class="nt"&gt;-o&lt;/span&gt; &lt;span class="nv"&gt;$DEST&lt;/span&gt; &lt;span class="nt"&gt;--authenticationDatabase&lt;/span&gt; your_auth_db_name
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Backup performed successfully"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Next step is to archive the dump folder generated by `mongodump`. We will use the TAR variable we setup previously for this.
&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Archiving the backup"&lt;/span&gt;&lt;span class="nb"&gt;tar&lt;/span&gt; &lt;span class="nt"&gt;-zcvf&lt;/span&gt; &lt;span class="nv"&gt;$TAR&lt;/span&gt; &lt;span class="nt"&gt;-C&lt;/span&gt; &lt;span class="nv"&gt;$DEST&lt;/span&gt; .echo &lt;span class="s2"&gt;"Done archiving"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;
Now we upload this archive to our s3 bucket. “a/path/you/desire/” is a relative path of your choice within your s3 bucket. Easy as cake!
&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Uploading to s3 bucket"&lt;/span&gt; &lt;span class="nv"&gt;$BUCKETaws&lt;/span&gt; s3 &lt;span class="nb"&gt;cp&lt;/span&gt; &lt;span class="nv"&gt;$TAR&lt;/span&gt; s3://&lt;span class="nv"&gt;$BUCKET&lt;/span&gt;/a/path/you/desire/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;
If all went well the backup should now appear on your s3 bucket.&lt;br&gt;
&lt;b&gt;NOTE: Make sure to download and restore the backup on your local database to make sure all your data are there once restored
&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;
&lt;b&gt;BUT&lt;/b&gt; we still have a few more steps to get through..
We first need to cleanup our backup directory and the following lines handles exactly that!
&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Cleaning up temp files"&lt;/span&gt;
&lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; &lt;span class="nv"&gt;$TAR&lt;/span&gt;    &lt;span class="c"&gt;# Remove tar file&lt;/span&gt;
&lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="nt"&gt;-rf&lt;/span&gt; &lt;span class="nv"&gt;$DEST&lt;/span&gt;  &lt;span class="c"&gt;# Remove backup directory&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Done cleaning up!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;
Now we’re done writing the script. Let’s log it
&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Backup available at https://s3.amazonaws.com/&lt;/span&gt;&lt;span class="nv"&gt;$BUCKET&lt;/span&gt;&lt;span class="s2"&gt;/a/path/you/desire/&lt;/span&gt;&lt;span class="nv"&gt;$DBNAME&lt;/span&gt;&lt;span class="s2"&gt;-&lt;/span&gt;&lt;span class="nv"&gt;$TIME&lt;/span&gt;&lt;span class="s2"&gt;.tar.gz"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;




&lt;br&gt;
&lt;h2&gt;Automating the script&lt;/h2&gt;

&lt;p&gt;
Now let’s automate this script using cron and we’re done!&lt;br&gt;
Firstly, open a crontab using the below command in the terminal (Select a preferred text editor of your choice if prompted, I usually use nano).
&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;crontab &lt;span class="nt"&gt;-e&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;
Now at the end of the file add the following line. It will set our cron job to run everyday at 01:00 a.m. based on System date/time. You can set it to your preference. A good reference is &lt;a href="https://crontab.guru/#0_1_*_*_*"&gt;crontab.guru&lt;/a&gt; if you’re not familiar with the notation! And in case you’re wondering, the “&amp;gt;&amp;gt; backup_log” at the end will append all your &lt;b&gt;echo&lt;/b&gt; output to a file named backup_log in the same directory as the script.
&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 1 * * * path/to/backup/script.sh &amp;gt;&amp;gt; backup_log
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;
It is advised to initially set it to run around every 10 minutes in order to test run whether your script works flawlessly with cron. If your cron job fails in any case, you’d have wasted your time following this tutorial and writing that beautiful work of art(the backup script)!
&lt;/p&gt;

&lt;p&gt;
So that’s it. If you’ve clearly followed the steps, you should have a fine working automated backup cycle up and running now for your mongo db. So go ahead and sip a well-deserved cup of coffee while your script does all the work for you.
&lt;/p&gt;

&lt;p&gt;&lt;b&gt;
Cheers!&lt;/b&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
