Sunday, January 26, 2014

django: permission denied: '/home/www-data'

Recently, I got a  error ("permission denied: '/home/www-data' ") in django. Couldn't figure out the problem for a while...... but finally i got the solution.

1. What i was trying to do......
     I have built a django application(that runs on apache server in production environment) that records all the user data, customer data and all the transactions. I was trying to add the feature that back-up the database to the location , "/home/binod/database-backup/xxxx-xx-xx", where binod is the username of the system and database-backup and xxxx-xx-xx (which is the current date) are the sub-folders inside "/home/binod/", are created dynamically by the system.


2. What happened............
    Everything was fine in development environment. I was happy :) .
The code was:

@login_required
@user_passes_test(lambda u:u.is_superuser)
def backUpDatabase(request):
    args = {}
    
    now     = datetime.datetime.now()
    now_time = str(now.hour)+"-"+str(now.minute)+"-"+str(now.second)

    today = now.date()
    today = dateConverter.english_to_nepali(datetime.date.today())
    today = str(today.year)+"-"+str(today.month)+"-"+str(today.day)
    
    import getpass
    user = str(getpass.getuser())             #this gets the current username i.e binod for my system [what i expected :( ]
    
    backup_base_dir ="/home/"+user+"/database-backup/"        
    backup_sub_dir  =backup_base_dir+today+"/"
    backup_filename =backup_sub_dir+today+"-"+now_time+".sql" 

    import os
    
    if not os.path.exists(backup_base_dir):
        os.makedirs(backup_base_dir)

    if not os.path.exists(backup_sub_dir):
        os.makedirs(backup_sub_dir)
    
    from projectUCN import settings
    password =settings.DATABASES['default']['PASSWORD']
    return_value = os.system("mysqldump -u root -p"+password+" projectUCN > "+backup_filename)

    if return_value == 0:
        from django.contrib import messages
        messages.success(request, "database has been successfully saved to  \""+backup_filename+"\"")

    return HttpResponseRedirect('/admin/databaseOperation/')

I got exactly same output in development environment what i had expected but while running in production environment, I got the error permission denied: '/home/www-data'















3.What I did.........

The problem was with the line

    import getpass
    user = str(getpass.getuser())             #this gets the current username i.e binod for my system [what i expected :( ]
                                                                   #But this gets the username 'www-data '    :D
    
    backup_base_dir ="/home/"+user+"/database-backup/"        
    backup_sub_dir  =backup_base_dir+today+"/"
    backup_filename =backup_sub_dir+today+"-"+now_time+".sql" 

so, 
 backup_base_dir  becomes  "/home/www-data/database-backup/"    and so on.......

 So, the solution is replace the user by the current username . i.e
 backup_base_dir  becomes  "/home/username/database-backup/"