Getting started with PHP Backup Utility (PHPBU)

Background

PHPBU is a PHP framework used to create backups, move data to other servers or cloud services using a simple XML configuration file.

PHPBU requires PHP 7.0, although if you don’t have access to PHP 7 you can use PHPBU version 4.0.10. PHPBU requires the DOM, JSON and SPL extensions, normally enabled by default. Note, different backup sources may need their respective executable binary too i.e. mysqldump .

Get detailed information about all features and documentation from the PHPBU website.

View the changedlog and download previous versions.

Installing PHPBU

The easiest way to get PHPBU is by downloading the latest phar archive. The PHAR extension provides a way to put entire PHP applications into a single file. PHAR archives provide a method to distribute a complete PHP application in a single file and run it from that file without the need to extract it to disk. PHAR is kind of like a thumb drive for PHP applications.

To globally install the PHPBU PHAR:

$ wget https://phar.phpbu.de/phpbu.phar
$ chmod +x phpbu.phar
$ sudo mv phpbu.phar /usr/local/bin/phpbu
$ php phpbu.phar --version

You can also use the downloaded PHAR file directly:

 $ wget https://phar.phpbu.de/phpbu.phar
 $ php phpbu.phar --version

Configuration

To use PHPBU you need a XML configuration file. You can validate the config files using the PHPBU schema definition found at schema.phpbu.de. PHPBU normally expects a file name of phpbu.xml.

The following is a good starting point for backing up a website and MySQL database. The files are moved via FTP to a backup server.

<?xml version="1.0" encoding="UTF-8"?>
<phpbu xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="http://schema.phpbu.de/5.0/phpbu.xsd">
	   
	<!-- create a json logfile -->
	<logging>
		<log type="json" target="/srv/abort-retry-fail.com/backups/.nobackup/logfile.json"/>
	</logging>


	<backups>
		<!-- backup website files -->
		<backup name="abort-retry-fail_com_files">
			<source type="tar">
				<option name="path" value="/srv/abort-retry-fail.com/public/htdocs" />
				<!--<option name="IgnoreFailedRead" value="true" />-->
				<!--<option name="exclude" value="/srv/abort-retry-fail.com/public/htdocs/wp-content/" />-->
			</source>

			<!-- check the size of the created backup -->
			<check type="SizeMin" value="100M" />

			<!-- where should the backup be stored -->
			<target dirname="/srv/abort-retry-fail.com/backups/.nobackup/files"
			filename="abort-retry-fail_com-%Y%m%d-%H%i.tar"
			compress="bzip2"/>

			<!-- cleanup the backup location -->
			<cleanup type="Capacity">
				<option name="size" value="1200M"/>
			</cleanup>

			<!-- sync sftp -->
			<sync type="sftp">
				<option name="host" value="8.8.1.1"/>
				<option name="user" value="ftusername"/>
				<option name="password" value="ftpassword"/>
				<option name="path" value="/home/web1/PHPBU_BKs/abort-retry-fail.com"/>
			</sync>	

			<!--
			<sync type="rsync">
				<option name="path" value="/home/web1/PHPBU_BKs/abort-retry-fail.com"/>
				<option name="dirsync" value="true"/>
				<option name="host" value="8.8.1.1"/>
				<option name="user" value="rsyncuser"/>
				<option name="password" value="rsyncpassword"/>
				<option name="delete" value="true" />
			</sync>	
			-->	

		</backup>

		<!-- backup database files -->
		<backup name="abort-retry-fail_com_database">

			<!-- source -->
			<source type="mysqldump">
				<option name="databases" value="databasename"/>
				<option name="user" value="databaseuser"/>
				<option name="password" value="databasepassword"/>
			</source>

			<!-- where should the backup be stored -->
			<target dirname="/srv/abort-retry-fail.com/backups/.nobackup/mysql"
			filename="abort-retry-fail_com-%Y%m%d-%H%i.sql"
			compress="bzip2"/>

			<!-- check the size of the created backup -->
			<check type="SizeMin" value="100K"/>

			<!-- cleanup the backup location -->
			<cleanup type="Capacity">
				<option name="size" value="4M"/>
			</cleanup>

			<!-- sync sftp -->
			<sync type="sftp">
				<option name="host" value="8.8.1.1"/>
				<option name="user" value="ftusername"/>
				<option name="password" value="ftpassword"/>
				<option name="path" value="/home/web1/PHPBU_BKs/abort-retry-fail.com"/>
			</sync>	 	  	  
		</backup>	
	</backups>
</phpbu>

Create the backup

Test the backup by executing from the command line.

$ sudo phpbu --verbose --configuration=phpconfig.xml

If the PHAR and XML file are in the same path you can execute like this:

$ php phpbu.phar --verbose

Backup CRON

Scheduling of backups can be done with a cron. To execute backups at 11:55 use:

55 11 * * * phpbu --configuration=/srv/abort-retry-fail.com/backups/phpbuconfig.xml

Make sure you test the job from the command line first. Successful output example:

PHPBU Backup Execute Output
PHPBU Backup Output

You May Also Like