Jump to content
DarrenWhite99

Automatic Script Backup and Revision Control

Recommended Posts

Well, nearly Automatic.. You just provide the scheduling! This script was inspired by the PowerShell version at http://labtechconsulting.com/labtech-script-backup-and-version-control/. But that solution is not compatible with Cloud Hosted partners.

This complete rewrite uses only LabTech scripting functions so it is compatible with both Cloud Hosted and On-Prem servers. It will export all scripts into a folder hierarchy that matches their organization in your environment. Each time a script is exported, the last updated time and user information is included, providing multiple script revisions. This script does not decode the scriptdata, so script dependencies like EDF's or other scripts will not be bundled in the XML export. But if you are just looking to undo a change, the script dependencies should likely exist already.

The backups will be created at the folder identified by "@BackupRoot@", which you can provide as a script parameter when scheduling if you do not want to use the default path. Target the script against an online agent, and the script data will be backed up to that computer. Future runs will reference the saved "Script State" variable for that agent and will only include the scripts updated since the last successful backup. Backup verification is performed, if a script backup file was not created as expected, the backup timestamp will not be changed, allowing the backup to be attempted again.

I am attaching a .zip bundle, containing scripts actually backed up by this solution. Import the "Send Email" script first, and then import the "Backup" script. If there are any problems or you would rather import a script exported by Automate, the "Backup Automate Scripts.xml" is included as well. You do not need to import all three files!

UPDATE - 20180409 - Script folders are now nested (before only the parent folder was included in the path) and folder paths do their best to support tricky characters. Also the export is never decoded inside the script engine, so script notes that referenced variable names will no longer be replaced. There is less "HTML" processing so script notes should retain their CRLF characters.

UPDATE - 20171205 - Support for UTF-8 Characters in Script Names and Scriptlets. Scripts are now MD5 Hashed instead of date checked. If the script has been CHANGED, even if it was restored to an earlier date, it will be backed up on the next pass. Scriptlets are MD5 Hashed and are only backed up if they have changed. Scripts/Scriptlets that were removed since the last backup will be reported.

UPDATE - 20170531 - Script is MUCH faster now. I bypassed the individual file writes and now send script bundles up to 32K in size. And even those commands are submitted in batches. Backing up nearly 1000 scripts took 2 or 3 hours before. It now takes under 5 minutes!

Update - 20170620 - Script now also exports your Scriptlets (all of them, every time, no versioning). Also replaced a variable that apparently wasn't supported in LabTech 10.5 for backwards compatibility.

I also have script unpacking and comparison in the works, so I am hoping a future update will include some level of "changelog" details generated automatically. Optionally I could just dump out the unpacked script data, and so that you could use you own versioning/diff tools for comparison. Let me know your thoughts.

I have moved the download to the File Downloads area - See https://www.labtechgeek.com/files/file/17-script-backup/automatic automatically schedule automate custom script version revision archive backup

 

Edited by DarrenWhite99
Relocated Script Download

Share this post


Link to post
Share on other sites

I'm still running 10.5. I obviously get the compatibility prompts when importing these as they are from a newer version.

Unfortunately the Script Run command doesn't properly find/associate with the Email script. And even if I try to update that line to point to the script, I still end up with an error when saving it and it corrupts the script so it doesn't work. Could you export both of these together in one export file to see if that solves this issue? Or maybe I just need to get upgraded to LT 11 (or 12 after next week).

Share this post


Link to post
Share on other sites

I have updated the .zip with a current script export from LabTech, as well as the current backup as made from the script. BTW, there are TWO lines that reference the email script.. Are you only updating one? If the Email script was imported first, the backup script should match up to it anyways (I believe that it uses the script GUID, but am not sure). I would be curious to know if you are able to use the backups the script is creating or if they are always reported as corrupt. Does the Email notification script import successfully, and are you able to open and save it without error?

Share this post


Link to post
Share on other sites

Good news! I was able to import, update the Script Run lines, and save the script with no errors. Honestly, I think my failure to see there were TWO places for Script Run was probably the failing point originally.

And I know from past experiences, the script references are based on ID and not GUID. So when you do this, you always have this issue and have to update Script Run lines.

THANKS!! Now to test this out.

Share this post


Link to post
Share on other sites

Now that I've got the script running, I've still got an issue or two left to solve.

On first run of the script, it successfully created all of the folders on the filesystem that mimic our script hierarchy in CWA. But none of the scripts themselves were exported.

The email log contains this:

 

6/19/2017 5:56:27 PM
Starting Script Export at 6/19/2017 5:13:51 PM.
Backing up scripts updated since 12:00:00 AM.
761 scripts identified for backup.
Starting extraction of script backups -  Mon 06/19/2017 17:56:03.70
The argument 'E:\CWAutomate\ScriptArchives\BUNDLE-.ps1' to the -File parameter does not exist. Provide the path to an existing '.ps1' file as an argument to the -File parameter.
Could Not Find E:\CWAutomate\ScriptArchives\BUNDLE-.ps1
The argument 'E:\CWAutomate\ScriptArchives\BUNDLE-.ps1' to the -File parameter does not exist. Provide the path to an existing '.ps1' file as an argument to the -File parameter.
...........
Extraction Completed -  Mon 06/19/2017 17:56:16.50

Then it shows error on all of the validations obviously as well.

 

When I look at my ScriptArchives folder, I see a file called BUNDLE-%scriptstepcounter%.ps1

Share this post


Link to post
Share on other sites
When I look at my ScriptArchives folder, I see a file called BUNDLE-%scriptstepcounter%.ps1

It looks like the "%scriptstepcounter%" variable isn't supported in LabTech 10.5. I replaced the variable with a simple counter variable, it only was used to give each backup file a unique name. Give it another shot and see if it works for you now.

Share this post


Link to post
Share on other sites

Having some trouble with the script when scheduling it. When I run this script one time against my own workstation it backs up every script as expected.  The same happens when its manually run against the Automate server. However after scheduling the script on the Automate server service plan it is inconsistent in that it doesn't always email the results and it always misses a large number of scripts in the backup destination. The scripts it has ignored do not appear in the email job log - I'd estimate that around 150 scripts are missed. All locations I've tested on use the same folder structure and permissions.

Share this post


Link to post
Share on other sites

It uses scriptstate variables to track new and changed scripts. You will need to track them down and remove them to reset the backups. IIRC, it uses the LTRunByUser variable that is set when you schedule a script. When you schedule a reoccurring script you need to make sure that variable is in the parameters, or it will think it is supposed to email “root”.

Share this post


Link to post
Share on other sites

Posted another update:
Script folders are now nested (before only the parent folder was included in the path) and folder paths do their best to support tricky characters. Also the export is never decoded inside the script engine, so script notes that referenced variable names will no longer be replaced. There is less "HTML" processing so script notes should retain their CRLF characters.

Updated file is attached to the topic post.( https://www.labtechgeek.com/topic/3377-automatic-script-backup-and-revision-control/ )

Share this post


Link to post
Share on other sites

I scheduled the script on our Automate server. It looks like the script completes successfully. I see that it didn't create CWAutomate folder under %systemdrive%. Is that something I need to create? does it backup all scripts the first time it runs?

I also temporarily disabled line 377, and 391. I guess they are there to run another script to email result?

 

Here is the script result.

Starting Backups at 1/14/2020 3:54:45 PM.
Last backup was 1/1/0001 12:00:00 AM.
No Scripts were found needing backup.
No Scriptlets were found needing backup.
No Internal Monitors were found needing backup.
No Group Monitors were found needing backup.
No Remote Monitors were found needing backup.
No Dataviews were found needing backup.
No ExtraData Fields were found needing backup.
No Role Detections were found needing backup.
No Virus Scanners were found needing backup.
All backups completed at 1/14/2020 3:56:23 PM.

Is the first backup saved somewhere else? What can I do to test this?

 

Thanks!

Share this post


Link to post
Share on other sites

It showed that there were no prior backups and nothing found to backup, so the SQL queries failed to return any results.  Are you running MySQL 5.7?  5.6 (or was it  5.5?) doesn’t include the BASE64 functions needed so that could be the problem.

Share this post


Link to post
Share on other sites

That’s the problem. You could try replacing  the TO_BAS64/FROM_BASE64 calls with BASE64_ENCODE/BASE64_DECODE. Those are stored procedures available in Automate, but they aren’t as fast as the native functions I’m using in 5.6+. 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...