Page 1 of 1

Automatically backing up the TM1 data directory

Posted: Tue Feb 05, 2013 12:27 am
by Martin Ryan
Backups have come up a few times but I've noticed we haven't put anything in this useful code forum about it. Here's how I've rolled mine...

1) I installed 7zip on the server that runs the backups.

2) I wrote the main bit of code in a batch file called "backupSlave.bat"

Code: Select all

:: Call this file with two arguments - the source folder (first argument) and the destination folder (second argument)
:: e.g. backupSlave.bat C:\TM1\MyServer\Data G:\Backups\MyServer
:: This file will create a timestamped folder within the destination folder, copy the source into the destination
:: run a zip then delete the copied folder.

::@echo off

:: Retrieve the two parameters which will be the directory to be backed up 
:: and the directory to back it up into respectively
set "srcFolder=%~1"
set "destnFolder=%~2"

if "%srcFolder%"=="" (
  set errorMsg=no srcFolder provided
  goto :exitwitherror
) else (
  echo srcFolder set to %srcFolder%
)
if "%destnFolder%"=="" (
  set errorMsg=no destnFolder provided
  goto :exitwitherror
) else (
  echo destnFolder set to %destnFolder%
)

set hh=%time:~-11,2%
set /a hh=%hh%+100
set hh=%hh:~1%
Set dateseed=%date:~10,4%%date:~7,2%%date:~4,2%-%hh%%time:~3,2%

set targetDir=%destnFolder%\%dateseed%

mkdir %targetDir%

:: copy the data.  /C switch tells it to carry out even if there are sharing violations (common with tm1s.log)
xcopy "%srcFolder%" "%targetDir%" /e /C

:: zip it up
"C:\Program Files\7-Zip\7z.exe" a -tzip ""%targetDir%".zip" "%targetDir%" -r

:: Remove the unzipped directory
RD /S /Q "%targetDir%"

:: Avoid error handler
goto :exitwithouterror

:exitwitherror
echo An error occurred - %errorMsg%

:exitwithouterror
3) I wrote separate batch files for each instance I'm backing up (but this could be skipped and folded directly into step 4).

Code: Select all

:: Calls the slave to backup the source folder (first argument) to the destination folder (second argument)
:: the slave will create a timestamped folder within the destination folder

E:\TM1\scripts\backupSlave.bat \\server\e$\TM1\MyServer G:\Backups\MyServer
4) I have a standalone instance called "backups" that does nothing but run a chored TI process that calls the batch file created in step 3 with this in the prolog
ExecuteCommand('E:\TM1\scripts\backupMaster.bat', 0);

Not sure if that's the best way, but it's been working for me for about a year now and has saved my bacon at least a dozen times.

Re: Automatically backing up the TM1 data directory

Posted: Thu Apr 25, 2013 11:41 am
by TM1KS
Hi Alan,
Thanks for the script.
I tried using it with only difference of the line where it uses 7zip to compress. Instead I used the code for WinRar.

Code: Select all

:: zip it up & remove the unzipped directory
"C:\Program Files (x86)\WinRAR\Rar.exe" m -r ""%targetDir%".RAR" "%targetDir%"
however, I don't see anything in the backup folder.
The process completes successfully.
I even tried changing the logging to DEBUG, but it does not show any error messages.
What could possibly be wrong?

Re: Automatically backing up the TM1 data directory

Posted: Mon Apr 29, 2013 7:09 am
by upali
Thanks for the code mate... I use this simple DOS command to clear old backup files, which runs through the Scheduler.

Code: Select all

@echo *** Started - %date% - %time% ***@echo on

forfiles /P "D:\TM1Database\App_backup" /S /M *.zip /D -90 /C "cmd /c del @path"

:EXIT
@echo *** Completed - %date% - %time% ***

Re: Automatically backing up the TM1 data directory

Posted: Wed May 01, 2013 11:32 pm
by Martin Ryan
upali,
That's useful, thanks for the addition.

TM1KS,

I haven't used WinRar before. 7zip is freeware and often used. It's a reliable tool.

If you want to stick with WinRar, you'll just have to use trial and error. Type things out in the command line till you find out what works, then insert that into the batch file.

BTW, while Alan is our most prolific poster, he's not our only poster. ;)

Re: Automatically backing up the TM1 data directory

Posted: Tue Jun 18, 2013 11:12 pm
by Craig Trevor
I use 7-zip as well. I the following code in the Epilog of a scheduled process:

Code: Select all

sExec = 'C:\Program Files\7-Zip\7z.exe';
sBackupDirectory = 'E:\TM1\TM1 Archive\';
sDataDirectory = 'E:\TM1\Server\Data\';

sFileName = 'Data.' | TIMST( NOW(), '\Y\m\d.\h\i', 1) | '.zip';
sArgs = ' a -tzip ';
sCommand = sExec | sArgs | '"' | sBackupDirectory | sFileName | '" "' | sDataDirectory |  '"';
EXECUTECOMMAND(sCommand,0);
This has worked flawlessly for more than 2 years now.

Just update the sBackupDirectory to your Archive location, and sDataDirectory to your TM1 data directory.

Re: Automatically backing up the TM1 data directory

Posted: Tue Jul 02, 2013 2:36 am
by Kyro
Thats the same code as on our blog - posted pretty much two years ago. :lol:
http://blog.tm1tutorials.com/2011/10/04 ... 1-backups/