SQL Server
SQL Server
Synchronize an SFTP Directory Tree to Local
See more SFTP Examples
Demonstrates the Chilkat SFtp.SyncTreeDownload method, which synchronizes a remote directory tree to a local directory. The arguments are the remote root, the local root, the sync mode, and whether to recurse into subdirectories.
Note: This example uses relative local paths, which are resolved against the application's current working directory. Absolute local paths may also be used. Supply the paths appropriate to your own environment.
Background: The mirror-image of
SyncTreeUpload, used to pull a remote tree down — fetching logs, backing up a server directory, or refreshing a local copy of published content. The same mode numbers apply, now judged from the download direction: only-missing, only-newer-on-server, differing-size, and so on. Missing local directories are created as needed, and GetSyncedFiles reports both the files and the directories that resulted.Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Demonstrates the SFtp.SyncTreeDownload method, which synchronizes a remote directory tree to a
-- local directory. The 1st argument is the remote root, the 2nd is the local root, the 3rd is
-- the sync mode, and the 4th selects recursion into subdirectories.
DECLARE @sftp int
EXEC @hr = sp_OACreate 'Chilkat.SFtp', @sftp OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Connect, authenticate, and initialize the SFTP subsystem.
DECLARE @port int
SELECT @port = 22
EXEC sp_OAMethod @sftp, 'Connect', @success OUT, 'sftp.example.com', @port
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @sftp
RETURN
END
-- Normally you would not hard-code the password in source. You should instead obtain it
-- from an interactive prompt, environment variable, or a secrets vault.
DECLARE @password nvarchar(4000)
SELECT @password = 'mySshPassword'
EXEC sp_OAMethod @sftp, 'AuthenticatePw', @success OUT, 'mySshLogin', @password
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @sftp
RETURN
END
EXEC sp_OAMethod @sftp, 'InitializeSftp', @success OUT
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @sftp
RETURN
END
-- Sync mode 5 downloads files that are missing locally or differ in size, plus files that are
-- newer on the server. Other modes:
-- 0 = all files
-- 1 = files not existing locally
-- 2 = files newer remotely or missing locally
-- 3 = only files newer remotely (do not download missing)
-- 4 = files missing locally or differing in size
-- 5 = mode 4 plus files newer remotely
DECLARE @mode int
SELECT @mode = 5
DECLARE @recurse int
SELECT @recurse = 1
EXEC sp_OAMethod @sftp, 'SyncTreeDownload', @success OUT, '/var/www/html', 'qa_output/website', @mode, @recurse
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @sftp
RETURN
END
PRINT 'Directory tree downloaded.'
EXEC sp_OAMethod @sftp, 'Disconnect', NULL
EXEC @hr = sp_OADestroy @sftp
END
GO