/build/static/layout/Breadcrumb_cap_w.png

Automating K1000 Backups

So I was less than thrilled with the method of using ftp to pull the backups. Mostly because it had a ton of extra configuration, but more importantly the implementation of FTP was less then stellar. I have my K1000 in my DMZ and I would have had to open a lot of ports just to allow data transfer. On top of that there was no method of retaining files.

My solution was to write a power shell script that I could schedule using windows task scheduler and have the script clean up my files I no longer needed. The script copies the files from the K1000 to a location on my NAS where I use commvault to back it up at the file level to disk and tape.

This method relies on using the direct http links and using powershell to retrieve the files from those urls.

The script has two components: a .BAT file which calls the PS1 file. This is because you cannot call a powershell script directly from the task schedule so I just the BAT file to load powershell and execute the script.

The script is heavily commented and you should only need to edit files in the Variable - EDIT section. Also to note, there are two back up files, one for the Database which is fairly small and the Files which are fairly large if you are using a lot of deployments etc.

If you have any questions please feel free to comment and I will do my best to help you out.

1. - The Script: K1000Backup.ps1 - Copy this into a txt file and save it as a .ps1 in whichever location you use. Copy and paste into notepad before editing etc. Formatting does not display properly on the blog but it shows up properly in notepad.

#####################################################
#This script will allow you to set a scheduled      #
#task and automatically backup your k1000           #
#please feel free to distribute but just include    #
#the copyright info and url to the original forum   #
#post on itninja,com                                #
#        Copyright 2012 - Andrew Raia               #
#####################################################

###########################
# Variables - EDIT        #
###########################

# This is the FQDN of your kbox
$myKbox = "kbox.domain.com"

# This is the place you wish to store your backups. Use a UNC if you plan on running when no user is logged on.
# Also Make sure account running this script has permissions to this location
$myBackupPath = "UNC Path"

# This defines how many days I want to keep on disk of each backup
$myFilesRetention = "7"
$myDBRetention = "30"

# Set overwrite (set to "enabled" if you want to overwrite backups with the same name, otherwise "disabled")
$myOverwriteOption = "disabled"

##########################################
# Execute Backup Retention - DO NOT EDIT #
##########################################

# Create useable var for the path to all file backups
$FileRetentionPath = "$myBackupPath" + "*.tgz"

# Create useable var for the path to all DB backups
$DBRetentionPath = "$myBackupPath" + "*.gz"

# Removes backup files in the backup path based on the number of days specified in the retention variable
echo "Please be patient while your backup directory is being cleaned..."

# File Retention
echo "Files older than $myFilesRetention day(s) are being removed"
Get-ChildItem "$FileRetentionPath" -recurse | where {$_.Lastwritetime -lt (date).adddays(-$myFilesRetention)} | Remove-Item -Force

# DB Retention
echo "Files older than $myDBRetention day(s) are being removed"
Get-ChildItem "$DBRetentionPath" -recurse | where {$_.Lastwritetime -lt (date).adddays(-$myDBRetention)} | Remove-Item -Force

###########################
# Variables - DO NOT EDIT #
###########################

# This gets the current date and formats it the way the kbox does in the backup file
$myDate = Get-Date -format "yyyyMMdd"

# This is the url for the database files
$urldb = "http://$myKbox/common/download_file.php?FILENAME=/kbackup/$myDate" + "_k1_dbdata.gz"

# This is the path that it will store your backups in
$pathdb = "$myBackupPath" + "$myDate" + "_k1_dbdata.gz"

# This is the url for the Files backup
$urlfile = "http://$myKbox/common/download_file.php?FILENAME=/kbackup/$myDate" + "_kbox_file.tgz"

# This is the path that it will store your files backup in
$pathfile = "$myBackupPath" + "$myDate" + "_kbox_file.tgz"

$myDBExist = Test-Path $pathdb
$myFileExist = Test-Path $pathfile

###################################
# Execute DB Backup - DO NOT EDIT #
###################################

echo "Please be patient while your K1000 files are being copied...."

# Check to see if the DB backup already exists, if not go ahead and copy it
if ($myDBExist -eq $False) {
echo "DB Backup is being copied"
$clientDB = new-object System.Net.WebClient
$clientDB.DownloadFile( $urldb, $pathdb)
}
# If the database already exists and overwrite is enabled then copy overwrite the existing file
elseif ($myDBExist -eq $True -And $myOverwriteOption -eq "enabled"){
echo "DB Backup already exists but is being overwritten because overwrite is enabled"
$clientDB = new-object System.Net.WebClient
$clientDB.DownloadFile( $urldb, $pathdb)
}
# If the database backup already exists and overwrite is disabled, then do nothing
else {
echo "DB Backup is being skipped because overwite is disabled and the file already exists"
}

#####################################
# Execute File Backup - DO NOT EDIT #
#####################################

# Check to see if the DB backup already exists, if not go ahead and copy it
if ($myFileExist -eq $False) {
echo "File Backup is being copied"
$clientfile = new-object System.Net.WebClient
$clientfile.DownloadFile( $urlfile, $pathfile)
}
# If the database already exists and overwrite is enabled then copy overwrite the existing file
elseif ($myFileExist -eq $True -And $myOverwriteOption -eq "enabled"){
echo "File Backup already exists but is being overwritten because overwrite is enabled"
$clientfile = new-object System.Net.WebClient
$clientfile.DownloadFile( $urlfile, $pathfile)
}
# If the database backup already exists and overwrite is disabled, then do nothing
else {
echo "File Backup is being skipped because overwite is disabled and the file already exists"
}


##############################
# Debugging                  #
##############################

# Uncomment the following to verify your variables. You may also want to comment out execute backup sections above to skip copying.
# echo "DB URL: $urldb"
# echo "DB Path: $pathdb"
# echo "File URL: $urlfile"
# echo "File Path: $pathfile"
# echo "DB Backup Already Exists: $myDBExist"
# echo "File Backup Already Exists: $myFileExist"
# echo "FileRetentionPath: $FileRetentionPath"
# echo "DB Retention Path: $DBRetentionPath"

 

2. The .BAT file - K1000Backup .bat - copy this into a txt file and save it as a .bat in the same location as your powershell file. Make sure the location in this bat file points to the path of your powershell file as well as the path for the log file.

@echo off
PowerShell.exe "C:\Scripts\k1000backup\k1000Backup.ps1" >c:\Scripts\K1000backup\log.txt

3. Schedule a task to run the .BAT file according to your desired schedule. I set mine to run daily, and set it to run hidden even when a user is not logged on. 


Comments

  • Awesome. We just recently started looking at K1000 Backups and were disappointed in seeing the FTP solution from KACE. I'll bookmark this and when that project comes up, give this a shot first. - samzeeco 11 years ago
    • No problem, let me know if you need any help. - MoranTug 11 years ago
  • This works perfectly. Thanks so much! - omgitsadam 11 years ago
    • No problem, really glad it worked for you :) - MoranTug 11 years ago
  • I tried this script out yesterday and it ran perfectly first time, but since last nights run the files are appearing on my backup share and the log shoes that they are being copied, however they are 0KB each. They should be about 1GB and 1.5MB. Any ideas? - asheridan 11 years ago
  • Try enabling the debugging section and step through it, see if anything is not coming back as expected. - MoranTug 11 years ago
    • Hi Moran Tug, i am just now implementing a backup and found this. The script runs successfully but the download DB and File size those not match what is on the server. Downloaded file for both only shows 44 KB but on the KACE server it shows 208.40 MB (DB) and 8 GB (Files). - mandapat 9 years ago
  • Hello, Thanks for the script. but I get an error when running it.
    Also how do I add the set-execution policy on the script. Thanks.

    PS C:\K1000\K1000Scripts> .\K1000Backup.ps1
    Please be patient while your backup directory is being cleaned...
    Files older than 7 day(s) are being removed
    Get-ChildItem : Illegal characters in path.
    At C:\K1000\K1000Scripts\K1000Backup.ps1:43 char:14
    + Get-ChildItem <<<< "$FileRetentionPath" -recurse | where {$_.Lastwritetime -lt (date).adddays(-$myFilesRetention)} |
    Remove-Item -Force
    Files older than 30 day(s) are being removed
    Get-ChildItem : Illegal characters in path.
    At C:\K1000\K1000Scripts\K1000Backup.ps1:47 char:14
    + Get-ChildItem <<<< "$DBRetentionPath" -recurse | where {$_.Lastwritetime -lt (date).adddays(-$myDBRetention)} | Remo
    ve-Item -Force
    Please be patient while your K1000 files are being copied....
    DB Backup is being copied
    Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."
    At C:\K1000\K1000Scripts\K1000Backup.ps1:81 char:23
    + $clientDB.DownloadFile( <<<< $urldb, $pathdb)
    File Backup is being copied
    Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."
    At C:\K1000\K1000Scripts\K1000Backup.ps1:102 char:25
    + $clientfile.DownloadFile( <<<< $urlfile, $pathfile) - shanky1972 10 years ago
  • What are the values for these two items exactly as you entered them?
    $myKbox = "kbox.domain.com"
    $myBackupPath = "UNC Path"

    For the execution policy you can just run this once on the machine you are running the script on
    Set-ExecutionPolicy unrestricted - MoranTug 10 years ago
  • $myKbox="sdisbkacek1" as I can get to this from my browser. and \\wdisb0111\K1000BP (wdisb0111 is my PC) from where I ran the PS. As far as the Execution policy, that is exactly what I did to run from my PC one time. - shanky1972 10 years ago
  • $myKbox should be the name of your kbox FQDN so kbox.domain.com and the unc looks ok, , can you enable the debug section and copy and past the echo showing all the vars? - MoranTug 10 years ago
  • ok. i ran the debug and found out that I need to add the \ after the file path.

    Please be patient while your backup directory is being cleaned...
    Files older than 7 day(s) are being removed
    Get-ChildItem : Illegal characters in path.
    At C:\K1000\K1000Scripts\k1000Backup.ps1:43 char:14
    + Get-ChildItem <<<< "$FileRetentionPath" -recurse | where {$_.Lastwritetime -lt (date).adddays(-$myFilesRetention)} |
    Remove-Item -Force
    Files older than 30 day(s) are being removed
    Get-ChildItem : Illegal characters in path.
    At C:\K1000\K1000Scripts\k1000Backup.ps1:47 char:14
    + Get-ChildItem <<<< "$DBRetentionPath" -recurse | where {$_.Lastwritetime -lt (date).adddays(-$myDBRetention)} | Remo
    ve-Item -Force
    Please be patient while your K1000 files are being copied....
    DB Backup is being copied
    Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."
    At C:\K1000\K1000Scripts\k1000Backup.ps1:81 char:23
    + $clientDB.DownloadFile( <<<< $urldb, $pathdb)
    File Backup is being copied
    Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."
    At C:\K1000\K1000Scripts\k1000Backup.ps1:102 char:25
    + $clientfile.DownloadFile( <<<< $urlfile, $pathfile)
    DB URL: http://sdisbkacek1/common/download_file.php?FILENAME=/kbackup/20130510_k1_dbdata.gz
    DB Path: \\wdisb0111\K1000BP20130510_k1_dbdata.gz
    File URL: http://sdisbkacek1/common/download_file.php?FILENAME=/kbackup/20130510_kbox_file.tgz
    File Path: \\wdisb0111\K1000BP20130510_kbox_file.tgz
    DB Backup Already Exists: False
    File Backup Already Exists: False
    FileRetentionPath: \\wdisb0111\K1000BP*.tgz
    DB Retention Path: \\wdisb0111\K1000BP*.gz - shanky1972 10 years ago
  • I ran it again and now get this error.

    Please be patient while your backup directory is being cleaned...
    Files older than 7 day(s) are being removed
    Files older than 30 day(s) are being removed
    Please be patient while your K1000 files are being copied....
    DB Backup is being copied
    Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."
    At C:\K1000\K1000Scripts\k1000Backup.ps1:81 char:23
    + $clientDB.DownloadFile( <<<< $urldb, $pathdb)
    File Backup is being copied
    Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."
    At C:\K1000\K1000Scripts\k1000Backup.ps1:102 char:25
    + $clientfile.DownloadFile( <<<< $urlfile, $pathfile) - shanky1972 10 years ago
  • make sure your kbox url has the domain name at the end to be safe, can you copy and paste that link in your browser? does the file download locally? - MoranTug 10 years ago
  • Hi MoranTug, I got it working. initially it was the folder path (missing the \) and now it was the folder permissions. I corrected it and it works. Thank you so much.

    Please be patient while your backup directory is being cleaned...
    Files older than 7 day(s) are being removed
    Files older than 30 day(s) are being removed
    Please be patient while your K1000 files are being copied....
    DB Backup is being copied
    File Backup is being copied - shanky1972 10 years ago
    • awesome, Glad you finally got it working. - MoranTug 10 years ago
  • as far as running the ps with the unrestricted, how do I add that to the script or the batch file? - shanky1972 10 years ago
    • you dont want that part of the batch file unless you want to take it out of restricted mode after you are done, which I would advise.

      (ADD)

      #######################
      # Set PowerShell Policy #
      #######################

      Set-ExecutionPolicy unrestricted

      (AFTER)

      # Copyright 2012 - Andrew Raia #
      #####################################################


      (ADD)

      ########################
      # Revert PowerShell Policy #
      ########################

      Set-ExecutionPolicy Restricted (or whichever mode you want)

      (AFTER)

      echo "File Backup is being skipped because overwite is disabled and the file already exists"
      }



      hope that makes sense - MoranTug 10 years ago
  • I added these to the ps1 script but get the same error about the execution policy. should we set these on the batch file or on the ps1 file?

    File C:\K1000\K1000Scripts\k1000Backup.ps1 cannot be loaded because the executi
    on of scripts is disabled on this system. Please see "get-help about_signing" f
    or more details.
    At line:1 char:37
    + C:\K1000\K1000Scripts\k1000Backup.ps1 <<<< - shanky1972 10 years ago
  • this is what I have.

    #####################################################
    #This script will allow you to set a scheduled #
    #task and automatically backup your k1000 #
    #please feel free to distribute but just include #
    #the copyright info and url to the original forum #
    #post on itninja,com #
    # Copyright 2012 - Andrew Raia #
    #####################################################

    #######################
    # Set PowerShell Policy #
    #######################

    Set-ExecutionPolicy unrestricted

    ###########################
    # Variables - EDIT #
    ###########################

    # This is the FQDN of your kbox
    $myKbox = "sdisbkacek1"

    # This is the place you wish to store your backups. Use a UNC if you plan on running when no user is logged on.
    # Also Make sure account running this script has permissions to this location
    $myBackupPath = "\\wdisb0111\K1000BP\"

    # This defines how many days I want to keep on disk of each backup
    $myFilesRetention = "7"
    $myDBRetention = "30"

    # Set overwrite (set to "enabled" if you want to overwrite backups with the same name, otherwise "disabled")
    $myOverwriteOption = "disabled"

    ##########################################
    # Execute Backup Retention - DO NOT EDIT #
    ##########################################

    # Create useable var for the path to all file backups
    $FileRetentionPath = "$myBackupPath" + "*.tgz"

    # Create useable var for the path to all DB backups
    $DBRetentionPath = "$myBackupPath" + "*.gz"

    # Removes backup files in the backup path based on the number of days specified in the retention variable
    echo "Please be patient while your backup directory is being cleaned..."

    # File Retention
    echo "Files older than $myFilesRetention day(s) are being removed"
    Get-ChildItem "$FileRetentionPath" -recurse | where {$_.Lastwritetime -lt (date).adddays(-$myFilesRetention)} | Remove-Item -Force

    # DB Retention
    echo "Files older than $myDBRetention day(s) are being removed"
    Get-ChildItem "$DBRetentionPath" -recurse | where {$_.Lastwritetime -lt (date).adddays(-$myDBRetention)} | Remove-Item -Force

    ###########################
    # Variables - DO NOT EDIT #
    ###########################

    # This gets the current date and formats it the way the kbox does in the backup file
    $myDate = Get-Date -format "yyyyMMdd"

    # This is the url for the database files
    $urldb = "http://$myKbox/common/download_file.php?FILENAME=/kbackup/$myDate" + "_k1_dbdata.gz"

    # This is the path that it will store your backups in
    $pathdb = "$myBackupPath" + "$myDate" + "_k1_dbdata.gz"

    # This is the url for the Files backup
    $urlfile = "http://$myKbox/common/download_file.php?FILENAME=/kbackup/$myDate" + "_kbox_file.tgz"

    # This is the path that it will store your files backup in
    $pathfile = "$myBackupPath" + "$myDate" + "_kbox_file.tgz"

    $myDBExist = Test-Path $pathdb
    $myFileExist = Test-Path $pathfile

    ###################################
    # Execute DB Backup - DO NOT EDIT #
    ###################################

    echo "Please be patient while your K1000 files are being copied...."

    # Check to see if the DB backup already exists, if not go ahead and copy it
    if ($myDBExist -eq $False) {
    echo "DB Backup is being copied"
    $clientDB = new-object System.Net.WebClient
    $clientDB.DownloadFile( $urldb, $pathdb)
    }
    # If the database already exists and overwrite is enabled then copy overwrite the existing file
    elseif ($myDBExist -eq $True -And $myOverwriteOption -eq "enabled"){
    echo "DB Backup already exists but is being overwritten because overwrite is enabled"
    $clientDB = new-object System.Net.WebClient
    $clientDB.DownloadFile( $urldb, $pathdb)
    }
    # If the database backup already exists and overwrite is disabled, then do nothing
    else {
    echo "DB Backup is being skipped because overwite is disabled and the file already exists"
    }

    #####################################
    # Execute File Backup - DO NOT EDIT #
    #####################################

    # Check to see if the DB backup already exists, if not go ahead and copy it
    if ($myFileExist -eq $False) {
    echo "File Backup is being copied"
    $clientfile = new-object System.Net.WebClient
    $clientfile.DownloadFile( $urlfile, $pathfile)
    }
    # If the database already exists and overwrite is enabled then copy overwrite the existing file
    elseif ($myFileExist -eq $True -And $myOverwriteOption -eq "enabled"){
    echo "File Backup already exists but is being overwritten because overwrite is enabled"
    $clientfile = new-object System.Net.WebClient
    $clientfile.DownloadFile( $urlfile, $pathfile)
    }
    # If the database backup already exists and overwrite is disabled, then do nothing
    else {
    echo "File Backup is being skipped because overwite is disabled and the file already exists"
    }

    ########################
    # Revert PowerShell Policy #
    ########################

    Set-ExecutionPolicy Restricted

    ##############################
    # Debugging #
    ##############################

    # Uncomment the following to verify your variables. You may also want to comment out execute backup sections above to skip copying.
    # echo "DB URL: $urldb"
    # echo "DB Path: $pathdb"
    # echo "File URL: $urlfile"
    # echo "File Path: $pathfile"
    # echo "DB Backup Already Exists: $myDBExist"
    # echo "File Backup Already Exists: $myFileExist"
    # echo "FileRetentionPath: $FileRetentionPath"
    # echo "DB Retention Path: $DBRetentionPath" - shanky1972 10 years ago
    • whoops my bad lol, yea that will not work on the script, all you have to do is open your powershell cmd prompt and set the powershell mode to unrestricted. Only have to do that once and the mode will remain in tact. Do you not want to leave it in unrestricted mode? - MoranTug 10 years ago
  • you are correct. I dont want to leave it in unrestricted mode? so how can I include those in the batch file? or can I include those on the task schedulef? - shanky1972 10 years ago
    • try this in the bat file after the @echo off

      powershell -noprofile Set-ExecutionPolicy Unrestricted

      then after the ps1 is called

      powershell -noprofile Set-ExecutionPolicy restricted

      if that one doesn't work try this format

      powershell -command "& {Set-ExecutionPolicy Unrestricted}" - MoranTug 10 years ago
  • MoranTug. I modified it like this and it works
    @echooff
    powershell -noprofile Set-ExecutionPolicy Unrestricted
    powerShell "C:\K1000\K1000Scripts\k1000Backup.ps1" >C:\K1000\K1000Scripts\log.txt
    powershell -noprofile Set-ExecutionPolicy restricted

    Thanks again. you made my day. - shanky1972 10 years ago
    • Awesome, glad to hear it worked. God Bless! - MoranTug 10 years ago
  • AWESOME POST! This is so very simple and elegantly written. Thank you, MoranTug! This worked beautifully. - awingren 10 years ago
  • Thanks awingren, wish by now they would have built a feature to dump backups off the box :/ - MoranTug 10 years ago
    • It is surprising they don't, but this is a great solution! Thanks again! - awingren 10 years ago
  • Awesome !! thank you - binuani 10 years ago
  • stopped working after upgrading to 6.0 :-( - binuani 9 years ago
  • Sorry Binuani :(. We moved to Kaseya so I can't really troubleshoot for you, but my GUESS would be that the PATH to the backup file changed. Navigate to the backup section and look for the file that you can manually download and compare the full path to this output

    http://$myKbox/common/download_file.php?FILENAME=/kbackup/$myDate" + "_kbox_file.tgz" - MoranTug 9 years ago
  • The Script runs with Version: 6.0.101863, too.

    We had to change in the Preferences -> Systemcontrol? -> Security
    the setting: Secure Backup Data - Karre 9 years ago
  • The PATH is the same in 6.0. However, you'll have to navigate to Settings>Control Panel>Security Settings and uncheck 'Enable secure backup files'.

    Also, the update changed the default time that the KACE runs its backup to 22:00 (in our environment anyway). The script is looking for files with the current date, so if you are scheduling this script prior to 22:00, the files won't be there. You need to have the script set to run on the same day AFTER the backup is complete. The time KACE runs the backup can be adjusted in Settings>Control Panel>Backup Settings.

    Thanks MoranTug for putting this together! Maybe 7.0 will be the version where we no longer need it... - Fletchdust 9 years ago
  • I really like this script, it really well with Kace 5.5 but since we have moved to 6.0 not so much I see the post where you have to uncheck the Secure Backup files, is there a way to make this script work with Secure backup files enabled? - haustin 9 years ago
  • Trying this script out now to backup our KBOX 1100. However, in the generated log, I get an "The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel" error for each file copy process. Can someone direct me how to resolve? Our KBOX has an SSL certificate. - JoeOC 9 years ago
    • Did you get this figured out? We are just now getting this set up and have the same issue with the SSL. - Ballen 8 years ago
      • Ballen,

        Nope...never got a chance to revisit this. I may need to contact KACE (Dell) Support to expedite getting this setup. - joaxe 8 years ago
      • Sorry...just used my other account.

        Joe OC - JoeOC 8 years ago
  • Hello Moran Tug, I have been using your script and it has been working fine till we did a hardware refresh to the new Kace box (keeping the same hostname and IP), I have made sure the secure backup is checked. called dell and they are not sure if the backup location has changed and they want me to run a different script and this one does not automatically delete the old backups. Have you come across this issue and if so, do you have a fix for this issue?. Thank you. - shanky1972 7 years ago
This post is locked

Don't be a Stranger!

Sign up today to participate, stay informed, earn points and establish a reputation for yourself!

Sign up! or login

Share

 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ