<?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: KM</title>
    <description>The latest articles on DEV Community by KM (@chyn_km).</description>
    <link>https://dev.to/chyn_km</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%2F193038%2F22d18182-51ac-413d-8cdb-aaa54d8fc3f9.jpg</url>
      <title>DEV Community: KM</title>
      <link>https://dev.to/chyn_km</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chyn_km"/>
    <language>en</language>
    <item>
      <title>MySQL DB backup using Bash script</title>
      <dc:creator>KM</dc:creator>
      <pubDate>Sun, 05 Jan 2020 15:53:24 +0000</pubDate>
      <link>https://dev.to/chyn_km/mysql-db-backup-using-bash-script-2bg4</link>
      <guid>https://dev.to/chyn_km/mysql-db-backup-using-bash-script-2bg4</guid>
      <description>&lt;p&gt;Backups are important for any data. Taking DB backups for MySQL/MariaDB is a cinch with the following script.&lt;/p&gt;

&lt;h1&gt;
  
  
  Bash script
&lt;/h1&gt;

&lt;p&gt;Create a file called &lt;code&gt;backup_script.sh&lt;/code&gt;. Replace the credentials and path to suit your system.&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="nv"&gt;USER&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'username'&lt;/span&gt;
&lt;span class="nv"&gt;PASSWORD&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'password'&lt;/span&gt;
&lt;span class="nv"&gt;DATABASE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'ALL_DB'&lt;/span&gt;
&lt;span class="c"&gt;# Directory should always be mentioned with a trailing slash&lt;/span&gt;
&lt;span class="nv"&gt;DIRECTORY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'/path/for/db_backup/'&lt;/span&gt;
&lt;span class="nv"&gt;DOW&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt; +&lt;span class="s2"&gt;"%A"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;

&lt;span class="c"&gt;# Delete backups which are 7 days old&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;$DIRECTORY$DATABASE&lt;/span&gt;-&lt;span class="nv"&gt;$DOW&lt;/span&gt;.sql.gz &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; /dev/null 2&amp;gt;&amp;amp;1

&lt;span class="c"&gt;#Backup the DB and Compress it&lt;/span&gt;
/usr/bin/mysqldump &lt;span class="nt"&gt;--user&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$USER&lt;/span&gt; &lt;span class="nt"&gt;--password&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$PASSWORD&lt;/span&gt; &lt;span class="nt"&gt;--host&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$HOST&lt;/span&gt; &lt;span class="nt"&gt;--all-databases&lt;/span&gt; &lt;span class="nt"&gt;--skip-lock-tables&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$DIRECTORY$DATABASE&lt;/span&gt;-&lt;span class="nv"&gt;$DOW&lt;/span&gt;.sql
/bin/gzip &lt;span class="nv"&gt;$DIRECTORY$DATABASE&lt;/span&gt;-&lt;span class="nv"&gt;$DOW&lt;/span&gt;.sql
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The script will create/keep in total 7 backups, with the day of the week as its suffix. Escape &lt;code&gt;$&lt;/code&gt; sign if its used in the PASSWORD variable.&lt;/p&gt;

&lt;p&gt;Provide execute permission to the script&lt;br&gt;
&lt;code&gt;$ chmod 755 backup_script.sh&lt;/code&gt;&lt;br&gt;
Make sure to verify the backup-file is generated by executing the script:&lt;br&gt;
&lt;code&gt;$ ./backup_script.sh&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  CRON Job
&lt;/h1&gt;

&lt;p&gt;Add the following line to your crontab. This will automatically execute a backup daily at 0400 hours.&lt;br&gt;
&lt;code&gt;0 4 * * * /bin/sh /path/to/backup_script.sh &amp;gt;&amp;gt; /dev/null 2&amp;gt;&amp;amp;1&lt;/code&gt;&lt;/p&gt;

</description>
      <category>bash</category>
      <category>database</category>
      <category>mariadb</category>
      <category>mysql</category>
    </item>
    <item>
      <title>SSHBlack - Securing open linux servers</title>
      <dc:creator>KM</dc:creator>
      <pubDate>Sun, 05 Jan 2020 15:53:21 +0000</pubDate>
      <link>https://dev.to/chyn_km/sshblack-securing-open-linux-servers-57li</link>
      <guid>https://dev.to/chyn_km/sshblack-securing-open-linux-servers-57li</guid>
      <description>&lt;p&gt;&lt;a href="http://www.pettingers.org/code/sshblack.html"&gt;SSHBlack&lt;/a&gt; is a must have utility for open servers in the public network(internet). Download the latest versions from the website. The installation steps are as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;tar&lt;/span&gt; &lt;span class="nt"&gt;-zxf&lt;/span&gt; sshblackv281.tar.gz
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;mv &lt;/span&gt;sshblack /usr/local/sshblack
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; /usr/local/sshblack
&lt;span class="c"&gt;# provide excutable permission for the script&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;chmod &lt;/span&gt;755 sshblack.pl
&lt;span class="c"&gt;# Create a new chain BLACKLIST&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;iptables &lt;span class="nt"&gt;-N&lt;/span&gt; BLACKLIST
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Next we need to update the &lt;code&gt;sshblack.pl&lt;/code&gt;. Open your favourite editor and update the variables&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight perl"&gt;&lt;code&gt;&lt;span class="c1"&gt;# this will run the process in the background&lt;/span&gt;
&lt;span class="k"&gt;my&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$DAEMONIZE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;1&lt;/span&gt;&lt;span class="p"&gt;';&lt;/span&gt;
&lt;span class="c1"&gt;# The INPUT log file you want to monitor; If Ubuntu OS its '/var/log/auth.log'; if its RedHat based OS '/var/log/secure';&lt;/span&gt;
&lt;span class="k"&gt;my&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$LOG&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/var/log/auth.log&lt;/span&gt;&lt;span class="p"&gt;';&lt;/span&gt;
&lt;span class="c1"&gt;# Update your static IP which should never be blacklisted, displayed as WWW.XXX.YYY.ZZZ;&lt;/span&gt;
&lt;span class="k"&gt;my&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$LOCALNET&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;^(?:127\.0\.0\.1|WWW\.XXX\.YYY\.ZZZ)&lt;/span&gt;&lt;span class="p"&gt;';&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Save the file. &lt;code&gt;./sshblack.pl&lt;/code&gt; will start the script as a background process. The &lt;code&gt;/var/log/sshblacklisting&lt;/code&gt; file will log the IP information of clients accessing/attacking the server.&lt;/p&gt;

&lt;p&gt;Once the server is attacked more than 5 times(default value of variable $MAXHITS), a block rule is added to iptables with the IP information. This prevents new connections to the server from the attacker, in turn preventing the server from brute force attempts. A sample of IP’s which is blacklisted in my server using the script is listed below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;iptables &lt;span class="nt"&gt;-L&lt;/span&gt;
Chain BLACKLIST &lt;span class="o"&gt;(&lt;/span&gt;1 references&lt;span class="o"&gt;)&lt;/span&gt;
target     prot opt &lt;span class="nb"&gt;source &lt;/span&gt;destination
DROP       all  &lt;span class="nt"&gt;--&lt;/span&gt;  132.232.54.102       anywhere
DROP       all  &lt;span class="nt"&gt;--&lt;/span&gt;  139.59.84.55         anywhere
DROP       all  &lt;span class="nt"&gt;--&lt;/span&gt;  222.187.232.212      anywhere
DROP       all  &lt;span class="nt"&gt;--&lt;/span&gt;  222.187.225.10       anywhere
DROP       all  &lt;span class="nt"&gt;--&lt;/span&gt;  222.187.238.32       anywhere
DROP       all  &lt;span class="nt"&gt;--&lt;/span&gt;  58.241.250.152       anywhere
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you clear the iptables, make sure to clear the text database which keeps track of the attacked IP address.&lt;br&gt;
&lt;code&gt;echo '' &amp;gt; /var/tmp/ssh-blacklist-pending&lt;/code&gt;&lt;/p&gt;

</description>
      <category>linux</category>
      <category>security</category>
      <category>ssh</category>
      <category>iptables</category>
    </item>
    <item>
      <title>emSigner USB DSC token installation and usage on macOS for filing GST</title>
      <dc:creator>KM</dc:creator>
      <pubDate>Sun, 05 Jan 2020 15:52:52 +0000</pubDate>
      <link>https://dev.to/chyn_km/emsigner-usb-dsc-token-installation-and-usage-on-macos-for-filing-gst-52b4</link>
      <guid>https://dev.to/chyn_km/emsigner-usb-dsc-token-installation-and-usage-on-macos-for-filing-gst-52b4</guid>
      <description>&lt;p&gt;Prerequisites: Chrome browser, ePass DSC USB token. I assume that you are using Google Chrome browser.&lt;/p&gt;

&lt;h2&gt;
  
  
  Steps
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Download and install &lt;a href="https://javadl.oracle.com/webapps/download/AutoDL?BundleId=239849_230deb18db3e4014bb8e3e8324f81b43" rel="noopener noreferrer"&gt;Java 8&lt;/a&gt; from &lt;a href="https://www.java.com/en/" rel="noopener noreferrer"&gt;Java website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Since Mac OS doesn't support Java 8, download and install &lt;a href="http://updates-http.cdn-apple.com/2018/macos/031-33898-20171026-7a797e9e-b8de-11e7-b1fe-c14fbda7e146/javaforosx.dmg" rel="noopener noreferrer"&gt;Java for OS X 2017–001&lt;/a&gt; from &lt;a href="https://support.apple.com/kb/dl1572" rel="noopener noreferrer"&gt;Apple website&lt;/a&gt;.  &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fid%3D1g7kO-f3ei4TJg5frUdK-tuf-rPwkqkfF" alt="java-for-osx"&gt;
&lt;/li&gt;
&lt;li&gt;Download and install &lt;a href="https://www.e-mudhra.com/repository/downloads/ePass-Mac.zip" rel="noopener noreferrer"&gt;ePass 2003 (Mac)&lt;/a&gt; from &lt;a href="https://www.e-mudhra.com/repository/" rel="noopener noreferrer"&gt;emudhra website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Download &lt;a href="https://tutorial.gst.gov.in/installers/dscemSigner/emsigner-2.6.dmg" rel="noopener noreferrer"&gt;emSigner for Mac&lt;/a&gt; from &lt;a href="https://www.gst.gov.in/help/docsigner" rel="noopener noreferrer"&gt;GST website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Open &lt;em&gt;emSigner&lt;/em&gt; installation utility and copy the &lt;em&gt;emSigner&lt;/em&gt; folder to Desktop.&lt;/li&gt;
&lt;li&gt;Open &lt;em&gt;Spotlight&lt;/em&gt; (use the keys) – &lt;code&gt;Command+Space&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;In &lt;em&gt;Spotlight&lt;/em&gt;, enter - &lt;code&gt;terminal&lt;/code&gt;. Open the application.&lt;/li&gt;
&lt;li&gt;Once the &lt;code&gt;terminal&lt;/code&gt; application opens, enter - &lt;code&gt;cd Desktop/emSigner&lt;/code&gt;. Press &lt;em&gt;enter&lt;/em&gt; key.&lt;/li&gt;
&lt;li&gt;Execute the following in the next line - &lt;code&gt;java -jar emsigner_WS_OMM.jar&lt;/code&gt;. The following message will be displayed:&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fid%3D17_iM1CZbFfQKlQgag5mTvlhdQE6gVeVA" alt="emsigner-ok"&gt;
&lt;/li&gt;
&lt;li&gt;Go to MacOS &amp;gt; System Preferences &amp;gt; Java. In the &lt;em&gt;Java Control Panel&lt;/em&gt;, open the &lt;em&gt;Security&lt;/em&gt; tab.&lt;/li&gt;
&lt;li&gt;Click on &lt;em&gt;Edit Site List&lt;/em&gt;. Add &lt;code&gt;https://127.0.0.1:1585&lt;/code&gt; in the exceptions list. Save and close the panel.&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdrive.google.com%2Fuc%3Fid%3D10lFj2kKYdAxc2MivsW9P5iAK8pnT9z4B" alt="java-control-panel"&gt;
&lt;/li&gt;
&lt;li&gt;Restart the machine.&lt;/li&gt;
&lt;li&gt;Execute steps (6), (7), (8) and (9) to run the &lt;em&gt;emSigner&lt;/em&gt; listner application.&lt;/li&gt;
&lt;li&gt;Insert the USB DSC token into the USB slot.&lt;/li&gt;
&lt;li&gt;Open &lt;em&gt;Spotlight&lt;/em&gt; (use the keys) – &lt;code&gt;Command+Space&lt;/code&gt;. Enter &lt;code&gt;EnterSafeUserMgr&lt;/code&gt;. It will display the DSC token.&lt;/li&gt;
&lt;li&gt;Open this URL in your Chrome browser &lt;code&gt;https://127.0.0.1:1585&lt;/code&gt;, Click on &lt;strong&gt;Advanced&lt;/strong&gt; button &amp;amp; again click &lt;strong&gt;Add certificate exception&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Now file your GST using the DSC key.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Since the application is installed on your machine, you don't need to execute all the steps to file GST again. For each month's filing, you only need to execute the steps 13 through 17.&lt;/p&gt;

</description>
      <category>emsigner</category>
      <category>gst</category>
    </item>
    <item>
      <title>Simple Laravel DB backups for MySQL/MariaDB</title>
      <dc:creator>KM</dc:creator>
      <pubDate>Mon, 19 Aug 2019 15:13:54 +0000</pubDate>
      <link>https://dev.to/chyn_km/simple-laravel-db-backups-for-mysql-mariadb-22mj</link>
      <guid>https://dev.to/chyn_km/simple-laravel-db-backups-for-mysql-mariadb-22mj</guid>
      <description>&lt;p&gt;Databases(DB) persists all critical information related to a web application. Hence DB backups are a necessity. I will post a simple code snippet suitable for Laravel applications to backup your DB to &lt;a href="https://aws.amazon.com/s3/"&gt;AWS S3&lt;/a&gt;,. I assume that you are using EC2 machine for hosting the Laravel application.&lt;/p&gt;

&lt;p&gt;Since configuring and setting up a S3 bucket is out of scope for the article, I will outline the steps for reference&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Start by creating a S3 bucket(eg: &lt;em&gt;db-backups&lt;/em&gt;) which is private. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Enable versioning for the bucket&lt;/strong&gt;, so that overwrite will still preserve the backups.&lt;/li&gt;
&lt;li&gt;Complete creating the bucket and note down ARN(eg: &lt;em&gt;arn:aws:s3:::db-backups/*&lt;/em&gt;).&lt;/li&gt;
&lt;li&gt;Create an IAM user(eg: &lt;em&gt;s3user&lt;/em&gt;) having programatic write access without selecting any existing policies. &lt;/li&gt;
&lt;li&gt;Note down the API access/secret keys and the ARN of the user(eg: &lt;em&gt;arn:aws:iam::8XX45XXXX57:user/s3user&lt;/em&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Under the &lt;em&gt;Permissions&lt;/em&gt; tab of the IAM user, add the following as an Inline policy.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "Version":"2012-10-17",
    "Statement":[
       {
          "Effect":"Allow",
          "Action":[
             "s3:PutObject"
          ],
          "Resource":[
             "arn:aws:s3:::db-backups/*"
          ]
       }
    ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the S3 bucket policy, add the following:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    {
       "Version": "2012-10-17",
       "Statement": [
          {
             "Sid": "statement1",
             "Effect": "Allow",
             "Principal": {
                "AWS": "arn:aws:iam::8XX45XXXX57:user/s3user"
             },
             "Action": [
                 "s3:PutObject"
             ],
             "Resource": [
                "arn:aws:s3:::db-backups/*"
             ]
          }
       ]
    }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Make sure to update the &lt;em&gt;user&lt;/em&gt;, &lt;em&gt;bucket&lt;/em&gt; ARN's. This IAM user will only be able to write to the bucket. No other permissions are granted for security reasons.&lt;/p&gt;

&lt;p&gt;Coming back to the main topic, DB backups in Laravel. If you are using RDS as the DB store, EC2 machine won't have the &lt;code&gt;mysqldump&lt;/code&gt; command. Hence, please install them using (Ubuntu distros)&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo apt-get install mysql-client&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Once installed, execute the following commands in shell and note the path&lt;br&gt;
&lt;code&gt;which mysqldump&lt;/code&gt; (eg: &lt;em&gt;/usr/bin/mysqldump&lt;/em&gt;) &amp;amp;&lt;br&gt;
&lt;code&gt;which gzip&lt;/code&gt; (eg: &lt;em&gt;/bin/gzip&lt;/em&gt;)&lt;/p&gt;

&lt;p&gt;Update the &lt;code&gt;.env&lt;/code&gt; file variables of the Laravel application&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AWS_ACCESS_KEY_ID=XXXXXXX
AWS_SECRET_ACCESS_KEY=XXXXXXX
AWS_DEFAULT_REGION=eu-central-1
DB_AWS_BUCKET=db-backups

MYSQLDUMP_EXE=/usr/bin/mysqldump
GZIP_EXE=/bin/gzip
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Read through the &lt;a href="https://laravel.com/docs/5.8/filesystem"&gt;Laravel documentation&lt;/a&gt; and install packages for S3 access. Create a new &lt;em&gt;disk&lt;/em&gt; in &lt;code&gt;config/filesystems.php&lt;/code&gt; file, eg:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        's3db' =&amp;gt; [
            'driver' =&amp;gt; 's3',
            'key' =&amp;gt; env('AWS_ACCESS_KEY_ID'),
            'secret' =&amp;gt; env('AWS_SECRET_ACCESS_KEY'),
            'region' =&amp;gt; env('AWS_DEFAULT_REGION'),
            'bucket' =&amp;gt; env('DB_AWS_BUCKET'),
        ],
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To utilise &lt;a href="https://laravel.com/docs/5.8/configuration#configuration-caching"&gt;Laravel configuration caching&lt;/a&gt;, I have created a file called &lt;code&gt;config/env.php&lt;/code&gt;, where custom ENV variables are configured. Its similar to &lt;code&gt;config/app.php&lt;/code&gt;, except it holds variables which are newly introduced in the application. A sample follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php 

return [
    'mysqldump_exe' =&amp;gt; env('MYSQLDUMP_EXE'),
    'gzip_exe' =&amp;gt; env('GZIP_EXE'),
];
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I prefer to create custom classes, under the &lt;code&gt;app/Classes&lt;/code&gt; directory. Feel free to use yours, but change the namespace of the following file accordingly. Create a file &lt;code&gt;app/Classes/DbBackup.php&lt;/code&gt;. The code is self explanatory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

namespace App\Classes;

use Illuminate\Support\Facades\Storage;

class DbBackup
{
    private $fileName, $tmpFile, $gzipFile;

    /**
     * Initialise the variables
     *
     * @return void
     */
    public function __construct()
    {
        $this-&amp;gt;fileName = config('database.connections.mysql.database').'-'.date('Y-m-d').'.sql';
        $this-&amp;gt;tmpFile = '/tmp/'.$this-&amp;gt;fileName;
        $this-&amp;gt;gzipFile = $this-&amp;gt;tmpFile.'.gz';
    }

    /**
     * Create the backup
     *
     * @return object this
     */
    protected function createBackup()
    {
        exec(config('env.mysqldump_exe').
            ' --user='.config('database.connections.mysql.username').
            ' --host='.config('database.connections.mysql.host').
            ' --password='.config('database.connections.mysql.password').
            ' --databases '.config('database.connections.mysql.database').
            ' &amp;gt; '.$this-&amp;gt;tmpFile);

        exec(config('env.gzip_exe').' '.$this-&amp;gt;tmpFile);

        return $this;
    }

    /**
     * Upload to S3
     *
     * @return object this
     */
    protected function uploadToS3()
    {
        Storage::disk('s3db')
            -&amp;gt;put($this-&amp;gt;fileName.'.gz', fopen($this-&amp;gt;gzipFile, 'r'));

        return $this;
    }

    /**
     * Delete the temp file
     *
     * @return object this
     */
    protected function removeBackup()
    {
        sleep(2);
        unlink($this-&amp;gt;gzipFile);
    }

    /**
     * Method which calls all sub functions
     *
     * @return void
     */
    public function init()
    {
        $this-&amp;gt;createBackup()
            -&amp;gt;uploadToS3()
            -&amp;gt;removeBackup();
    }

}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, we require the DB backups to be run daily, only in Production systems. Hence update the method in &lt;code&gt;app/Console/Kernel.php&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    protected function schedule(Schedule $schedule)
    {
        // this code will create the automatic DB backups.
        $schedule-&amp;gt;call('App\Classes\DbBackup@init')
            -&amp;gt;daily()
            -&amp;gt;environments(['production']);
    }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The above code will only run if the &lt;a href="https://laravel.com/docs/5.8/scheduling#introduction"&gt;Laravel scheduler&lt;/a&gt; is configured. &lt;/p&gt;

&lt;p&gt;That's it, folks! DB backups will be created &amp;amp; saved to S3 daily at midnight.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>sql</category>
      <category>php</category>
    </item>
  </channel>
</rss>
