2016-04-13 11:44:21 +02:00
using System ;
using System.Collections.Generic ;
2019-05-01 21:30:22 +02:00
using System.Diagnostics ;
2016-04-13 11:44:21 +02:00
using System.Linq ;
2016-09-15 14:39:19 +02:00
using System.Reflection ;
2019-05-01 21:30:22 +02:00
using System.Security.Principal ;
2016-04-13 11:44:21 +02:00
using System.ServiceProcess ;
using System.Text ;
using System.Threading.Tasks ;
namespace Duplicati.WindowsService
{
2016-10-12 22:58:48 +02:00
public class Program
2016-04-13 11:44:21 +02:00
{
2016-10-12 22:58:48 +02:00
[STAThread]
public static int Main ( string [] args )
{
return Duplicati . Library . AutoUpdater . UpdaterManager . RunFromMostRecent ( typeof ( Program ). GetMethod ( "RealMain" ), args , Duplicati . Library . AutoUpdater . AutoUpdateStrategy . Never );
}
public static void RealMain ( string [] args )
2016-04-13 11:44:21 +02:00
{
2018-10-06 15:50:05 -07:00
var install = args != null && args . Any ( x => string . Equals ( "install" , x , StringComparison . OrdinalIgnoreCase ));
var uninstall = args != null && args . Any ( x => string . Equals ( "uninstall" , x , StringComparison . OrdinalIgnoreCase ));
var help = args != null && args . Any ( x => string . Equals ( "help" , x , StringComparison . OrdinalIgnoreCase ));
2016-09-15 14:39:19 +02:00
2016-09-23 14:11:53 +02:00
if ( help )
{
Console . WriteLine ( "This is a Windows Service wrapper tool that hosts the Duplicati.Server.exe process, letting it run as a windows service." );
Console . WriteLine ( "|To run from a console, run the Duplicati.Server.exe instead of Duplicati.WindowsService.exe" );
Console . WriteLine ();
2019-05-01 21:30:22 +02:00
Console . WriteLine ( "Supported commands (Must be run as Administrator):" );
2016-09-23 14:11:53 +02:00
Console . WriteLine ( " install:\r\n Installs the service" );
Console . WriteLine ( " uninstall:\r\n Uninstalls the service" );
Console . WriteLine ();
Console . WriteLine ( "It is possible to pass arguments to Duplicati.Server.exe, simply add them to the commandline:" );
Console . WriteLine ( " Duplicati.WindowsService.exe install --webservice-interface=loopback --log-retention=3M" );
Console . WriteLine ();
Console . WriteLine ( "To see supported options, run Duplicati.Server.exe:" );
Console . WriteLine ( " Duplicati.Server.exe help" );
Console . WriteLine ();
Console . WriteLine ( "To debug the WindowsService setup, add the --debug-service:" );
Console . WriteLine ( " Duplicati.WindowsService.exe install --debug-service" );
}
else if ( install || uninstall )
{
// Remove the install and uninstall flags if they are present
2018-02-11 19:54:05 +01:00
var commandline = Library . Utility . Utility . WrapAsCommandLine ( args . Where ( x => !( string . Equals ( "install" , x , StringComparison . OrdinalIgnoreCase ) || string . Equals ( "uninstall" , x , StringComparison . OrdinalIgnoreCase ))));
2016-09-23 14:11:53 +02:00
var selfexec = Assembly . GetExecutingAssembly (). Location ;
2019-05-01 21:30:22 +02:00
2016-09-23 14:11:53 +02:00
// --uninstall + --install = reinstall
if ( uninstall )
2019-05-01 21:30:22 +02:00
{
try
{
ServiceInstaller . DeleteService ( ServiceControl . SERVICE_NAME );
Console . WriteLine ( "Duplicati service delete succeeded." );
}
catch ( Exception ex )
{
Console . WriteLine ( "Duplicati service delete failed. Exception: {0}" , ex . Message );
}
}
2016-09-23 14:11:53 +02:00
if ( install )
2019-05-01 21:30:22 +02:00
{
//The fully qualified path to the service binary file. If the path contains a space, it must be quoted so that it is correctly interpreted. For example, "d:\my share\myservice.exe" should be specified as ""d:\my share\myservice.exe"".
//The path can also include arguments for an auto-start service. For example, "d:\myshare\myservice.exe arg1 arg2". These arguments are passed to the service entry point (typically the main function).
try
{
ServiceInstaller . InstallService ( ServiceControl . SERVICE_NAME , ServiceControl . DISPLAY_NAME , "\"" + selfexec + "\"" + " " + commandline );
Console . WriteLine ( "Duplicati service installation succeeded." );
}
catch ( Exception ex )
{
Console . WriteLine ( "Duplicati service installation failed. Exception: {0}" , ex . Message );
}
}
2016-09-23 14:11:53 +02:00
}
else
{
ServiceBase . Run ( new ServiceBase [] { new ServiceControl ( args ) });
}
2016-04-13 11:44:21 +02:00
}
}
}