How to run terminal command as root user from python script?

This is the place for queries that don't fit in any of the other categories.

How to run terminal command as root user from python script?

Postby shahriyar.r » Mon Sep 02, 2013 10:46 am

Dear experts,
I have a trouble with running innobackupex command from python script. It is a backup tool for MySQL.
i am using PyCharm and Python 3.3.2.
Here is full code:

Code: Select all
# Backup script for MySQL DB (using the latest version of Xtrabackup)
import os
import configparser
import subprocess
import shlex


class Backup:
    def __init__(self,conf = 'bck.conf'):
        con = configparser.ConfigParser()
        con.read('bck.conf')
        bolme = con.sections()
        DB = bolme[0]
        self.mysql = con[DB]['mysql']
        self.mycnf = con[DB]['mycnf']
        self.mysqladmin = con[DB]['mysqladmin']
        self.myuseroption = con[DB]['useroption']

        BCK = bolme[1]
        self.backupdir = con[BCK]['backupdir']
        self.full_dir = self.backupdir + '/full'
        self.inc_dir = self.backupdir + '/inc'
        self.backup_tool = con[BCK]['backup_tool']


        statusargs = '%s %s status' % (self.mysqladmin,self.myuseroption)
        statusargs = shlex.split(statusargs)
        myadmin = subprocess.Popen(statusargs,stdout = subprocess.PIPE)
        if not ('Uptime' in str(myadmin.stdout.read())):
            print('Server is NOT Up')
        else:
            print('Server is Up')


        if not os.path.exists(self.mycnf):
            print('Check MySQL configuration file path')

        if not os.path.exists(self.mysql):
            print('Check MySQL')

        if not os.path.exists(self.mysqladmin):
            print('Check MySQL Admin')

        if not os.path.exists(self.backup_tool):
            print('Check backup tool')

        if not (os.path.exists(self.backupdir)):
            print('Check core backup directory')

        if not (os.path.exists(self.full_dir)):
            print('Full directory is not exist. Creating full backup directory...')
            os.makedirs(self.backupdir + '/full')
            print('Created')

        if not (os.path.exists(self.inc_dir)):
            print('Increment directory is not exist. Creating increment backup directory...')
            os.makedirs(self.backupdir + '/inc')
            print('Created')

    def full_backup(self):

        args = '%s %s %s' % (self.backup_tool,self.myuseroption,self.full_dir)
        args = shlex.split(args)
        print(args)
        fb = subprocess.Popen(args,stdout=subprocess.PIPE)
        print(str(fb.stdout.read()))




b = Backup()
b.full_backup()


Here is bck.conf file that i used in code:

Code: Select all
[MySQL]
mysql = /usr/bin/mysql
mycnf = /usr/my.cnf
mysqladmin = /usr/bin/mysqladmin
useroption = --user=root --password=12345 --defaults-file=/usr/my.cnf

[Backup]
backupdir = /home/sh/backup
backup_tool = /usr/bin/innobackupex


When i run script from PyCharm encountered error:

Code: Select all
2013-09-02 15:32:50 7fd8aa43c720  InnoDB: Operating system error number 13 in a file operation.
InnoDB: The error means mysqld does not have the access rights to
InnoDB: the directory.
2013-09-02 15:32:50 7fd8aa43c720  InnoDB: File name ./ib_logfile0
2013-09-02 15:32:50 7fd8aa43c720  InnoDB: File operation call: 'open' returned OS error 113.
2013-09-02 15:32:50 7fd8aa43c720  InnoDB: Cannot continue operation.
innobackupex: Error: ibbackup child process has died at /usr/bin/innobackupex line 416.


It is similarly the same error , when i use innobackupex as regular OS user:

Code: Select all
[sh@localhost ~]$ /usr/bin/innobackupex --user=root --password=12345 --defaults-file=/usr/my.cnf /home/sh/backup/full
.
.
.2013-09-02 15:23:14 7fbbc81f6720  InnoDB: Operating system error number 13 in a file operation.
InnoDB: The error means mysqld does not have the access rights to
InnoDB: the directory.
2013-09-02 15:23:14 7fbbc81f6720  InnoDB: File name ./ib_logfile0
2013-09-02 15:23:14 7fbbc81f6720  InnoDB: File operation call: 'open' returned OS error 113.
2013-09-02 15:23:14 7fbbc81f6720  InnoDB: Cannot continue operation.
innobackupex: Error: ibbackup child process has died at /usr/bin/innobackupex line 416.


Surely it is OK When i run this command as root user:
Code: Select all
[root@localhost ~]#/usr/bin/innobackupex --user=root --password=12345 --defaults-file=/usr/my.cnf /home/sh/backup/full
.
.
.130902 15:39:04  innobackupex: Connection to database server closed
130902 15:39:04  innobackupex: completed OK!


So i want to run this command within script as root user. How i can do it?
shahriyar.r
 
Posts: 7
Joined: Thu Aug 29, 2013 9:50 am

Re: How to run terminal command as root user from python scr

Postby rrashkin » Mon Sep 02, 2013 2:28 pm

What if you run the script as root?
___________________
Bob Rashkin
User avatar
rrashkin
 
Posts: 35
Joined: Tue Feb 12, 2013 4:47 pm

Re: How to run terminal command as root user from python scr

Postby verb » Mon Sep 02, 2013 7:17 pm

use pexpect to login as root and do what you have to do
verb
 
Posts: 12
Joined: Fri Feb 22, 2013 8:15 pm

Re: How to run terminal command as root user from python scr

Postby shahriyar.r » Mon Sep 02, 2013 7:48 pm

rrashkin wrote:What if you run the script as root?


surely i can and i know that it works ;)
But think about my situation...
i have a Linux desktop where i develope python scripts using regular linux user, with IDE (PyCharm)..When i click run button from ide it doesnt work..but when i run script from terminal as root it works..and i must run it everytime from terminal to see errors or to check for something else within the script?

I want to learn how to run command within python script as root exactly ..is it possible?
shahriyar.r
 
Posts: 7
Joined: Thu Aug 29, 2013 9:50 am


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 4 guests