Forums

SQLite Scheduled Backup

Hi all,

I've recently set up my first flask web app and created a function to back up my SQLite database. I tested this function locally and it worked as anticipated, I also checked it within pythonanywhere and it worked as anticipated.

Unfortunately, when I've run it as a scheduled task, the program seems to creates a back up but it's completely empty and not in the folder I expect it to be in. In the tests the back up file ends up in /home/Liamjd93/portal which is what I was expecting. When the scheduled task runs it, it is in /home/Liamjd93.

Does anyone have any ideas why the backup file is empty and in the wrong folder when it's run as a scheduled task but not when I manually run the program?

TIA, Liam.

Sounds like an issue with relative paths. What if you change the command in the task to cd to the app directory, then run your script from there?

Hi sboydeu

Do I do that in the box where you set up the task? Currently it just says '/home/Liamjd93/portal/db_backup.py' so would I simply change that do 'cd portal /home/Liamjd93/portal/db_backup.py' ?

You can try that. Maybe use absolute path and divide the commands with a semicolon: cd /home/Liamjd93/portal; /home/Liamjd93/portal/db_backup.py.

So I've just tried this and got the following:

"bash: /home/Liamjd93/portal/db_backup.py: Permission denied 2024-03-27 16:22:05 -- Completed task, took 2.52 seconds, return code was 126."

The task I tried was this: cd /home/Liamjd93/portal; /home/Liamjd93/portal/db_backup.py

Make sure that you have set execute permissions on the file you are trying to execute: https://help.pythonanywhere.com/pages/ScheduledTasks/#running-non-python-scripts

Thanks everyone for your help. I've managed to get this to work now using the following:

(hashbang)/usr/bin/python; cd /home/Liamjd93/portal; chmod +x /home/Liamjd93/portal/db_backup.py; /home/Liamjd93/portal/db_backup.py

Great, glad to hear you were able to get it working!

Spoke too soon, I just checked and it's saying that the task is complete but it isn't actually running the file it seems! The script still works if I run it manually!

Ah, I see the problem. I'd misunderstood what you meant by (hashbang) at the start of the command. You're using a # character, which in all cases when it's not the first line of a file means "the rest of this line is a comment", so your task isn't doing anything.

Here's what you should schedule:

cd /home/Liamjd93/portal; python ./db_backup.py

Ooh right I see. So if I was running something non-Python then I'd have to do the 'hashbang/usr/bin/whatever' at the start of the file I'm wanting to run, not in the task to schedule. Either way I don't need to do it because it's a Python file anyway?

Thanks for the help! It's been driving me nuts ha.

That's correct.