Sunday, March 13, 2022

How to Schedule a Backup of All MySQL Databases on Ubuntu 16.04

How to Schedule a Backup of All MySQL Databases on Ubuntu 16.04

By

This tutorial will show you how to create a shell script that will backup all of your MySQL databases and how to schedule the backup to run daily.


Step 1: Create the Backup Script

Create a folder to store your backup script in. I suggest /scripts for this example:

sudo mkdir /scripts

Create a file called mysql-backup.sh inside the scripts folder:

sudo vim /scripts/mysql-backup.sh

Add the following code to the file and save it:

#!/bin/bash
#----------------------------------------
# OPTIONS
#----------------------------------------
USER='root'       # MySQL User
PASSWORD='webdev' # MySQL Password
DAYS_TO_KEEP=0    # 0 to keep forever
GZIP=1            # 1 = Compress
BACKUP_PATH='/backups/mysql'
#----------------------------------------

# Create the backup folder
if [ ! -d $BACKUP_PATH ]; then
  mkdir -p $BACKUP_PATH
fi

# Get list of database names
databases=`mysql -u $USER -p$PASSWORD -e "SHOW DATABASES;" | tr -d "|" | grep -v Database`

for db in $databases; do

  if [ $db == 'information_schema' ] || [ $db == 'performance_schema' ] || [ $db == 'mysql' ] || [ $db == 'sys' ]; then
    echo "Skipping database: $db"
    continue
  fi
  
  date=$(date -I)
  if [ "$GZIP" -eq 0 ] ; then
    echo "Backing up database: $db without compression"      
    mysqldump -u $USER -p$PASSWORD --databases $db > $BACKUP_PATH/$date-$db.sql
  else
    echo "Backing up database: $db with compression"
    mysqldump -u $USER -p$PASSWORD --databases $db | gzip -c > $BACKUP_PATH/$date-$db.gz
  fi
done

# Delete old backups
if [ "$DAYS_TO_KEEP" -gt 0 ] ; then
  echo "Deleting backups older than $DAYS_TO_KEEP days"
  find $BACKUP_PATH/* -mtime +$DAYS_TO_KEEP -exec rm {} \;
fi

You will notice 5 configurable options at the beginning of this script. The main ones you will need to edit are the USER and PASSWORD. This should be changed to a MySQL user and password that has permissions to list and backup databases.

Once you have modified the options, make the script executable with the following command:

sudo chmod +x mysql-backup.sh

You can now test the backup script by running:

sudo ./mysql-backup.sh
Advertisement

Step 2: Create the Crontab Scheduled Task

Now we will schedule the backup script to be run daily. We will do this by adding a call to the mysql-backup.sh script to the root crontab.

Run the following command to open the root crontab file:

sudo crontab -e

Now add to the last line of the file the following:

@daily sh /scripts/mysql-backup.sh >> /var/log/mysql-backup.log 2>&1

Save the file and then wait for the script to run. You can check for errors in /var/log/mysql-backup.log. Once you are happy it is working, you can remove the >> /var/log/mysql-backup.log 2>&1 from the crontab file.

 

Thursday, March 3, 2022

Laravel Special Query

 

$users = User::orderBy(Company::select('name')
    ->whereColumn('companies.user_id', 'users.id')
)->get();
 
date('d-m-y', strtotime($req->received_date)) 
$users = User::select('users.*')
    ->join('companies', 'companies.user_id', '=', 'users.id')
    ->orderBy('companies.name')
    ->get();
 
 $keyResult= DB::table('item_maps')
->join('suppliers','item_maps.sup_id', '=', 'suppliers.id')
->join('reqs', 'item_maps.id', '=' ,'reqs.item_id' )
->select('*')
->addselect(DB::raw('DATE_FORMAT(reqs.created_at, "%d/%m") as req_date'))
->where('order_status', 'no')
->where(function($query) use ($input){
$query->where('item_name', 'LIKE', '%'.$input.'%')
->orWhere('supplier_name', 'LIKE', '%'.$input.'%')
->orWhere('req_id', 'LIKE', '%'.$input.'%');
})
->get();
 
 
  $reqs= Req::orderBy(Item_map::select('sup_id')
->whereColumn('item_maps.id','reqs.item_id')
)
->orderBy(Item_map::select('item_name')
->whereColumn('item_maps.id','reqs.item_id')

)
->orderBy('req_id')
->where('order_status','no')
->where('outlet_name','Staff')
->get();
 
$purchase_adjustments = Purchase::where(Item::select('name')
->whereColumn('purchases.item_id', 'items.id'),"LIKE",$search)
->orWhere(Item::select('item_code')
->whereColumn('purchases.item_id', 'items.id'),"LIKE",$search)
->orderBy(Item::select('name')
->whereColumn('purchases.item_id', 'items.id'))
->paginate(10); 

Sunday, December 26, 2021

How to change Apache port

 sudo nano /etc/apache2/ports.conf

Restart apache2

Virtual Host in Ubuntu

 go to /etc/apache2/sites-available/

nano sitename.conf

copy- 

<VirutalHost *:80>

    ServerName example.com

    DocumentRoot /var/www/html/xxxx


</VirtualHost>


sudo a2ensite sitename


reload apache2

Saturday, December 18, 2021

sendmail install and configure ubuntu

 Update Server Packages


SSH inside your server and update the packages to it’s latest version.


sudo apt update

sudo apt upgrade


Install Sendmail


Sendmail is available in the ubuntu repository, so you can directly install using apt install command.


sudo apt install sendmail


Configure Hostname


Edit the /etc/hosts file and add your hostname.


sudo nano /etc/hosts


On the line starting with 127.0.0.1, add the hostname to the end as it looks below. This should be on a single line.


127.0.0.1 localhost hostname


Configure SMTP


Create new directory inside /etc/mail for SMTP configurations.


sudo mkdir /etc/mail/authinfo


Setup correct permissions.


sudo chmod -R 700 /etc/mail/authinfo


Create a new file for your SMTP authentication inside the newly created directory.


cd /etc/mail/authinfo

sudo nano smtp-auth


Paste the following line and replace the email-address with your login email and password with your password.


AuthInfo: "U:root" "I:email-address" "P:password"


Hit CRTL + X followed by Y and ENTER to save and exit the file.


Create a hash database map for the above created authentication.


sudo makemap hash smtp-auth < smtp-auth


Configure SMTP


Navigate to the sendmail configuration directory and edit the sendmail.mc file.


cd /etc/mail

sudo nano sendmail.mc


Add the below configurations right after the MAILER _DEFINITIONS line.


Replace smtp-host with your SMTP hostname.


define(`SMART_HOST',`[smtp-host]')dnl

define(`RELAY_MAILER_ARGS', `TCP $h 587')dnl

define(`ESMTP_MAILER_ARGS', `TCP $h 587')dnl

define(`confAUTH_OPTIONS', `A p')dnl

TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl

define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl

FEATURE(`authinfo',`hash -o /etc/mail/authinfo/smtp-auth.db')dnl


The configuration should look like the same as the below screenshot.

SMTP configuration


Now save the file and exit the editor.

Rebuild Sendmail Configuration


Once the configuration is done you need to rebuild the Sendmail configuration using the make command.


cd /etc/mail

make


Once the configuration is rebuilt you need to restart Sendmail

Restart Sendmail


Restart Sendmail using the following command.


sudo /etc/init.d/sendmail restart


Now you can send emails using SMTP.

Additional Configurations

Configuration with PHP


To use Sendmail with PHP you need to ad sendmail path in your php.ini file.


sudo nano /etc/php/version/fpm-or-apache2/php.ini


To the bottom of the file add the following.


sendmail_path= /usr/sbin/sendmail -t -i


Restart Apache or PHP-FPM for the changes to take effect.


sudo service apache2 restart


or


sudo service php8.0-fpm restart


Sendmail SMTP Configuration without Auth


Incase if you have whitelisted your server IP for SMTP and you can send emails without authentication you can follow the below method.


You don’t need to create the smtp-auth file that we created above.


You can directly edit the sendmail.mc file and make the following changes.


cd /etc/mail

sudo nano sendmail.mc


Add the below configurations to the last.


Replace smtp-host with your SMTP hostname.


define(`SMART_HOST',`smtp-host')dnl

define(`RELAY_MAILER', `esmtp')dnl

define(`RELAY_MAILER_ARGS', `TCP $h 587')dnl


Save the file, rebuild configuration and restart Sendmail.


cd /etc/mail

make

sudo /etc/init.d/sendmail restart