Port php mysql scripts to php 7.0 from 5.x version

Recently I got a script or series of scripts that were written for PHP 5.6x and hence used mysql_connect which as you know by now does not work with PHP 7.0. Since there were number of scripts, I thought it would be waste of time to change them manually and wrote a script to fix them. If you have similar situation then probably this few lines could help you.

Since my scripts did not use all the functions so I did not put the sed commands for all of them but you get the idea 🙂

#!/bin/bash - 
#===============================================================================
#
#          FILE: fix_mysql.sh
# 
#         USAGE: ./fix_mysql.sh 
# 
#   DESCRIPTION: 
# 
#       OPTIONS: ---
#  REQUIREMENTS: ---
#          BUGS: ---
#         NOTES: ---
#        AUTHOR: Amit Agarwal (aka), 
#  ORGANIZATION: 
#       CREATED: 03/23/2018 16:35
# Last modified: Fri Mar 23, 2018  04:36PM
#      REVISION:  ---
#===============================================================================

set -o nounset                              # Treat unset variables as an error


### Run as 
#### find . -type f -name \*php -exec ~/fixmysql.sh {} \;

####

# mysql_affected_rows -> mysqli_affected_rows($link)
# mysql_close -> mysqli_close($link)
# mysql_data_seek -> mysqli_data_seek( $result, $offset)
# mysql_errno -> mysqli_errno( $link)
# mysql_error -> mysqli_error( $link)
# mysql_fetch_array -> mysqli_fetch_array( $result, $type)
# mysql_fetch_assoc -> mysqli_fetch_assoc( $result)
# mysql_fetch_lengths -> mysqli_fetch_lengths( $result )
# mysql_fetch_object -> mysqli_fetch_object( $result, $class, $params)
# mysql_fetch_row -> mysqli_fetch_row( $result)
# mysql_field_seek -> mysqli_field_seek( $result, $number)
# mysql_free_result -> mysqli_free_result(result)
# mysql_get_client_info -> mysqli_get_client_info( $link)
# mysql_get_host_info -> mysqli_get_host_info( $link)
# mysql_get_proto_info -> mysqli_get_proto_info( $link)
# mysql_get_server_info -> mysqli_get_server_info( $link)
# mysql_info -> mysqli_info( $link)
# mysql_insert_id -> mysqli_insert_id( $link)
# mysql_num_rows ->  mysqli_num_rows( $result)
# mysql_ping -> mysqli_ping( $link)
# mysql_query -> mysqli_query( $link, $query)
# mysql_real_escape_string -> mysqli_real_escape_string( $link)
# mysql_select_db - > mysqli_select_db( $link, $database)
# mysql_set_charset -> mysqli_set_charset( $link, $charset)
# mysql_stat -> mysqli_stat( $link)
# mysql_thread_id -> mysqli_thread_id( $link)
######


file=$1
sed -i 's/mysql_connect/mysqli_connect/g' $file
sed -i 's/mysql_query(/mysqli_query($con,/g' $file
sed -i 's/mysql_error/mysqli_error/g' $file
sed -i 's/mysql_select_db(/mysqli_select_db($con,/g' $file
sed -i 's/mysql_affected_rows/mysqli_affected_rows($con,/' $file
sed -i 's/mysql_escape_string(/mysqli_escape_string($con/' $file 
sed -i 's/mysql_real_escape_string(/mysqli_real_escape_string($con/' $file 
sed -i 's/mysql_fetch_array/mysqli_fetch_array/g' $file
sed -i 's/mysql_fetch_assoc/mysqli_fetch_assoc/g' $file
sed -i 's/mysql_fetch_row/mysqli_fetch_row/g' $file
sed -i 's/mysql_select_db/mysqli_select_db/g' $file

Linked clone with qemu-img

As you would have seen in Virtualbox or vmware, there is option to create a linked clone. I wanted to use the same feature as “Snapshot” feature anyway does not look/work so great with virt-manager. So, I created a script to create a linked clone VM and here it is :

 

#!/bin/bash - 
#===============================================================================
#
#          FILE: qcow2-linked-clone.sh
# 
#         USAGE: ./qcow2-linked-clone.sh 
# 
#   DESCRIPTION: 
# 
#       OPTIONS: ---
#  REQUIREMENTS: ---
#          BUGS: ---
#         NOTES: ---
#        AUTHOR: Amit Agarwal (aka)
#  ORGANIZATION: Mobileum
#       CREATED: 01/05/2018 09:54
# Last modified: Wed Feb 28, 2018  04:39PM
#      REVISION:  ---
#===============================================================================

set -o nounset                              # Treat unset variables as an error
UP="amitag:amitag" #Here you need to put your username and group.

if [[ $# == 0 ]]
then
    read -p "Enter the source path :: " spath
    read -p "Enter the source disk :: " sdisk
    read -p "Enter the destin path :: " dpath
    read -p "Enter the destin disk :: " ddisk
    read -p "Enter new VMName :: " vmname
else
    spath=$(dirname $1)
    dpath=$spath
    sdisk=$(basename $1)
    ddisk=$2.qcow2
    vmname=$2
fi


sudo chown $UP "$spath/$sdisk"
qemu-img create -f qcow2 -b "$spath/$sdisk" "$dpath/$ddisk"

virt-install --disk $dpath/$ddisk --ram 512 \
    --virt-type kvm --vcpus 1 --name "$vmname" --import

The script will create a linked qcow2 and then create a VM with that image. Running it is simple, either provide command line options or just run and it will ask you for details.

 

Hope it is useful.

Monitor progress of coreutils commad like mv, cp etc

Monitor progress of coreutils commad like mv, cp etc

You can use command called “progress”. Other option is “pv” but that is little complicated so here is simpler option.

Progress – A Tiny Tool to Monitor Progress for (cp, mv, dd, tar, etc.) Commands in Linux

To install, you can do the following:

dnf install progress