mssql-to-csv
A small, lightweight self-contained Go program that eats MS SQL for breakfast
and spits out CSV for lunch
Usage
mssql-to-csv.exe --help
Example:
mssql-to-csv.exe --env env.txt --map map.txt --log log.txt
To see the full CSV output in the logs add --debug
:
mssql-to-csv.exe --env env.txt --map map.txt --log log.txt --debug
Configuration
Secrets
See example.env
.
On Windows use WordPad and edit the file as env.txt
.
IMPORTANT
DB Column to CSV Field Mappings:
See map.txt
.
map.txt
will define which columns will be exported (if they do not exist, they
will be left blank), and in which order.
CSV Field Name: DatabaseColumnName
For example:
Name of Fruit: FruitName
Type of Fruit: FruitType
Extra Info: DOESNT_EXIST
Quantity of Fruit: FruitQuantity
Name of Fruit,Type of Fruit,Extra Info,Quantity of Fruit
Apple,Sweet or Sour,,7
Orange,Citrus,,3
Note that the order is important.
Install
You will need:
mssql-to-csv.exe
(Windows) or mssql-to-csv
(Mac, Linux)
- Note: You can grab the latest version for your platform on the
Releases Page
- You may need to place the binary in a folder in your system's
PATH
map.txt
.env
, which can be modeled after example.env
- Note: Do NOT commit secrets, tokens, passwords, etc to git!!
Build
Here's how you build from source.
STOP: Do you need to build this?
In most instances you do NOT need to build from source. You can get a
pre-built version from the releases page.
Install Go
If you do need to build this, you'll need go
and goreleaser
.
Install go
(and follow the onscreen instructions):
Mac, Linux:
curl -sS https://webinstall.dev/go | bash
Windows 10:
curl.exe -A MS https://webinstall.dev/go | powershell
Windows System Service
Task Schedule
- Search Task Scheduler
- Create Task...
- (that brings you to Properties automatically)
- General Tab
- Give descriptive name
- Run whether user is logged on or not
- (can run as a service account, does not need admin permissions)
- Triggers Tab
- Actions Tab
- Settings Tab
- Stop the task if it runs longer than:
1 hour
nssm
- Download nssm.exe
- Open a Command Prompt
- Register system service
nssm install mssql-to-csv mssql-to-csv.exe --env .env --map map.txt --log log.txt
- Start the service
nssm start mssql-to-csv
- Set service to start on boot
nssm set Start Automatic
One off build (ex: for Windows)
When you need to build for testing, you can do so like this
git clone git@github.com:therootcompany/mssql-to-csv.git
pushd ./mssql-to-csv/
# Build for Windows Command Prompt
go build -v -mod=vendor -race -o mssql-to-csv.debug.exe .
# Build for headless Windows system service
go build -v -mod=vendor -race -ldflags "-H windowsgui" -o mssql-to-csv.exe .
Create a Release
When you want to create a release, tag it, and then run goreleaser
Install goreleaser
:
curl -sS https://webinstall.dev/goreleaser | bash
Test build:
goreleaser --snapshot --skip-publish --rm-dist
Proper release build
# check existing version tags
git tag
# create a new version, such as v1.0.37, after committing and testing
git tag v1.x.y
# release it to github (working git directory must be clean)
goreleaser --rm-dist
Note: You should have a Github Personal Access Token in the file at
~/.config/goreleaser/github_token
.
The contents should be just the token, which is a random hexadecimal string that
looks like this: 48b8b13473965ead05889fa073530c630ca70da42
LICENSE
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
Copyright 2021 AJ ONeal
Copyright 2021 The Root Group, LLC