NAUbackup / VmBackup

XenServer simple backup script
228 stars 61 forks source link

VmBackup.py line 858 except socket.error as e #53

Closed ericzhg closed 6 years ago

ericzhg commented 7 years ago

It's showing the following error when I tried to execute the script:

[root@NAUbackup]# ./VmBackup.py password VMname preview File "./VmBackup.py", line 858 except socket.error as e: ^ SyntaxError: invalid syntax

Anyone has any clue how does that come?

NAUbackup commented 7 years ago

I cannot replicate that error. Did you download and install these two libraries, as explained in the installation guide? They should look like this and be in the executable path: -rw-r--r-- 1 root root 9553 Aug 30 14:20 XenAPI.py -rw-r--r-- 1 root root 8683 Sep 6 11:26 XenAPI.pyc

Also, did you configure your email server and any other parameters such as the DEFAULT_STATUS_LOG parameter? --Tobias

ericzhg commented 7 years ago

Hi Tobias, XenAPI.pyc? Sorry but I didn't see you mentioned that file in the readme file. Do we really have to have that and where can I get it? I did install the XenAPI.py in the server and they look like the following:

ll -a NAUbackup

total 128 drwxr-xr-x 3 root root 4096 Sep 20 10:59 . drwxr-xr-x 4 root root 4096 Sep 20 11:47 .. -rw-r--r-- 1 root root 2150 Sep 15 03:51 example.cfg drwxr-xr-x 2 root root 4096 Sep 19 16:14 logs -rw-r--r-- 1 root root 30808 Sep 15 03:51 README.md -rwxr-xr-x 1 root root 59052 Sep 20 10:59 VmBackup.py -rw-r--r-- 1 root root 9553 Sep 20 10:51 XenAPI.py

I didn't configure the email server in the test environment yet. I did configure other parameters such as the following: DEFAULT_POOL_DB_BACKUP = 0 DEFAULT_MAX_BACKUPS = 2 DEFAULT_VDI_EXPORT_FORMAT = 'raw' # xe vdi-export options: 'raw' or 'vhd' DEFAULT_BACKUP_DIR = '/var/run/sr-mount/e86dbadd-e627-3474-3007-5ccd5a110173/snapshots/BACKUPS'

DEFAULT_BACKUP_DIR = '\snapshots\BACKUPS' # alt for CIFS mounts

note - some NAS file servers may fail with ':', so change to your desired format

BACKUP_DIR_PATTERN = '%s/backup-%04d-%02d-%02d-(%02d:%02d:%02d)' DEFAULT_STATUS_LOG = '/var/run/sr-mount/e86dbadd-e627-3474-3007-5ccd5a110173/snapshots/NAUbackup/status.log'

Any advice?

ericzhg commented 7 years ago

The above code part seems quite strange so I re-post it as following: DEFAULT_POOL_DB_BACKUP = 0 DEFAULT_MAX_BACKUPS = 2 DEFAULT_VDI_EXPORT_FORMAT = 'raw' # xe vdi-export options: 'raw' or 'vhd' DEFAULT_BACKUP_DIR = '/var/run/sr-mount/e86dbadd-e627-3474-3007-5ccd5a110173/snapshots/BACKUPS' BACKUP_DIR_PATTERN = '%s/backup-%04d-%02d-%02d-(%02d:%02d:%02d)' DEFAULT_STATUS_LOG = '/var/run/sr-mount/e86dbadd-e627-3474-3007-5ccd5a110173/snapshots/NAUbackup/status.log'

NAUbackup commented 7 years ago

Hi, Eric: The backup_dir needs to point to some external (preferably) file. That .pyc library is indeed not needed, just the XenAPI.py library. That was just an older version I still had lying around, so please ignore that.

Your configuration file is not right, though, First off the backup_dir need to preferably be some area mounted from an external NFS server, or in any case, NOT an SR. I have an NFS mount called /snapshots so I use: backup_dir=/snapshots/BACKUPS

The same is true for logs, so you might want to use something like: DEFAULT_STATUS_LOG=/snapshots/Vmbackup.log

You do not want to ever put this somewhere underneath an existing SR! Also, make sure when you get the code that none of the lines wrap around to split lines that must be kept together.

Also, do not use passwords containing weird characters like slashes or anything that can be interpreted as a wildcard. And even without a password supplied at all, the command should still sort of run (and should at least not result in a python error) as long as you use the "preview" option because all it is doing is checking syntax.

ericzhg commented 7 years ago

Thx Tobias! All things cleared!

NAUbackup commented 7 years ago

Great to hear! What exactly was it if something similar comes up and someone else needs help? If the documentation can be improved, that's always good.

ericzhg commented 7 years ago

Same error even with NFS storage:

/snapshots/NAUbackup/VmBackup.py File "/snapshots/NAUbackup/VmBackup.py", line 858 except socket.error as e: ^ SyntaxError: invalid syntax

df -h Filesystem Size Used Avail Use% Mounted on nfsserver:/backup 1.8T 1.1G 1.7T 1% /snapshots

DEFAULT_POOL_DB_BACKUP = 0 DEFAULT_MAX_BACKUPS = 4 DEFAULT_VDI_EXPORT_FORMAT = 'raw' # xe vdi-export options: 'raw' or 'vhd' DEFAULT_BACKUP_DIR = '/snapshots/BACKUPS' BACKUP_DIR_PATTERN = '%s/backup-%04d-%02d-%02d-(%02d:%02d:%02d)' DEFAULT_STATUS_LOG = '/snapshots/NAUbackup/status.log'

Any idea?

NAUbackup commented 7 years ago

The directories /snapshots and /snapshots/NAUbackup and /snapshots/BACKUPS all exist with the proper permissions?

ericzhg commented 7 years ago

YesI think they do:

ll -a /snapshots total 16 drwxr-xr-x 4 root root 4096 Sep 22 16:01 . drwxr-xr-x 25 root root 4096 Sep 22 11:36 .. drwxr-xr-x 2 root root 4096 Sep 22 12:33 BACKUPS drwxr-xr-x 3 root root 4096 Sep 22 23:04 NAUbackup

ll -a /snapshots/NAUbackup total 124 drwxr-xr-x 3 root root 4096 Sep 22 23:04 . drwxr-xr-x 4 root root 4096 Sep 22 16:01 .. -rw-r--r-- 1 root root 2150 Sep 15 03:51 example.cfg drwxr-xr-x 2 root root 4096 Sep 19 16:14 logs -rw-r--r-- 1 root root 30808 Sep 15 03:51 README.md -rw-r--r-- 1 root root 58934 Sep 22 12:25 VmBackup.py -rw-r--r-- 1 root root 9553 Sep 20 10:51 XenAPI.py -rw-r--r-- 1 root root 3353 Sep 20 10:52 XenAPI.zip

/snapshots/NAUbackup/VmBackup.py passwd 84 preview -bash: /snapshots/NAUbackup/VmBackup.py: Permission denied

chmod 755 /snapshots/NAUbackup/VmBackup.py

/snapshots/NAUbackup/VmBackup.py passwd 84 preview File "/snapshots/NAUbackup/VmBackup.py", line 858 except socket.error as e: ^ SyntaxError: invalid syntax

NAUbackup commented 7 years ago

I assume you are substituting your actual XenServer's password for "password" and that "84" is the name of the VM you are testing? Do you still get an error with just "./VmBackup.py password preview" ? It should show something like "ERROR no VMs loaded" but no syntax errors. I'm just concerned that somehow in copying the code that some extra line break or extra character got inserted. The original copy of VmBackup.py should have 58944 bytes. AQLso, did you make any changes to the email configuration, including the server name or suck? I'm wondering if there's an attempt being made to contact your email server and that is failing:

855 s.sendmail(MAIL_FROM_ADDR, to.split(','), msg.as_string()) 856 s.quit() 857 break 858 except socket.error as e: 859 print("Exception: socket.error - {}".format(e)) 860 time.sleep(5)

ericzhg commented 7 years ago

Hi Tobias, same error there even ran with just preview:

/snapshots/NAUbackup/VmBackup.py preview File "/snapshots/NAUbackup/VmBackup.py", line 858 except socket.error as e: ^ SyntaxError: invalid syntax

Kinda depressed. I didn't make any changes to the email configuration since it's optional.

I don't think some extra line break or extra character got inserted, otherwise there must be something indicative for that. Or may be I was wrong so I just put the files attached for your investigation. XenAPI.py is also attached for integrity check... 2file.zip

NAUbackup commented 7 years ago

Eric, Really sorry to hear this! Can you try copying it once again and carefully pasting in the VmBackup.py file and make sure you get no line wrapping? I also tried it without configuring the SMTP server and had no issues, either. Any chance you have a conflicting environment variable? Also, is there another server you could try this on? What version of XenServer is this, anyway? --Tobias

brainstorm443 commented 7 years ago

Hi Tobias. I have exactly the same error. My Xenserver version is 6.5. Is there any solution for this ? Like You I didn't configure smtp settings.

NAUbackup commented 7 years ago

Unfortunately, I have no 6.5 setups any more so cannot easily check that. Did you try this on a different server? Try adding after line 859 an additional print statement so you get this:

    except socket.error as e:
        print("Exception: socket.error - {}".format(e))
        print "Exception: socket.error - %s" %e
        time.sleep(5)

You could also try commenting out the entire section from lines 816 - 863, the block starting with:

def send_email(to, subject, body_fname):

NAUbackup commented 7 years ago

I am wondering if the except socket.error as e: construction isn't recognized in older versions. Might have to replace the send_email(to, subject, body_fname): block with the earlier version and try that. Here is that code block:

V3.1-email-code-block.txt

dbaselica commented 7 years ago

I have the same problem except socket.error as e:

change with --> except socket.error, e:

NAUbackup commented 7 years ago

Thanks much for that, @dbaselica ... could you please try that modification, @ericzhg and see if it solves your situation, as well? Again, I suspect it's a syntax compatibility issue between python versions.

dbaselica commented 7 years ago

(sorry my english) I suspect the same, but I have no knowledge about pyton to inquire more. I have XenServer6.2 with Python 2.4.3

This works for me: except socket.error, e: print("Exception: socket.error - {}".format(e)) time.sleep(5) except smtplib.SMTPException, e: print("Exception: SMTPException - {}".format(e.message)) time.sleep(5)

NAUbackup commented 7 years ago

That checks fine with that syntax on XenServer 7.1, so that's good. I would like to hear back from @ericzhg, as well, but this looks like a good solution. Many thanks again!

ericzhg commented 7 years ago

Hi Tobias, sorry for the late reply. Here is the result under 6.5:

  1. same error when add print "Exception: socket.error - %s" %e after line 859: except socket.error as e: ^ SyntaxError: invalid syntax

  2. still problematic when replace the send_email(to...) by V3.1-email-code-block.txt provided: File "/snapshots/NAUbackup/VmBackup.py", line 848 break ^ SyntaxError: invalid syntax

  3. seems only working if lines 816 - 863 totally removed: /snapshots/NAUbackup/VmBackup.py pass vmname preview 2017-09-29-(12:37:22) - VmBackup config loaded from: 84 2017-09-29-(12:37:22) - VmBackup.py running with these settings: 2017-09-29-(12:37:22) - backup_dir = /snapshots/BACKUPS 2017-09-29-(12:37:22) - status_log = /snapshots/NAUbackup/status.log 2017-09-29-(12:37:22) - compress = False 2017-09-29-(12:37:22) - max_backups = 4 2017-09-29-(12:37:22) - vdi_export_format = raw 2017-09-29-(12:37:22) - pool_db_backup = 0 2017-09-29-(12:37:22) - exclude (cnt)= 0 2017-09-29-(12:37:22) - exclude: 2017-09-29-(12:37:22) - vdi-export (cnt)= 0 2017-09-29-(12:37:22) - vdi-export: 2017-09-29-(12:37:22) - vm-export (cnt)= 1 2017-09-29-(12:37:22) - vm-export: 84 2017-09-29-(12:37:22) - SUCCESS preview of parameters

So do we have to sacrifice the mail function for the version 6.5?

NAUbackup commented 7 years ago

In other words, the set of commands will be (not including here the proper indentation):

except socket.error, e: print("Exception: socket.error - {}".format(e)) time.sleep(5) except smtplib.SMTPException, e: print("Exception: SMTPException - {}".format(e.message)) time.sleep(5)

In other words, replace the two current lines that contain: except socket.error as e:

with: except socket.error, e:

You are saying that this doesn't work on your server, even though apparently it does with @dbaselica on XS 6,2? I can't imagine why it would there but not in your case. The failure on "break" may be just an issue of something being misaligned with a missing or extra space, is what I'm wondering.

Do you really not have another host you could try this on? I can see if I can get onto a co-worker's 6.5 SP1 XenServer to try it, or I may be able to find another test box.

The temporary workaround could be to substitute back the email function from before without the multiple resend attempts, which is what you've shown works with the substitute code from the V3.1-email-code-block.txt file above,

ericzhg commented 7 years ago

except socket.error, e: this one works fine on all 6.5 servers I tested . Sorry I didn't read that before. Things are looking up now. Thank you!

NAUbackup commented 7 years ago

Yay, so I can update the distribution and close this now?

brainstorm443 commented 7 years ago

That solution works for me

ericzhg commented 7 years ago

Yep. Pls update and close. Thx! Great job!

dbaselica commented 7 years ago

New problem.....

2017-09-29-(13:23:00) - cifs 36T 258G 36T 1% /var/run/sr-mount/bcfb7e87-cce2-af29-f45d-11cac15e7b05 'str' object has no attribute 'format' 2017-09-29-(13:23:00) - ERROR EXCEPTION - exceptions.AttributeError 2017-09-29-(13:23:00) - ERROR NOTE: see VmBackup output for details Traceback (most recent call last): File "./VmBackup.py", line 1464, in ? main(session) File "./VmBackup.py", line 478, in main send_email(MAIL_TO_ADDR,'WARNING VmBackup.py', status_log) File "./VmBackup.py", line 859, in send_email print("Exception: socket.error - {}".format(e)) AttributeError: 'str' object has no attribute 'format'

Partial Solution.... except socket.error, e:

print("Exception: socket.error - {}".format(e))

        time.sleep(5)
    except smtplib.SMTPException, e:
        # print("Exception: SMTPException - {}".format(e.message))
        time.sleep(5)

Perhaps the main problem is the pyton version and the smtplib/socket lib This libs are external libs? or exist in Xen6.2?

Thanks

NAUbackup commented 7 years ago

The python library should be self-contained. The API python version I have installed is 1.2 and python itself under XS 7.1 is version 2.7.5.

I should probably get rid of the fancy format and make rather just something simple like:

print("Exception: socket.error - %s" %e)

and

print("Exception: SMTPException - %s" %e.message)

Could you try those, please? Thank you, --Tobias

NAUbackup commented 7 years ago

I pushed a new version with simplified error message reporting. Please try it out and report back if you still encounter any errors.

dbaselica commented 7 years ago

This works fine

    except socket.error, e:
        # print("Exception: socket.error - {}".format(e))
        print("Exception: socket.error - %s" %e)
        time.sleep(5)
    except smtplib.SMTPException, e:
        # print("Exception: SMTPException - {}".format(e.message))
        print("Exception: SMTPException - %s" %e.message)
        time.sleep(5)   
NAUbackup commented 7 years ago

OK, good I have meanwhile committed that change. I will leave this open a while longer, just in case anything else crops up, and thank you for your assistance and patience!

NAUbackup commented 6 years ago

Declaring that the V3.21 release has resolved this issue and am therefore closing it.