Database Backup to remote NAS

Hello,
I’m trying to have SambaPOS to backup automatically to my NAS which is in the same network as the Database Server PC.

I’m getting the same error as in “[RESOLVED] Restore backup - access denied”:

This Error actually appears whenever i try to have the backup file anywhere but to an usb key.

By now i have followed some suggestion in other post:

Tried to map the network drive as suggested here: problems-with-the-network, the error didn’t disappear

and i tried to change the permission like here DATABASE backup problem, and it did work only locally “C:\SambaPOS Backup”

then

I found @QMcKay’s DB Backup button here:
General System Build questions

it seems a good walkaround the issue, i kinda understand how to set it up in SambaPOS and i also tried to write/modify a BAT file but the problem is that i don’t really know how to code properly a BAT file. If is possible can somebody give me an hint on the BAT file?

Also the NAS may be moved in another location. Usally i use a DDNS service to keep track of my IP when i have to reach my NAS using FTP.
.
would be possible to implement it as well directly in the BAT file or is better to map the network resource first?

i hope i explain everything clearly,

Kind regards
Claudio

Your NAS should have a static IP, or it should be given an IP reservation through the DHCP service so that it always gets the same IP. It won’t be reliable otherwise. The error message you are receiving is likely a result of this, since the host IP of the NAS has changed, and thus the Drive Mapping is invalid. It states: cannot find the path specified.

Once that is done, it doesn’t really matter much whether you refer to the NAS path via UNC or via Mapped Drive.

UNC:

\\NASWD3TB01\Private\BACKUPS\POS\DBBU\

or

\\192.168.0.200\Private\BACKUPS\POS\DBBU\

Mapped Y: drive to \\NASWD3TB01\Private share:

Y:\BACKUPS\POS\DBBU\

Here is the BAT file that I use to run DB Backup. You will need to make modifications to the parameters set near the top. It also assumes your system Short Date/Time Format is YYYY-MM-DD and HH:mm:ss. This is required to build a proper date/time-stamped file name, for example:

SambaPOS4_2015-01-31_144737.bak

BAT file (fire this with a Start Process Action):

@echo off
cls

::
:: Set Path Information
::
::set pathSQL="D:\Programs\Microsoft SQL Server\120\Tools\Binn\osql.exe"
set pathSQL="D:\Programs\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\sqlcmd.exe"
set pathCOMP="C:\Program Files\WinRAR\Rar.exe"
set SERVERINSTANCE=localhost\SQLEXPRESS
set DBNAME=SambaPOS4
set pathBU=D:\Programs\POS\DBBU\
set pathNAS=\\NASWD3TB01\Private\BACKUPS\POS\DBBU\

::
:: Go to Backup Location
::
D:
CD %pathBU%

::
:: Set LOGFILE, ERRMSG, DateTime, BackupName
::
set LOGFILE=DBbackup.txt
set ERRMSG=

set dateTimeDB=%date%_%time:~0,2%%time:~3,2%%time:~6,2%
:: replace space with 0 :: @CALL set variable=%%variable: =0%%
@CALL set dateTimeDB=%%dateTimeDB: =0%%
set BackupName=%DBNAME%_%dateTimeDB%.bak
set CompressedName=%DBNAME%_%dateTimeDB%.rar


::
:: Start Logging
::
set dateTime=%date%_%time:~0,2%.%time:~3,2%.%time:~6,2%.%time:~9,2%
:: replace space with 0 :: @CALL set variable=%%variable: =0%%
@CALL set dateTime=%%dateTime: =0%%

@echo. >> %LOGFILE% 2>&1
@echo ******************************************************* >> %LOGFILE% 2>&1
@echo -- BAT BEG --------------------- %dateTime% >> %LOGFILE% 2>&1


::
:: Check parameters to ensure they are Ok
::
IF "%SERVERINSTANCE%" == "" set ERRMSG=%ERRMSG% ERROR:SERVERINSTANCE is BLANK
IF "%DBNAME%" == "" set ERRMSG=%ERRMSG% ERROR:DBNAME is BLANK
IF "%BackupName%" == "" set ERRMSG=%ERRMSG% ERROR:BackupName is BLANK
IF NOT EXIST %pathSQL% set ERRMSG=%ERRMSG% ERROR:pathSQL is Invalid
IF NOT EXIST "%pathBU%" set ERRMSG=%ERRMSG% ERROR:pathBU is Invalid
IF NOT EXIST %pathCOMP% set ERRMSG=%ERRMSG% ERROR:pathCOMP is Invalid
IF NOT EXIST "%pathNAS%" set ERRMSG=%ERRMSG% ERROR:pathNAS is Invalid
set errorlevel=


::
:: If there is a problem with the given parameters, %ERRMSG% will contain a value, and we abandon the script
::
IF "%ERRMSG%" NEQ "" GOTO ERRORPROCESS


::
:: If parameters Ok, we continue...
::
@echo.
@echo DB Backup starting soon...
timeout 10
@echo.

::
:: Run BACKUP Commands
::

@echo -- DATABASE BACKUP BEG ------- %dateTime% >> %LOGFILE% 2>&1

@echo Backing up database [%DBNAME%] to %pathBU%%BackupName%
:: "C:\Program Files\Microsoft SQL Server\90\Tools\Binn\osql.exe" -E -Q "BACKUP DATABASE mydatabase TO DISK='C:\tmp\db.bak' WITH FORMAT"
@echo sqlcmd.exe -S %SERVERINSTANCE% -E -Q "BACKUP DATABASE [%DBNAME%] TO DISK='%pathBU%%BackupName%'" >> %LOGFILE% 2>&1 
%pathSQL% -S %SERVERINSTANCE% -E -Q "BACKUP DATABASE [%DBNAME%] TO DISK='%pathBU%%BackupName%'" >> %LOGFILE% 2>&1 

@set dateTime=%date%_%time:~0,2%.%time:~3,2%.%time:~6,2%.%time:~9,2%
@CALL set dateTime=%%dateTime: =0%%

@echo -- DATABASE BACKUP END ------- %dateTime% >> %LOGFILE% 2>&1


::
:: Run COMPRESS Commands
::

@echo -- COMPRESS BEG ------- %dateTime% >> %LOGFILE% 2>&1

@echo Compressing %BackupName%
@echo Compressing %BackupName% to %CompressedName% >> %LOGFILE% 2>&1 
::rar a -ep -idcpd -m5 archivename filestoarchive
%pathCOMP% a -ep -idcp -m5 %pathBU%%CompressedName% %pathBU%%BackupName% >> %LOGFILE% 2>&1

@echo Deleting %BackupName%
@echo Deleting %pathBU%%BackupName% >> %LOGFILE% 2>&1 
del %pathBU%%BackupName%

@set dateTime=%date%_%time:~0,2%.%time:~3,2%.%time:~6,2%.%time:~9,2%
@CALL set dateTime=%%dateTime: =0%%

@echo -- COMPRESS END ------- %dateTime% >> %LOGFILE% 2>&1


::
:: Run COPY Commands
::

@echo -- COPY BACKUP BEG ------- %dateTime% >> %LOGFILE% 2>&1

@echo Copying %CompressedName% to %pathNAS%
@echo Copying %pathBU%%CompressedName% to %pathNAS%%CompressedName% >> %LOGFILE% 2>&1 
copy %pathBU%%CompressedName% %pathNAS%%CompressedName% >> %LOGFILE% 2>&1

@set dateTime=%date%_%time:~0,2%.%time:~3,2%.%time:~6,2%.%time:~9,2%
@CALL set dateTime=%%dateTime: =0%%

@echo -- COPY BACKUP END ------- %dateTime% >> %LOGFILE% 2>&1



::
:: We are almost done. Test for Errors.
::
IF [%ERRMSG%] == [] GOTO finished


:ERRORPROCESS
@echo %ERRMSG% 
@echo %ERRMSG% >> %LOGFILE% 2>&1 
@set dateTime=%date%_%time:~0,2%.%time:~3,2%.%time:~6,2%.%time:~9,2%
:: replace space with 0 :: @CALL set variable=%%variable: =0%%
@CALL set dateTime=%%dateTime: =0%%
@echo -- BAT END --------------------- %dateTime% >> %LOGFILE% 2>&1
:: Open the LOGFILE
@notepad %LOGFILE%
goto endoffile


:finished
@set dateTime=%date%_%time:~0,2%.%time:~3,2%.%time:~6,2%.%time:~9,2%
:: replace space with 0 :: @CALL set variable=%%variable: =0%%
@CALL set dateTime=%%dateTime: =0%%
@echo -- BAT END --------------------- %dateTime% >> %LOGFILE% 2>&1


:endoffile
@echo.
::
:: We are done.  Uncomment the following lines if you wish.
::

::@notepad %LOGFILE%

@echo DB Backup Complete.
timeout 2
::pause
1 Like

Thank for the instruction!
The NAS now has a reserved IP trough the router’s DHCP table, much better!

I’m still bit stuck on the BAT file,

How should i set the “> set SERVERINSTANCE=localhost\SQLEXPRESS”?

by now here is what i did:

When running it i had back:“File System cannot find the drive specified”, i changed the “D” before “CD %pathBU%” to “C” and the message didn’t appear.

here is the header that i modify it:

@echo off
cls

::
:: Set Path Information
::
::set pathSQL=“C:\Program Files\Microsoft SQL Server\110\Tools\Binn\osql.exe”
set pathSQL=“C:\Program Files\Microsoft SQL Server\110\Tools\Binn\sqlcmd.exe”
set pathCOMP=“C:\Program Files\WinRAR\Rar.exe”
set SERVERINSTANCE=localhost\SQLEXPRESS
set DBNAME=SambaPOS4
set pathBU=C:\SambaPOS Backup
set pathNAS=Y:\SambaPOS Backups\

::
:: Go to Backup Location
::
C:
CD %pathBU%

::
:: Set LOGFILE, ERRMSG, DateTime, BackupName

Here the Log file:


– BAT BEG --------------------- Sun015/02/2015_04.44.56.77
– DATABASE BACKUP BEG ------- Sun015/02/2015_04.44.56.77
sqlcmd.exe -S localhost\SQLEXPRESS -E -Q “BACKUP DATABASE [SambaPOS4] TO DISK=‘C:\SambaPOS Backup\SambaPOS4_Sun015/02/2015_044456.bak’”
Sqlcmd: Error: Microsoft SQL Server Native Client 11.0 : SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF]. .
Sqlcmd: Error: Microsoft SQL Server Native Client 11.0 : Login timeout expired.
Sqlcmd: Error: Microsoft SQL Server Native Client 11.0 : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online…
– DATABASE BACKUP END ------- Sun015/02/2015_04.45.07.38
– COMPRESS BEG ------- Sun015/02/2015_04.45.07.38
Compressing SambaPOS4_Sun015/02/2015_044456.bak to SambaPOS4_Sun015/02/2015_044456.rar

Evaluation copy. Please register.

Cannot open Backup\SambaPOS4_Sun015\02\2015_044456.rar
The system cannot find the path specified.
Cannot open C:\SambaPOS
The system cannot find the file specified.
Cannot open Backup\SambaPOS4_Sun015\02\2015_044456.bak
The system cannot find the path specified.
Cannot create C:\SambaPOS.rar
Access is denied.
Program aborted
Deleting C:\SambaPOS Backup\SambaPOS4_Sun015/02/2015_044456.bak
– COMPRESS END ------- Sun015/02/2015_04.45.07.44
– COPY BACKUP BEG ------- Sun015/02/2015_04.45.07.44
Copying C:\SambaPOS Backup\SambaPOS4_Sun015/02/2015_044456.rar to Y:\SambaPOS Backups\SambaPOS4_Sun015/02/2015_044456.rar
The system cannot find the file specified.
– COPY BACKUP END ------- Sun015/02/2015_04.45.07.44
– BAT END --------------------- Sun015/02/2015_04.45.07.44

Set SERVERINSTANCE to be the same as the Data Source in SambaPOS …

Once you get past that point, you are going to run into another problem. Your Date/Time format contains forward-slash (/), which cannot be used in a file name. So you need to either change your Regional Clock Settings to not use Slash in the Short Date field (preferred), or you need to figure out another way to name your files (i.e. remove the %dateTimeDB% portion in the BAT file…

set BackupName=%DBNAME%_%dateTimeDB%.bak
set CompressedName=%DBNAME%_%dateTimeDB%.rar

The best thing to do with be to change your Short Date format to yyyy-MM-dd because the BAT file expects it to be in that format and parses it accordingly …

1 Like

You probably know about this but thought I would show it just in case. This is part of Database Backup module from Samba Market.

1 Like

Thanks!

@Jesse Thanks i didn’t know indeed…

i got to work the Database backup,

i found a workaround to my problem,

I use the Backup database action to backup the db to my local target folder,
then I use Windows task scheduler to run a bat to copy the files on the NAS. (which once a day suffice)

Thank you for your help!!

1 Like