User Input in Bash

Ask the User for Input

#!/bin/bash
# Ask the user for their name
echo Hello, who am I talking to?
read varname
echo It\'s nice to meet you $varname

More variables

#!/bin/bash
# Demonstrate how read actually works
echo What cars do you like?
read car1 car2 car3
echo Your first car was: $car1
echo Your second car was: $car2
echo Your third car was: $car3

More with Read

#!/bin/bash
# Ask the user for login details
read -p 'Username: ' uservar
read -sp 'Password: ' passvar
echo
echo Thankyou $uservar we now have your login details

-p allows you to specify a prompt and -s makes the input silent

References
https://ryanstutorials.net/bash-scripting-tutorial/bash-input.php

Secure Apache with Let’s Encrypt on Ubuntu 18.04

Installing Certbot

sudo add-apt-repository ppa:certbot/certbot
sudo apt install python-certbot-apache

Set Up the SSL Certificate

Certbot needs to be able to find the correct virtual host in your Apache configuration for it to automatically configure SSL. Specifically, it does this by looking for a ServerName directive that matches the domain you request a certificate for.

Obtaining an SSL Certificate

sudo certbot --apache -d example.com -d www.example.com

This runs certbot with the --apache plugin, using -d to specify the names you’d like the certificate to be valid for.

Verifying Certbot Auto-Renewal

sudo certbot renew --dry-run

References
https://www.digitalocean.com/community/tutorials/how-to-secure-apache-with-let-s-encrypt-on-ubuntu-18-04

Cannot access javascript folder on Apache

Go to /etc/apache2/conf-available/javascript-common.conf, you will find this:

Alias /javascript /usr/share/javascript/
<Directory "/usr/share/javascript/">
     Options FollowSymLinks MultiViews
</Directory>

So you just have to comment this lines (with the # char) (is not recommend to edit directly the file in conf-enabled) to avoid the forbidden error. After that, do this:

a2disconf javascript-common
a2enconf javascript-common

References
https://serverfault.com/questions/274254/cannot-access-javascript-folder

Search files from the terminal on Linux using locate

sudo apt-get install locate

Using locate Command

locate LAMP-Setup.odt
locate "*.html"

Ignore Case Sensitive Locate Outputs

locate -i *text.txt*

Refresh mlocate Database

Since locate command relies on a database called mlocate. The said database needs to be updated regularly for the command utility to work efficiently

sudo updatedb

Display Only Files Present in Your System

locate -i -e *text.txt*

Review Your Locate Database

locate -S

Limit Search Queries to a Specific Number

locate "*.html" -n 20

Display The Number of Matching Entries

locate -c [tecmint]*

References
https://www.howtoforge.com/tutorial/linux-search-files-from-the-terminal/
https://www.tecmint.com/linux-locate-command-practical-examples/

Scheduled backup MongoDB database using mongodump and cron

mkdir backup
touch monitoring.sh
#!/bin/bash
 
currentDate=`date +"%Y%m%d%H%M"`
dirName="monitoring_$currentDate"
filePath=/home/mahmood/backup/$dirName
mkdir $filePath
mongodump --db monitoring --out $filePath
sudo chmod +x monitoring.sh
sudo crontab -e
30 4 * * * /home/mahmood/backup/monitoring.sh

References
https://stackoverflow.com/questions/35574603/run-cron-job-everyday-at-specific-time
https://tecadmin.net/get-current-date-and-time-in-bash/
https://askubuntu.com/questions/350861/how-to-set-a-cron-job-to-run-a-shell-script
https://coderwall.com/p/hdmmnq/easy-automatic-nightly-backups-for-mongodb