Friday, June 20, 2014

How to Configure Remote Backups Using Bacula in an Ubuntu 12.04 VPS

Client Installation

No local backups will be stored on the remote client machine, so not all of the bacula components need to be installed.
Install the the bacula-fd (file-daemon) and the bconsole (bacula console) on this machine with apt-get using the bacula-client metapackage:
sudo apt-get update
sudo apt-get install bacula-client
The necessary components are now installed and ready to be configured.

Client Machine Configuration

The configuration of the client environment is relatively straightforward. We will only be editing the bacula file daemon configuration file. Open the file with root privileges with the following command:
sudo nano /etc/bacula/bacula-fd.conf
We need to change a few items and save some information that we will need for our server configuration. Begin by finding the Director section.
The bacula director is located on the backup VPS. Change the "Name" parameter to the hostname of your backup server followed by "-dir".
You also need to copy the password that bacula generated for your client file daemon to some place that you'll have available when you are configuring your backup server settings:
Director {
  Name = BackupServer-dir
  Password = "u2LK-yBrQzfiEsc6NWftHEhymmdPWsklN"  # Copy this password for later reference!
}
Next, we need to adjust one parameter in the FileDaemon section. We will change the "FDAddress" parameter to match the IP address or domain name of our client machine. The "Name" parameter should already be populated correctly with the client file daemon name:
FileDaemon {                          # this is me
  Name = ClientMachine-fd
  FDport = 9102                  # where we listen for the director
  WorkingDirectory = /var/lib/bacula
  Pid Directory = /var/run/bacula
  Maximum Concurrent Jobs = 20
  FDAddress = ClientMachine.DomainName.com
}
We also need to configure this daemon to pass its log messages to the backup cloud server. Find the Messages section and change the "director" parameter to match your backup cloud server's name.
Messages {
  Name = Standard
  director =  BackupServer-dir = all, !skipped, !restored
}
Save the file and exit.
Check that your configuration file has the correct syntax with the following command:
sudo bacula-fd /etc/bacula/bacula-fd.conf
If the command returns no output, the configuration file has valid syntax. Restart the file daemon to use the new settings:
sudo service bacula-fd restart
The client machine is now correctly configured.
In this example, we would like to restore to a folder on this same machine. Create the file structure and lock down the permissions and ownership for security with the following commands:
sudo mkdir -p /bacula/restore
sudo chown -R bacula:bacula /bacula
sudo chmod -R 700 /bacula
The client machine is now configured correctly. Next, we will configure the backup cloud server to pull the file data from the client.

Backup Server Configuration

Log into the backup cloud server to complete this stage of the configuration.
The bulk of the configuration is actually done on the backup server. That is because the bacula "director" manages all other bacula processes and must be able to communicate correctly with a number of different components.
To start, open the "bacula-dir.conf" file with administrator privileges:
sudo nano /etc/bacula/bacula-dir.conf

Job Configuration

Begin by finding the Job Section. The current configuration is named "BackupClient1" and is used for the backup server's local backup. We need to change the name to reflect this:
Job {
  Name = "LocalBackup"
  JobDefs = "DefaultJob"
}
Now that we have identified the first job as backing up on the local machine, we want to create a similar job for backup up our remote client. To do this, copy and paste the job definition below the one you just modified.
Change the name to reflect the fact that this is a remote backup scenario. The "Client" parameter identifies our remote client file daemon as the target for our backup. The Pool parameter allows bacula to store its remote backups separate from our local backups. We will define the pool we're referencing later in the file:
Job {
  Name = "RemoteBackup"
  JobDefs = "DefaultJob"
  Client = ClientMachine-fd
  Pool = RemoteFile
}
Next, define a place for the remote backups to restore. We will use the directory that we created on the client machine to restore remote backups.
Find the "RestoreFiles" job definition. Copy the current entry and paste it below. We will then modify some entries to label it accurately and work with client machine:
Job {
  Name = "RestoreRemote"
  Type = Restore
  Client=ClientMachine-fd
  FileSet="Full Set"
  Storage = File     
  Pool = Default
  Messages = Standard
  Where = /bacula/restore
}

Client Configuration

Find the Client definition. We will change the "Address" parameter to reflect our actual backup cloud server IP address instead of using localhost. The password should already be set correctly for the local machine.
Client {
  Name = BackupServer-fd
  Address = BackupServer.DomainName.com
  FDPort = 9102
  Catalog = MyCatalog
  Password = "CRQF7PW-mJumFtENX2lqGvJ6gixPTyRQp"          # password for Local FileDaemon
  File Retention = 30 days            # 30 days
  Job Retention = 6 months            # six months
  AutoPrune = yes                     # Prune expired Jobs/Files
}
The next step is to actually define the client machine that we've been referencing in our configuration. Copy the Client entry we just modified paste it below the current definition. This new definition will be for the remote machine that we are backing up.
Match the name to your client machine's hostname followed by "-fd". The "Address" line needs to match the client machine's IP address or domain name as well.
Finally, this is where you enter the password that you copied from the remote client's file daemon configuration file. Make sure that you modify this password value, or else bacula will not function correctly.
Client {
  Name = ClientMachine-fd
  Address = ClientMachine.DomainName.com
  FDPort = 9102 
  Catalog = MyCatalog
  Password = "u2LK-yBrQzfiEsc6NWftHEhymmdPWsklN"          # password for Remote FileDaemon
  File Retention = 30 days            # 30 days
  Job Retention = 6 months            # six months
  AutoPrune = yes                     # Prune expired Jobs/Files
}

Storage Configuration

Next, change the "Address" parameter in the Storage section with the IP address or domain name of the backup VPS. Once again, the password should already be correct here:
Storage {
  Name = File
# Do not use "localhost" here   
  Address = BackupServer.DomainName.com                # N.B. Use a fully qualified name here
  SDPort = 9103
  Password = "097dnj3jw1Yynpz2AC38luKjy5QTnGoxS"
  Device = FileStorage
  Media Type = File
}

Pool Configuration

Find the Pool definitions section. We will first add a parameter to the "File" pool definition. Add the "Label Format" parameter to the definition and choose a prefix to name local file backups. For this guide, local backups will have "Local-" as a prefix.
Pool {
  Name = File
  Pool Type = Backup
  Recycle = yes                       # Bacula can automatically recycle Volumes
  Label Format = Local-
  AutoPrune = yes                     # Prune expired volumes
  Volume Retention = 365 days         # one year
  Maximum Volume Bytes = 50G          # Limit Volume size to something reasonable
  Maximum Volumes = 100               # Limit number of Volumes in Pool
}
Next, we need to copy the section we just modified and paste it below the current entry. This will be set up for remote backup storage.
Change the name of the new pool to reflect its job of storing remote backups. Also, change the prefix by adjusting the "Label Format" parameter to be "Remote-"
Pool { 
  Name = RemoteFile
  Pool Type = Backup
  Recycle = yes                       # Bacula can automatically recycle Volumes
  Label Format = Remote-
  AutoPrune = yes                     # Prune expired volumes
  Volume Retention = 365 days         # one year
  Maximum Volume Bytes = 50G          # Limit Volume size to something reasonable
  Maximum Volumes = 100               # Limit number of Volumes in Pool
}
Save and close the file.

Editing bacula-sd.conf

Next, open the "bacula-sd.conf" file with root privileges:
sudo nano /etc/bacula/bacula-sd.conf
Change the "SDAddress" parameter to reflect the backup server's IP address or domain name:
Storage {                             # definition of myself
  Name = BackupServer-sd
  SDPort = 9103                  # Director's port
  WorkingDirectory = "/var/lib/bacula"
  Pid Directory = "/var/run/bacula"
  Maximum Concurrent Jobs = 20
  SDAddress = BackupServer.DomainName.com
}
Save and close the file.

Checking the Configuration and Restarting Services

Check the configuration with the following commands:
sudo bacula-dir /etc/bacula/bacula-dir.conf
sudo bacula-sd /etc/bacula/bacula-sd.conf
If no output is returned, the configuration files have valid syntax. If this is the case, restart the daemons to use the new settings:
sudo service bacula-director restart
sudo service bacula-sd restart

Testing Remote Backups

Log into the bacula console to test the backup functionality.
sudo bconsole
Test that the bacula director can connect to the remote machine by typing the following:
status
Status available for:
     1: Director
     2: Storage
     3: Client
     4: All
Select daemon type for status (1-4): 
Choose #3 to check on the client connection and then select the remote machine:
3: Client
2: ClientMachine-fd
It should return a summary with some statistics, confirming that we can connect to the remote file daemon.
Run a test backup of the remote system by typing the following command:
run
Automatically selected Catalog: MyCatalog
Using Catalog "MyCatalog"
A job name must be specified.
The defined Job resources are:
     1: LocalBackup
     2: RemoteBackup
     3: BackupCatalog
     4: RestoreFiles
     5: RestoreRemote
Select Job resource (1-5): 
Select the "RemoteBackup" option to run a backup of the remote machine. Type "yes" to begin the backup:
2: RemoteBackup
The director will send the backup task to the remote file daemon and which will pass its information to the backup server's storage daemon. You can check the status of the job using the "status" command as we did above. You should also check the messages using the "messages" command.
messages
If you continue to check messages, eventually you will receive a summary of the backup operation. It should contain the line "Termination: Backup OK" if everything went as expected.

Testing Remote Restore

Now, test the restore functionality:
restore all
Choose the "Select the most recent backup for a client" option. Select the remote client that we have just backed up:
5: Select the most recent backup for a client
2: ClientMachine-fd
You will be dropped into a file tree where you are able to select the files you would like to restore with the "mark" and "unmark" commands.
We have chosen to restore everything, so we can just type "done" to move on. Select the job that we defined for remote restoration and type "yes" to run the restoration:
done
2: RestoreRemote
Again, you can check the restoration with the "status" and "messages" commands. You should eventually get a summary in the messages that contains the line "Termination: Restore OK". This means the restoration was successful. Type "exit" to leave the bacula console.
exit

Checking the Filesystem

We can check that our remote backup file has the correct file format with the following command:
sudo ls /bacula/backup
LocalBackup   Remote-0002
As you can see, our backup file for the remote system has adapted the naming conventions we supplied. The local backup is not named according to our convention because it is from before our changes.
If we log into our remote client machine, we can check our restore with the following line:
sudo ls /bacula/restore
bin   dev  home        lib    media  opt run   selinux  sys  var
boot  etc  initrd.img  lost+found  mnt   root sbin  srv      usr  vmlinuz
As you can see, we have restored the filesystem to this folder correctly.

0 comments: