Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to get snapshots issue after leaving Sleep mode on Windows #180

Closed
RobProductions opened this issue Mar 27, 2024 · 7 comments
Closed
Labels
enhancement New feature or request

Comments

@RobProductions
Copy link

Describe the bug
I have a scheduled plan that is supposed to run at 5pm each day with the target repo existing on an external NAS machine plugged into my computer. Everything seems to work fine for the most part when the computer is active but I've noticed some backups failed to go off if my computer is asleep during the scheduled time. Of course I wouldn't expect it to run in this state but I saw that it actually attempts a backup after I log in and fails, giving the error failed to get snapshots for plan with exit status 1. Because of this I haven't been getting my files saved every time this happens and I have to backup manually using the "Backup Now" button while my computer is awake to get new snapshots.

To Reproduce
Steps to reproduce the behavior:

  1. Schedule a plan for a specific time on Windows 11 which backs up to an external drive or NAS (might have to be a NAS machine)
  2. Activate sleep mode for the computer before that time
  3. Leave sleep mode after the time has passed
  4. Failed to get snapshots error shows up and nothing was backed up

Expected behavior
Hoping that the backup can succeed once I leave sleep mode and log in.

While I don't know what "failed to get snapshots" exactly means in restic/backrest terms (perhaps it can't connect to the repo location?), my ideas as to why this is happening are:

  1. NAS isn't yet mounted properly by the time backrest runs its check after exiting sleep mode OR
  2. Some weird security rule is kicking in that won't let programs access the NAS until after I log in on Windows, and backrest is able to run before that point

Some potential approaches for fixing this:

  1. Add a (user configured?) arbitrary time delay for X amount of minutes before checking past backups that were missed from Sleep mode in order to allow external devices to mount in time
  2. If it's related to logging in, maybe backrest could somehow figure out if the user is logged in or not and hook into that as the best time to run the catch-up operations
  3. Have backrest check if the repo is accessible before attempting backup and redo this check every minute until it is able to backup properly, this could be also be user configured for better control. This would ensure that it doesn't just fail and give up, and would also help if someone unplugged a hard drive for example as the next time they plug it in, the backup could still run

Screenshots
image

Would appreciate any info regarding what exactly this error means as it would help track down the best route to fix it.

Desktop (please complete the following information):

  • OS: Windows 11
  • Browser: Firefox
  • Backrest Version: 0.14.0

Thanks for any help regarding this issue! I really love the way this app works and would be happy to see this streamlined in some way so I can better trust that my files are being backed up without having to manually check it so often.

@RobProductions RobProductions added the bug Something isn't working label Mar 27, 2024
@garethgeorge
Copy link
Owner

Hmm, one way you may be able to work around this (it's a bit silly) is with a command hook that runs a sleep statement to delay running the backup operation until your NAS has time to mount. You could even use a while loop of some sort to continually poll and finally exit the script when the volume is available.

The biggest caveat I'll add is that command hooks are entirely untested on windows and they default to /usr/sh as the entrypoint which is only valid on linux. You'll need to add a comment e.g. #! cmd.exe to indicate that backrest should run commands on the windows cmd line (you might need to play with this a bit). I'll try to test this out if I get a bit of time!

@garethgeorge
Copy link
Owner

Command hook implementation is here https://github.com/garethgeorge/backrest/blob/main/internal/hook/commandhook.go#L13-L39 so you can get some idea of how to craft a script that should work w/windows. If just setting #! cmd.exe does work, I'll try to make a fix to set that as the default for the windows build.

@garethgeorge garethgeorge added enhancement New feature or request and removed bug Something isn't working labels Mar 29, 2024
@RobProductions
Copy link
Author

Thanks, that's a great idea! The first few steps of this worked after some trial and error. I used #! cmd.exe at the start of my hook and I could see in the console output that the command prompt opening lines (c) Microsoft Corporation popped up so that confirmed that cmd.exe opened correctly.

Then I tried to use the timeout command along with > nul to have it wait 30 seconds but it didn't seem to work. Instead I just see the word "More?". Not sure what happened there:

image

So instead I got rid of the > nul but then I hit this issue which apparently is a problem with the timeout command.

Finally, I tried an alternate way of pausing the command line and although it's a bit hacky, ping 127.0.0.1 -n 30 > nul seemed to do the trick:

image

Execution paused for 30 seconds and after that I see the backup start. Haven't yet tested if it solves my issue after waking up my computer, so I'll leave it like this for a few days and get back to you if I see the snapshots going through. Appreciate the help!

@garethgeorge
Copy link
Owner

garethgeorge commented Mar 31, 2024

Hmm, on Linux/MacOS /bin/sh and /bin/bash can accept a script written to their stdin. I'm not seeing an obvious way to do this on Windows.

I think the best workaround would be to create a new .bat file for your script and to use backrest to launch that bat file. I suspect you'll be able to get that to work so long as the first line of backrest script is the call delegating to your bat script e.g.

#!  cmd.exe
call C:\your-bat-file.bat

may work. I may aim to add a "script" hook type that accepts a filepath instead of the script as text so that I can support this in a crossplatform way.

@RobProductions
Copy link
Author

Hmm, on Linux/MacOS /bin/sh and /bin/bash can accept a script written to their stdin.

Actually, I think it works just fine here because ultimately the ping solution worked as I mentioned above. There may just be issues with the timeout command in particular in this environment for whatever reason.

I may aim to add a "script" hook type that accepts a filepath instead of the script as text so that I can support this in a crossplatform way.

This would still be a useful option though! In addition to the automatic cmd detection on Windows you mentioned earlier^

As for my testing with this ping hack, it works so far! When I check the logs after waking up my computer I see the backup went through after 30 seconds and succeeded:

image

So this looks promising but one thing I have yet to check is if it'll work after waking up my computer and not logging in immediately, just to be sure the issue is the NAS mounting and not something else. Will report back when I've tried that, thanks!

@garethgeorge
Copy link
Owner

Brilliant, glad this patch worked. I'll leave this open to track defaulting to cmd.exe for script execution on Windows.

@RobProductions
Copy link
Author

Sounds good, and to give one last update here I did a test leaving my PC logged out but still on and the backup worked correctly though after a longer delay than expected. So it does seem to be that the NAS mounting was the culprit, therefore this solution should hold up in all situations where it mounts in under 30 seconds :)

I do still think it would be nice to have a "wait X amount of seconds before catching up on backups" option for the user in the UI (so that I don't have to hook up these hacky scripts in the first place) but I understand that this is an uncommon case. If it was up to me though I'd want the most control possible over how/when it runs considering all the different situations & platforms Backrest could be run from. Even if this option had to be enabled via a settings file or hidden under an "advanced options" dropdown to keep the main experience simple I'd still think it's worth it, but that's just me haha

Regardless though, thanks a ton for the help and I'm really glad I got this figured out :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants