MsSqlBackup 1.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package MsSqlBackup --version 1.0.2
NuGet\Install-Package MsSqlBackup -Version 1.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MsSqlBackup" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MsSqlBackup --version 1.0.2
#r "nuget: MsSqlBackup, 1.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install MsSqlBackup as a Cake Addin
#addin nuget:?package=MsSqlBackup&version=1.0.2

// Install MsSqlBackup as a Cake Tool
#tool nuget:?package=MsSqlBackup&version=1.0.2

Ms Sql Backup

Tiny DAO utility that backs up any Ms Sql Db (any edition) to a local or network drive.

Interface

The nuGet has three method.

One async, one purely sync and one fully running in background

using System;
using System.Threading.Tasks;
using pvWay.MethodResultWrapper.Interfaces;

namespace pvWay.MsSqlBackup
{
    public interface IMsSqlBackupCreator
    {
        /// <summary>
        /// Creates a full backup of the database
        /// </summary>
        /// <param name="bakFileName">Fully qualified backup file name where the running app has write access</param>
        /// <returns>MethodResult see pvWay MethodResultWrapper nuGet package</returns>
        Task<IMethodResult> BackupDbAsync(string bakFileName);

        /// <summary>
        /// Creates a full backup of the database
        /// </summary>
        /// <param name="bakFileName">Fully qualified backup file name where the running app has write access</param>
        /// <returns>MethodResult see pvWay MethodResultWrapper nuGet package</returns>
        IMethodResult BackupDb(string bakFileName);

        /// <summary>
        /// Creates a full backup of the database in background
        /// </summary>
        /// <param name="bakFileName">Fully qualified backup file name where the running app has write access</param>
        /// <param name="callback">A method that will be called on completion</param>
        /// <returns>void</returns>
        void BgBackupDb(string bakFileName, Action<IMethodResult> callback);
    }
}

Usage

See here after a short Console that use the service

Principe

  • need to pass a work folder where the Ms Sql Server has write access
  • need to pass the connection string
  • need to pass a destination file where the app has write access
  • you can follow up on progress by passing a notifier callback

remark: you'll need to get the MethodResultWrapper to use this service

The code


using System;
using pvWay.MethodResultWrapper.Model;
using pvWay.MsSqlBackup;

namespace MsSqlBackupLab
{
    internal static class Program
    {
        private static void Main(/*string[] args*/)
        {
            // need to pass the ILoggerService from pvWay MethodResultWrapper nuGet package
            // her I use the ConsoleLogger implementation
            // see https://github.com/licheez/pvWayNuGetsSolution/tree/master/MethodResultWrapperSolution/MethodResultWrapper
            var ls = new ConsoleLogger();

            // make sure Ms Sql Server has write access to this work folder
            const string localWorkFolder = "d:\\temp";

            // the connection string to the db you want to back up
            const string connectionString = "data source = localhost; " +
                                            "initial catalog = TCM004_DEV; " +
                                            "integrated security = True; " +
                                            "MultipleActiveResultSets = True; ";

            var backupCreator = new MsSqlBackupCreator(
                ls,
                localWorkFolder,
                connectionString,
                // let's redirect progress notifications to the console
                Console.WriteLine);

            // let's generate a unique bak file name using a GUID
            var id = Guid.NewGuid().ToString();
            var bakFileNameNwDrive = $"y:\\MsSqlBak\\Tcm004_{id}.bak";

            // ok now launch the backup
            var res = backupCreator
                .BackupDbAsync(bakFileNameNwDrive).Result;

            /*
            
            CONSOLE Should display this
            ---------------------------
            nuGet pvWay.MsSqlBackup connecting to localhost
            nuGet pvWay.MsSqlBackup backing up database TCM004_DEV to local work folder d:\temp\
            nuGet pvWay.MsSqlBackup copying backup file to destination: y:\MsSqlBak\Tcm004_85daa4de-d2b3-42e8-99bb-aad2252f1b41.bak
            nuGet pvWay.MsSqlBackup removing temp file from d:\temp

            */

            if (res.Failure)
            {
                ls.Log(res);
            }
            else
            {
                Console.WriteLine("Backup completed");
            }

            Console.WriteLine("hit a key to quit");
            Console.ReadKey();
        }
    }
}

Happy coding

Product Compatible and additional computed target framework versions.
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.3 538 8/27/2020
1.0.2 396 8/26/2020
1.0.0 513 8/24/2020

Added sync and background methods