<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sai Tarun Desu on Medium]]></title>
        <description><![CDATA[Stories by Sai Tarun Desu on Medium]]></description>
        <link>https://medium.com/@saitarundesu?source=rss-f21095b0a415------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*AqTQMwivWXnG0TCv</url>
            <title>Stories by Sai Tarun Desu on Medium</title>
            <link>https://medium.com/@saitarundesu?source=rss-f21095b0a415------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 04:40:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@saitarundesu/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Day 10: Automating Website Backups with Bash]]></title>
            <link>https://blog.stackademic.com/day-10-automating-website-backups-with-bash-d48918a9d1f5?source=rss-f21095b0a415------2</link>
            <guid isPermaLink="false">https://medium.com/p/d48918a9d1f5</guid>
            <category><![CDATA[sysadmin]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[kodekloudengineer]]></category>
            <category><![CDATA[bash]]></category>
            <category><![CDATA[linux]]></category>
            <dc:creator><![CDATA[Sai Tarun Desu]]></dc:creator>
            <pubDate>Tue, 23 Jun 2026 11:25:20 GMT</pubDate>
            <atom:updated>2026-06-24T07:46:31.821Z</atom:updated>
            <content:encoded><![CDATA[<h3>Day 10: Automating Website Backups with Bash — Linux Bash Scripts</h3><h3><strong>Task:</strong></h3><blockquote><em>The production support team of </em><em>xFusionCorp Industries is working on developing some bash scripts to automate different day to day tasks. One is to create a bash script for taking websites backup. They have a static website running on </em><em>App Server 2 in </em><em>Stratos Datacenter, and they need to create a bash script named </em><em>ecommerce_backup.sh which should accomplish the following tasks. (Also remember to place the script under </em><em>/scripts directory on </em><em>App Server 2).</em></blockquote><blockquote><em>a. Create a zip archive named </em><em>xfusioncorp_ecommerce.zip of </em><em>/var/www/html/ecommerce directory.</em></blockquote><blockquote><em>b. Save the archive in </em><em>/backup/ on </em><em>App Server 2. This is a temporary storage, as backups from this location will be clean on weekly basis. Therefore, we also need to save this backup archive on </em><em>Nautilus Backup Server.</em></blockquote><blockquote><em>c. Copy the created archive to </em><em>Nautilus Backup Server server in </em><em>/backup/ location.</em></blockquote><blockquote><em>d. Please make sure script won’t ask for password while copying the archive file. Additionally, the respective server user (for example, </em><em>tony in case of </em><em>App Server 1) must be able to run it.</em></blockquote><blockquote><em>e. Do not use sudo inside the script.</em></blockquote><blockquote><em>Note:<br>The zip package must be installed on given App Server before executing the script. This package is essential for creating the zip archive of the website files. Install it manually outside the script.</em></blockquote><blockquote><strong><em>Note: You can find the infrastructure details by clicking on the Details of all Users and Servers button on the top-right section of the page. Attaching the link for reference:</em></strong><em> </em><a href="https://kodekloudhub.github.io/kodekloud-engineer/docs/projects/nautilus#infrastructure-details"><strong><em>https://kodekloudhub.github.io/kodekloud-engineer/docs/projects/nautilus#infrastructure-details</em></strong></a></blockquote><p>Let’s go through the problem statement that we got and try to understand exactly what are we asked to do. With a question like this, the first thing to do is gather the data that’s mentioned.</p><p>So, let’s make a note of the data that we have in hand:</p><ul><li>We have a static website running in App server 2 and we have to create a script (<em>ecommerce_backup.sh) </em>in the /script directory in the app server.</li><li>Now what should this script do:<br>- It must create a zip (compress the content in <em>/var/www/html/ecommerce </em>directory) and must be named as <em>xfusioncorp_ecommerce.zip . </em>This archive must be saved in the<em> /backup</em> folder in the app server.<em><br>- </em>That means the <em>/scripts</em> and the <em>/backup </em>folder must be owned by the app sever user (steve in our case) and must have the read, write and executable permissions.<br>- Then the script must move the <em>xfusioncorp_ecommerce.zip </em>from /backup in the app server to /backup in the backup server. And do note, that, it mustn’t ask for the password or any such details when moving the data to the backup server, which means that we must set up the passwordless connection to the backup server from the app server.</li></ul><p>Now that we have the data in hand and have understood the problem statement, let’s work on it!</p><h3>Step 1: Validate the permissions of all the mentioned directories and check if the necessary packages are installed:</h3><p>We have 3 directories to validate:<br>- /backup in the app server.<br>- /script in the app server.<br>- /backup in the backup server.</p><pre># APP SERVER:<br>ssh &lt;app-server-user&gt;@&lt;app-server-hostname&gt; (app-server-user=steve, app-server-hostname=stapp02)<br><br># Check the permission for /scripts and /backup for the app-server:<br>[steve@stapp02 /]$ cd /<br>[steve@stapp02 /]$ ls -ld /scripts /backup<br>drwxrwxrwx 2 root root 4096 Nov 10 08:36 /backup<br>drwxrwxrwx 2 root root 4096 Nov 10 08:36 /scripts<br><br># Change the ownership of these directories to the steve user:<br>[steve@stapp02 /]$ sudo chown steve:steve /scripts /backup<br>[steve@stapp02 /]$ ls -ld /scripts /backup<br>drwxrwxrwx 2 steve steve 4096 Nov 10 08:36 /backup<br>drwxrwxrwx 2 steve steve 4096 Nov 10 08:36 /scripts<br><br># Let&#39;s check if zip package is installed in app server. If not, we have to install it manually.<br>[steve@stapp02 /]$ which zip<br>/usr/bin/which: no zip in (/home/steve/.local/bin:/home/steve/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin)<br>[steve@stapp02 /]$ sudo yum install zip</pre><pre># BACKUP SERVER:<br>ssh &lt;backup-server-user&gt;@&lt;backup-server-hostname&gt; (backup-server-user=clint, backup-server-hostname=stbkp01)<br><br># Check the permission for /backup for the backup-server:<br>[clint@stbkp01 ~]$ cd /<br>[clint@stbkp01 /]$ ls -ld /backup<br>drwxrwxrwx 2 root root 4096 Nov 10 08:36 /backup<br><br># Change the ownership of this directory to the &lt;backup-server-user&gt; so that copying the file from app-server to backup-server will work:<br>[clint@stbkp01 /]$ sudo chown clint:clint /backup<br>[clint@stbkp01 /]$ ls -ld /backup<br>drwxrwxrwx 2 clint clint 4096 Nov 10 08:36 /backup</pre><p>Perfect! We’ve validated the ownership and the existence of the packages. Let’s move on to the next step.</p><h3>Step 2: Setting up the passwordless connection from the app server to the backup server.</h3><p>To move the file from the app server to the backup server, you need to setup passwordless connection. We want <strong>App Server (</strong><strong>steve) → Backup Server (</strong><strong>clint)</strong> via SSH without a password, so that the script can move the archived file within the servers.</p><pre># Generate both the private and public key pair in the app server.<br><br>[steve@stapp02 scripts]$ ssh-keygen -t rsa -b 4096<br><br>Generating public/private rsa key pair.<br>Enter file in which to save the key (/home/steve/.ssh/id_rsa): <br>Enter passphrase (empty for no passphrase): <br>Enter same passphrase again: <br>Your identification has been saved in /home/steve/.ssh/id_rsa<br>Your public key has been saved in /home/steve/.ssh/id_rsa.pub<br><br># Place the public key (id_rsa.pub) into the stbkp01 user&#39;s authorized_keys file:<br>ssh-copy-id clint@stbkp01<br># Now the backup server knows, if someone connects using the private key corresponding to this public key, let them in.<br><br>#Validate the passwordless connection:<br>ssh clint@stbkp01<br># You should be able to ssh into the server without entering the password.</pre><h3>Step 3: Create the ecommerce_backup.sh.</h3><p>Make sure you create the script inside the /scripts directory in the app server. The name of the script must be according to what’s mentioned in your problem statement.</p><pre>#!/bin/bash<br># ==============================================<br># ecommerce_backup.sh<br># Purpose: Backup website ecommerce to local and remote backup server<br># ==============================================<br># --- Configuration ---<br>SRC_DIR=&quot;/var/www/html/ecommerce&quot;<br>BACKUP_DIR=&quot;/backup&quot;<br>ARCHIVE_NAME=&quot;xfusioncorp_media.zip&quot;<br>REMOTE_USER=&quot;clint&quot;<br>REMOTE_HOST=&quot;stbkp01&quot;<br>REMOTE_DIR=&quot;/backup&quot;<br># --- Step 1: Create local backup archive ---<br>echo &quot;[$(date)] Starting backup process...&quot;<br>echo &quot;Creating zip archive from $SRC_DIR ...&quot;<br>zip -r &quot;${BACKUP_DIR}/${ARCHIVE_NAME}&quot; &quot;${SRC_DIR}&quot; &gt; /dev/null 2&gt;&amp;1<br>if [ $? -ne 0 ]; then<br>  echo &quot;Error: Failed to create zip archive. Exiting.&quot;<br>  exit 1<br>fi<br># --- Step 2: Copy archive to remote backup server ---<br>echo &quot;Copying archive to ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_DIR} ...&quot;<br>scp &quot;${BACKUP_DIR}/${ARCHIVE_NAME}&quot; &quot;${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_DIR}/&quot; &gt; /dev/null 2&gt;&amp;1<br>if [ $? -ne 0 ]; then<br>  echo &quot;Error: Failed to copy archive to remote server. Exiting.&quot;<br>  exit 2<br>fi<br>echo &quot;[$(date)] Backup completed successfully.&quot;<br>exit 0</pre><p>Save this file. Let’s just go through the script to understand what exactly it does:<br>- It zips the content of the <em>${SRC_DIR}</em> and move it to <em>${BACKUP_DIR}/${ARCHIVE_NAME} </em>in the app server (temp location).<br>- It then moves the copy the zipped/archived file from <em>${BACKUP_DIR}/${ARCHIVE_NAME} </em>in the app server to the <em>${BACKUP_DIR}/${ARCHIVE_NAME} </em>in the backup server.</p><p><strong>What this script actually does, line by line:</strong></p><ul><li><strong>Configuration block</strong> — sets up variables for the source directory, local backup path, archive name, and remote server details. Keeping these as variables (instead of hardcoding paths everywhere) makes the script easy to reuse for other websites or servers later.</li><li><strong>Step 1 (zip creation)</strong> — uses zip -r to recursively compress the entire ecommerce directory into the local /backup folder. The $? check right after verifies the command actually succeeded; if it didn&#39;t, the script prints an error and exits immediately instead of carrying on with a broken archive.</li><li><strong>Step 2 (remote copy)</strong> — uses scp to securely copy that archive to the backup server. Because passwordless SSH was set up in Step 2 of the walkthrough, this runs without any manual input. Again, there&#39;s an exit-code check so the script fails loudly if the transfer doesn&#39;t go through.</li><li><strong>Logging</strong> — the echo &quot;[$(date)] ...&quot; lines timestamp the start and end of the process, which is handy if this script ever runs on a cron schedule and you need to check logs later.</li></ul><pre># Give the executable permission so that the user can run this script.<br>[steve@stapp02 scripts]$ sudo chmod 700 ecommerce_backup.sh <br>[sudo] password for steve: <br>[steve@stapp02 scripts]$ ls -la<br>total 12<br>drwxrwxrwx 2 steve steve 4096 Nov 10 09:12 .<br>drwxr-xr-x 1 root  root  4096 Nov 10 09:28 ..<br>-rwx------ 1 steve steve 1064 Nov 10 09:12 ecommerce_backup.sh<br><br># Run the script.<br>[steve@stapp02 scripts]$ /scripts/ecommerce_backup.sh <br>[Mon Nov 10 09:28:48 UTC 2025] Starting backup process...<br>Creating zip archive from /var/www/html/ecommerce ...<br>Copying archive to clint@stbkp01:/backup ...<br>[Mon Nov 10 09:28:48 UTC 2025] Backup completed successfully.</pre><p>Let’s move on to the next step.</p><h3>Step 4: Validate if the backup is created successfully in both the temp directory in app server and the /backup directory in the backup server.</h3><pre># Validating in the app-server.<br>[steve@stapp02 scripts]$ ls -la /backup/<br>total 12<br>drwxrwxrwx 2 steve steve 4096 Nov 10 09:28 .<br>drwxr-xr-x 1 root  root  4096 Nov 10 09:28 ..<br>-rw-r--r-- 1 steve steve  623 Nov 10 09:28 xfusioncorp_media.zip<br><br># Unzip the archived file and validate the content.<br>[steve@stapp02 scripts]$ unzip -l /backup/xfusioncorp_media.zip <br>Archive:  /backup/xfusioncorp_media.zip<br>  Length      Date    Time    Name<br>---------  ---------- -----   ----<br>        0  11-10-2025 08:15   var/www/html/ecommerce/<br>       43  11-10-2025 08:36   var/www/html/ecommerce/index.html<br>        0  11-10-2025 08:15   var/www/html/ecommerce/.gitkeep<br>---------                     -------<br>       43                     3 files</pre><pre>#Validating in the backup-server.<br>[steve@stapp02 scripts]$ ssh clint@stbkp01<br>Last login: Mon Nov 10 09:26:42 2025 from 172.16.238.11<br>[clint@stbkp01 ~]$ ls -la /backup/<br>total 12<br>drwxrwxrwx 2 clint clint 4096 Nov 10 09:28 .<br>drwxr-xr-x 1 root  root  4096 Nov 10 09:29 ..<br>-rw-r--r-- 1 clint clint  623 Nov 10 09:28 xfusioncorp_media.zip<br><br># Unzip the archived file and validate the content.<br>[clint@stbkp01 ~]$ unzip -l /backup/xfusioncorp_media.zip<br>Archive:  /backup/xfusioncorp_media.zip<br>  Length      Date    Time    Name<br>---------  ---------- -----   ----<br>        0  11-10-2025 08:15   var/www/html/ecommerce/<br>       43  11-10-2025 08:36   var/www/html/ecommerce/index.html<br>        0  11-10-2025 08:15   var/www/html/ecommerce/.gitkeep<br>---------                     -------<br>       43                     3 files</pre><p>As observed, the backup is created successfully from our script in both the servers.</p><p>Also, do note that, sometimes the problem statement might ask to work with the <em>/var/www/html/media </em>directory. So, do read the problem statement carefully, before attempting!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d48918a9d1f5" width="1" height="1" alt=""><hr><p><a href="https://blog.stackademic.com/day-10-automating-website-backups-with-bash-d48918a9d1f5">Day 10: Automating Website Backups with Bash</a> was originally published in <a href="https://blog.stackademic.com/">Stackademic</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>