KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: How to Programmatically Backup Your Database?
PRODUCT: 4D Backup | VERSION: 2003 | PLATFORM: Mac & Win
Published On: July 29, 2004

In order to backup your 4D database programmatically, you must first install the 4D Backup plug-in. This can be done by copying the Mac4DX or Win4DX (depending on which OS) folder adjacent to your structure file. This plug-in is included in the 4D package or can be downloaded from the 4D Product Downloads page

Once installed, you will need to run a backup. In 4D Single user, go to the User environment. Select Full Backup from the Plug-Ins menu. This will allow you to execute and save a Backup project that will later be referred to when executing your backup method. In 4D Server, select Full Backup from the Backup menu.

Now you can start utilizing the 4D Backup commands. You have three ways of performing a backup programmatically. The first and simplest way is to use the BK Full backup command. This command will execute a backup based on the parameters set in your backup project file.

The second way to backup is done by utilizing various backup projects. You can create multiple backup projects and later call them when executing a backup. In the example below, the command BK OPEN PROJECT is used to open a specific project based on the path you provide. Parameters from the selected project will then be used when backing up. There is also error handling being utilized in the method below.

` ----------------------------------------------------
` Method: Method: ExecuteBackup
` Description: Programmatically backup your database
` ----------------------------------------------------
C_INTEGER($Error)
C_TIME(vhDocRef)
C_TEXT(vProjectPath)

vProjectPath:=""
`Path to Backup Project

If (BK Begin full backup #0)
`This will begin the backup process
    ALERT("Impossible to start the backup.")
Else
    BK OPEN PROJECT (vProjectPath)
    $Error:=BK Get error number
    If ($Error#0)
      `If an error occurred
      ALERT("Error at the opening of the project: "+BK Get error text ($Error))
    Else
      If (BK Get state #3)
            `If the state is not "Ready to Copy"
           ALERT("The project is no longer valid. The database cannot be backed up.")
        Else
           If (BK Start copy =0)
            `This is the actual command to execute the backup
            Repeat
            Until (BK Get state #4)
`Now that the backup has begun, you must wait until complete
`If BK Get state returns 4, we know the backup is still in progress
            End if
          End if
      End if
      BK END BACKUP
`This command will close our backup process once finished
End if

The third way to execute a backup is using the BK FULL BACKUP WINDOW. This command will display the backup dialog, identical to what is seen when executing a Full Backup from the Plug-in menu in the User environment. Use this dialog to then finalize your settings and execute the backup.