Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Saturday, August 27, 2016

Running Python script on Unix



So far we have learned about basics of Python including how to print anything in Python and its major data types. We have also seen different types of loop and function declarations. In this article, we will write small function and save it to a file (.py) and then we will execute it from terminal

Writing first Python script  

We will write a function, which will accept user input and validate, if user has entered incorrect input. It will keep on prompting for user input until it gets correct input.


def getFloatFromUser(prompt):
    while True:
        number = raw_input(prompt)
        try:
            number = float(number)
        except:
            print 'That is not a float, please try again.'
            continue
        # everything OK
        return number

myFloat = getFloatFromUser('Please enter a float: ')
print myFloat


We have saved above code to my_first_python_script.py file.


Running Python script  

We have saved our python file to our tutorial folder. Now, we will open the terminal and run this file, but before that, we need to change file to executable.
 $chmod +x /Desktop/MyTutorial/Python/my_first_python_script.py
Once, you have changed file to executable , you need to type following command to execute your python script :
 $python /Desktop/MyTutorial/Python/my_first_python_script.py

The script will start executing and expects valid input, please refer below snippet : 


$ python /Desktop/MyTutorial/Python/my_first_python_script.py
Please enter a float: tt
That is not a float, please try again.
Please enter a float: las
That is not a float, please try again.
Please enter a float: sdf
That is not a float, please try again.
Please enter a float: 90
90.0

Until user enters valid input, it will keep on prompting for valid input. Once, user enters valid input, it will display it to screen and come out from the loop. 

That's it !!

Thursday, August 18, 2016

Schedule MySQL Database backup on CPanel or Linux




If you running a business online and you are using MySQL database to keep your customer and product related information. Then, you always try to keep database backed up, just in case, if your database got crashed, you will have a copy of data. 

Taking manual backup will never be a good idea as it takes time and resource whereas you will find many paid services which will help you to keep your database backed up on regular interval. But, if you are looking for something, which can do same job without paying single penny to any third party. Then, this might be useful article for you

First of all, we create a backup folder, where we will keep our database backup dump file. And then, we will write the shell script, which will create database backup file and place it to our backup folder. 


#!/bin/sh
now="$(date +'%d_%m_%Y_%H_%M_%S')"
filename="db_backup_$now".gz
backupfolder=“"
fullpathbackupfile="$backupfolder/$filename"
logfile="$backupfolder/"backup_log_"$(date +'%Y_%m')".txt
echo "mysqldump started at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"
mysqldump —user= —password= --default-character-set=utf8  | gzip > "$fullpathbackupfile"
echo "mysqldump finished at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"
chown  "$fullpathbackupfile"
chown  "$logfile"
echo "file permission changed" >> "$logfile"
find "$backupfolder" -name db_backup_* -mtime +2 -exec rm {} \;
echo "old files deleted" >> "$logfile"
echo "operation finished at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"
echo "*****************" >> "$logfile"
exit 0

Above script will create backup file and place it to your backup folder and also it will remove dump file, which is older than 2 days. You can keep this shell script file to any location (but I would recommend to keep this file in same backup folder)

Now, depending on your requirement, you should schedule this script to run on specific time/interval by adding it to CRON job. And to add new CRON job, you need to provide following details, path of shell script file (i.e. command) and email id where you want to receive notification for any failure/success.




Once, you have successfully created CRON job, then you will see a CRON job entry created, which will be executed only specified time/interval 




Upon your specified time/interval, your backup folder will start getting populated with your database backup and log files.

That's it. !!

Let me know, if you got into any issue while configuring it.