Automatic Update Script For Self-Hosted (Updated)

Old news for some, unless you’re not keeping up with the Trello page. New stuff below the explanation.

If you’re running the self-hosted version of IN on either your own server, or one where you have console/ssh access, I’ve made a handy little bash script to simplify checking for updates, and installing the newest version if it sees that yours is out of date.

The latest version of the script will always be at https://pastebin.com/j657uv9A

So how do you use it? Just change a few lines to make sure it matches your Invoice Ninja install (instructions in the comments), then run it via sudo from any folder in your $PATH (sudo required so it doesn’t hound you for your password when resetting the permissions later). Alternatively, you can place it in the root’s crontab and have it run according to whatever schedule you want, effectively enabling automatic updates. For simplicity’s sake in that case, I just use /usr/local/bin so I don’t have the issue of my main user folder not being decrypted and mounted when I’m not logged in.

Example from my crontab

0 18 * * * /usr/local/bin/ninja_autoupdate >> /usr/local/logs/ninja_update.log

will run the script at 6pm every day, and append the output to a log file that I keep.

Now with that out of the way, on to the new part…

I realized that some of you might be using the UPDATE_SECRET option in the .env file. To keep the script as portable as possible, I added a couple lines to pull that variable from .env and pass that into the script when the ninjaurl/update portion is run at the end. All you need to do is un-comment two lines, and comment out a third (listed in the comments). Haven’t had a chance to test it properly yet (that’ll happen when the next update drops), but I foresee no major issues. But use at your own risk, of course.

Thanks again for your help, we highly recommend using the script to keep your app up to date.

Just confirmed, the UPDATE_SECRET code works. Updated from 4.1.2 to 4.1.3 earlier and everything went fine. The usual disclaimers still apply, obviously.

Appreciate all the efforts, I will say though I much prefer to do updates myself. I hope this won’t be force included in future versions without a disable switch in settings. With that said I do see the great value in this for many, thanks guys.

This method is 100% optional and appears nowhere in the actual Invoice Ninja codebase (I’m not even on the IN team).

It’s literally nothing more than a bash script that carries out the manual update process without user intervention whenever it’s invoked. The only way way it automatically keeps the application up to date is if you specifically set it to run as a cron, otherwise it has to be explicitly run by the user, and will do nothing until the script is told to execute.

Basically, there was an automatic update option in the works for the app itself, but that person seems to have ghosted. So in the meantime, I wrote up the bash script as a convenience to help out other self-hosted users.

It’s firmly a “nice to have”, so I don’t think you have to worry. =)

I’m no linux expert so is there a instructional on how to use this script? like what to save it as, how to run it, etc.

All I get is “: invalid option”

I saved the file as ninjaupdate.sh. then made sure the file had permission of 775.

I’m assuming you edited the appropriate lines already?

": invalid option" means you ended up with DOS-style carriage returns in the file somehow. Check with cat -v ninja_autoupdate (or whatever you saved it as). If you see lines ending in ^M, those will have to go. Easiest way to do that is install dos2unix (sudo apt install dos2unix) then just dos2unix ninja_autoupdate.

As for usage, once you’ve changed the appropriate lines, the only thing left would be to place it in a directory listed under $PATH. I just use /usr/local/bin for convenience, since that’s listed in pretty much every user’s $PATH. To run it, just use sudo ninja_autoupdate (or whatever you saved it as).

I copied the script from the website you have linked above and pasted it into notepad++. Then I saved it as ninjaupdate.sh then uploaded it to my linux server and ran it. Notepad++ switched to unix script syntax as soon as I pasted the code so it recognized it as such. Couldn’t find any formatting errors.

And yes I edited all lines that was supposed to be.

The DOS line breaks are non-printing characters, which is why you need the -v switch when looking at the file with cat. Notepad++ and even nano will fail to show them by default.

I did exactly as you did, and pasted from the link into a new file in Notepad++. While it saves fine and does proper syntax highlighting, those non-printing characters are indeed there, and they were not visible in Notepad++. They only show up when you’re viewing them with an option specifically set to show them. If you view the vile with cat -v ninjaupdate.sh, you’ll see what I mean.

$ cat -v ninjaupdate.sh
#!/bin/bash -ue^M
#Invoice Ninja Self-Hosted Automatic Update - USE AT YOUR OWN RISK^M
#This version will check https://invoiceninja.org for an updated version and install if found.^M
#Tested and works with cron. Lines 51, 54, & 79 output timestamps so you can pipe to a logfile.^M
#This does NOT backup the MySQL database. Use mysqldump if you would like to create a backup.^M
#Replace lines 14 and 58 with your specifics (omit <>), obviously.^M
#If using UPDATE_SECRET in the .env file, uncomment lines 64 & 83, and comment out line 84.^M
#!!IMPORTANT!! Be sure to edit lines 17 & 18 if www-data is not the owner/group for /ninja/storage!^M
 ^M
 ^M
#SET INITIAL VARIABLES^M
#Remaining variables will be set if an update is required^M
#--------------------------------------------------------^M
ninja_home="</path/to/ninja>"^M
ninja_storage="$ninja_home/storage"^M
versiontxt="$ninja_storage/version.txt"^M
storage_owner="www-data"^M
storage_group="www-data"^M
 ^M
 ^M

To fix it, all you need to do is use dos2unix ninjaupdate.sh.

As an aside, you don’t really need to save with the .sh (or any) extension, as it explicitly specifies bash rather than sh at the top of the script.

ok, that worked great. Thanks.

Now I get an error “cat: </var/www/ninja/>/storage/version.txt: No such file or directory”

Do I need to set absolute paths for all the links?

This is what I have set

#SET INITIAL VARIABLES
#Remaining variables will be set if an update is required
#--------------------------------------------------------
ninja_home="</var/www/ninja>"
ninja_storage="$ninja_home/storage"
versiontxt="$ninja_storage/version.txt"
storage_owner=“www-data”
storage_group=“www-data”

Again, sorry for the noob questions.

The <>'s need to go from around the path. I just included them to make it easier to search for what needs to be changed by the user. See line #6 =)

As an aside, I’m working on an updated version of the script that takes away all the guesswork and hand editing. I’m just trying to figure out the issues I’m having with installs that use a key to allow the update commands to trigger. But once it’s done, it will figure out all the specifics on its own.

Ok, my bad for not removing the <>. I ran it but I only get this.

Fri Feb 16 06:49:18 CST 2018 - Updating Invoice Ninja from v3.7.0 to v4.2.2.

But nothing actually happens.

If it kicks out there, then it’s likely one of two things.

  1. You don’t have write permissions on the folder you set in line 58. The script needs to be run as sudo (sudo ninjaupdate.sh)

  2. You don’t have wget installed. sudo apt install wget will fix that.

wget is installed. I have tried everything. Permissions are correct. I have a download folder with 775 set for root and I even tried it for www-data. I’m at a complete loss. Running 14.04 ubuntu (updated). I might just need to reinstall it.

is there any way to print to the screen what is stopping it?

Still definitely sounds like a write permission issue, otherwise you would see the wget status as it downloads the zip file.

Honestly, the best advice I can give would be to setup a VM to play around with. VirtualBox is free, and you can install a barebones Debian or Ubuntu Server system. That way you get a minimal system that’s a blank canvas, and it’s easy to learn more about the system without worrying about breaking anything. I knew next to nothing about Linux save for how to get around in the command line before I rolled my own self-hosted install of Invoice Ninja, so there’s a lot to be said for starting from the ground up. I had to learn everything from user management to configuring sudo to getting ssh working so I could run the VM in headless mode (because VirtualBox with a text console is painful on the eyes).

If you take snapshots regularly, then it’s a simple matter of rolling back if you fry something.

I’ve been running my Base ubuntu install in a VM on my QNAP server.LOL I take nightly snapshots and stor them on my backup QNAP. I also manually snapshot my install when I’m doing this. I use my invoiceninja for my personal billing system and so I need it but at the same time I’d like to get it up to speed. It took me about 5 straight days to get this install working and was a big learning curve.

I have setup a folder (/download) at the root of the filing system and gave it root permissions. root:root 775 on the folder itself. I also run the script with sudo.