0

I have a python3.2 script that's supposed to delete a folder after everything is done:

def perforce_backup(
source,
destination,
tmp_location,
zip_tmp_loc,
):

logger.info('--------------------Perforce Backup--------------------'
            )

logger.info('--- Check integrity of perforce depot (p4 verify)')
p4verify(source, 'user', 'password')

logger.info('--- Create a checkpoint (p4 admin checkpoint)')
p4checkpoint(source, 'user', 'password')

logger.info('--- Do the backup locally')
rsync(source, tmp_location)

logger.info('--- Zip perforce db and depot locally')
zipdir(tmp_location, zip_tmp_loc)

logger.info('--- Remove file from last folder on backup FTP')
shutil.rmtree(destination.path)
makedir(destination.path)

logger.info('--- Move zip to backup FTP')
cp(zip_tmp_loc.path + '/*', destination.path)

logger.info('--- Remove tmp_file locally - raw copy and archive')
shutil.rmtree(tmp_location.path)
logger.info('--- Remove tmp_file locally - raw copy and archive2')
shutil.rmtree(zip_tmp_loc.path)
logger.info('--- Remove tmp_file locally - raw copy and archive3')

When I run the script manually, using the "vbackup" user, it works. I defined a task in my "user" crontab with this syntax (i am executing crontab -e as "vbackup" by using "su vbackup":

00  22 * * * python3.2 /opt/valibackup/main.py

When I use the above, the script runs every day at 22:00. The problem is that it seems to run without the needed privileges and the shutil.rmtree() doesn't work, when it does when I run the script manually.

I tried the following syntax that I found here to be sure that it was ran with "vbackup" rights, but it doesn't even start.

*/30 * * * * vbackup python3.2 /opt/valibackup/main.py

If I edit by using "sudo crontab -e" instead, the rmtree works, but not the rsync sends a Permission denied error.

Any idea?

nivolas
  • 317
  • 1
  • 5
  • 16

1 Answers1

0

It sounds like you need to be a privileged user to remove the folders and need to run rsync as you local user.

Modify your script to work as below and try.

Just switch to sudo user before the delete operation and then switch back to your normal user.

I think this answer in stackoverflow will help you

switching user in python

Community
  • 1
  • 1
Share_Improve
  • 417
  • 5
  • 15