1

Python 2.7

I am getting the following error message. Not clear as to why this is. I have searched and no luck. Fairly basic script. Has worked across all our windows environments

#!/usr/bin/env python

import os
import time
import subprocess
import platform

#Log Files

source = ['"/home/datatec/ds/datos"' ]

#Backup
target_dir = "/home/datatec/Backup"

#Zip file + date and time
target = "target_dir + os.sep + time.strftime('%Y%m%d') + platform.node()  + '.zip'"

zip_command = zip_command = "zip -r {0} {1}".format(target, ' '.join(source))
err = open('error.txt' , 'w')

#Run the backup + verify
if os.system(zip_command) == 0:

    print('Successful backup to', target)
else:
    print('Backup FAILED')

#create server directory, all folders are moved at end of the week
mkdir = 'ssh logcp@ushsdata01p "cd ../proddata;sleep 3;hn=$ushsdtec01p;mkdir ushsdtec01p;"'
os.system(mkdir)

dz = "rm /home/datatec/Backup/*.zip"
psfiles = "scp  *.zip logcp@ushsdata01p:/proddata/ushsdtec01p/"
print 'TRANSFERRING ZIP FILES!!'
if os.system(psfiles) == 0:
    os.system(dz)
    print ('Files transferred')
else:
       print('TRANSFER FAILED')
       err.write("job failed")
       err.close()

this is the output:

sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `zip -r target_dir + os.sep + time.strftime('%Y%m%d') + platform.node()  + '.zip' "/home/datatec/ds/datos"'
Backup FAILED

2 Answers 2

3

os.system executes an argument in a subshell, where braces have special meanings. You should use subprocess instead.

But the immediate error is caused by the quotes in

target = "target_dir + os.sep + time.strftime('%Y%m%d') + platform.node() + '.zip'"

This line should almost certainly be

target = target_dir + os.sep + time.strftime('%Y%m%d') + platform.node() + '.zip'
Sign up to request clarification or add additional context in comments.

1 Comment

Cheers! Thanks for helping a newb out. Hopefully I can return the favor to someone soon!
2

For starters, the "target = ..." line should probably not be quoted.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.