<?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>Forem: Abhimanyu Babbar</title>
    <description>The latest articles on Forem by Abhimanyu Babbar (@babbarshaer).</description>
    <link>https://forem.com/babbarshaer</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%2F175689%2F70f2f937-70d6-4049-a497-c30c18b99bc1.jpeg</url>
      <title>Forem: Abhimanyu Babbar</title>
      <link>https://forem.com/babbarshaer</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/babbarshaer"/>
    <language>en</language>
    <item>
      <title>SFTP to S3 Backup</title>
      <dc:creator>Abhimanyu Babbar</dc:creator>
      <pubDate>Mon, 15 Jul 2019 17:05:18 +0000</pubDate>
      <link>https://forem.com/babbarshaer/sftp-to-s3-backup-2e18</link>
      <guid>https://forem.com/babbarshaer/sftp-to-s3-backup-2e18</guid>
      <description>&lt;p&gt;In our company, we had a simple SFTP Server where third parties used to supply us with reports, files etc. In order to backup the information onto S3 where we could start processing the files, I wrote a simple utility in &lt;code&gt;Go&lt;/code&gt; to achieve the same &lt;a href="https://github.com/babbarshaer/sftp-s3-backup"&gt;sftp-s3-backup&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The purpose of this article is to walk through a simple process of backing up the files present in SFTP onto S3.&lt;/p&gt;

&lt;h1&gt;
  
  
  S3 Login Credentials
&lt;/h1&gt;

&lt;p&gt;In order to backup onto S3 our system needs to be able to login to the platform. Below environment variables needs to be set for the program to automatically pick them up. The variables are as follows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;AWS_ACCESS_KEY_ID&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;AWS_SECRET_ACCESS_KEY&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;AWS_REGION&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;AWS_SESSION_TOKEN&lt;/code&gt; // In case you have 2 factor auth enabled.&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Simple Backup
&lt;/h1&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (
  "log",
  backup "github.com/babbarshaer/sftp-s3-backup"
)

func main() {

  config := backup.Config{
    User:              "user",
    Address:           "address",
    Port:              22,
    PublicKeyLocation: "/home/user/.ssh/id_rsa.pub",
  }

  client := backup.Client(config)
  err = client.Init()
  if err != nil {
    log.Fatalf("Unable to initialize the client: %s", err.Error())
  }

  defer client.Close()

  err := client.Backup(
    "/data/user/uploads",
    "aws.bucket",
    backup.DefaultPathTransformer)

  // If we don't want the default path transformer
  // we can override it with our own implementation.

  if err != nil {
    log.Fatalf("Unable to backup, err: %s", err.Error())
  }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We will go through each part of the program step by step:&lt;/p&gt;

&lt;p&gt;We start by creating a &lt;code&gt;config&lt;/code&gt; object which mainly contains the credentials of the sftp server who's directory we need to backup onto S3.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;config := backup.Config{
    User:              "user",
    Address:           "address",
    Port:              22,
    PublicKeyLocation: "/home/user/.ssh/id_rsa.pub",
  }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We then create a new backup &lt;code&gt;client&lt;/code&gt; object by supplying in the config. We initialize the client by calling the &lt;code&gt;Init()&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;client := backup.Client(config)
client.Init()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If everything goes smoothly, we should be able to establish a sftp connection after this point. Otherwise, it would return an error. Handling of error depends upon the user at this point.&lt;/p&gt;

&lt;p&gt;We should always remember to free up the client connection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;defer client.Close()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Finally, we can now perform a directory backup. This method will backup all the files in the directory to the corresponding bucket in s3. We supply an additional &lt;code&gt;transformer&lt;/code&gt; variable which dictates how the path needs to be transformed when storing the object from sftp onto s3.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;directoryToBackup := "/data/user/uploads"
awsBucket := "aws.bucket"
transformer := backup.DefaultPathTransformer

err := client.Backup(
    directoryToBackup,
    awsBucket,
    transformer)

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



&lt;p&gt;A user can supply there own &lt;code&gt;transformer function&lt;/code&gt; as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func MyTransformer(path string) string {
  newPath := path
  // computation ...
  return newPath
}

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






</description>
      <category>aws</category>
      <category>sftp</category>
      <category>go</category>
      <category>github</category>
    </item>
  </channel>
</rss>
