0

Same question has been asked here and here but these are not helping me out.

In my python utility, I'm trying to take MySQL database backup using mysqldump. It runs successfully but I'm getting 0kb file in my backup folder.

This is my code;

DB_HOST = '192.168.1.1'
DB_USER = 'root'
DB_USER_PASSWORD = '_root_user_password_'
DB_NAME = 'db_name'
BACKUP_PATH = '/backup/dbbackup/'

DATETIME = time.strftime('%m%d%Y-%H%M%S')
TODAYBACKUPPATH = BACKUP_PATH + DATETIME

dumpcmd = "mysqldump -h " + DB_HOST + " -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + DB_NAME + " > " + TODAYBACKUPPATH + "/" + DB_NAME + ".sql"

os.system(dumpcmd)

Can anyone guide me out that what I'm missing here??

Naila Akbar
  • 3,033
  • 4
  • 34
  • 76

1 Answers1

1

You could analyse the code in a python console (just execute python at the shell command line), and find some problems:

First, an import is missing at the top of the code:

import time, os

Second, the db variable is not set, I guess this should be DB_NAME. A good practice in python is to concatenate strings by using %-formatting:

dumpcmd = "mysqldump -h %s -u %s -p%s %s > %s/%s.sql" % (DB_HOST,DB_USER,DB_USER_PASSWORD,DB_NAME,TODAYBACKUPPATH,DB_NAME,)

Finally, as a hint, add print dumpcmd and test the command manually.

Keep in mind that the path should be created first, or replace the / by - for the filename.

admirableadmin
  • 2,669
  • 1
  • 24
  • 41