Backing up your WordPress website is a crucial maintenance task that ensures the safety of your data against data loss, hacking, server failures, and other unforeseen issues. While plugins are a popular solution for automating backups, they can often bloat your website and slow down your server. This guide will show you how to backup your WordPress website programmatically without a plugin, giving you more control and potentially increasing the efficiency of your backup processes.
Benefits of Programmatically Backing Up WordPress
- Control: Complete control over what is backed up and when.
- Performance: Potentially faster and less resource-intensive than plugin-based backups.
- Customization: Ability to customize the backup process to suit specific needs.
- Automation: Easy integration with server cron jobs for scheduling.
Step-by-Step Guide to Programmatically Backing Up Your WordPress Site
Step 1: Backup the Database
A complete backup of your WordPress site includes both the files and the database. Start by backing up the database, which contains your posts, pages, comments, and configurations.
PHP Code to Backup Database:
Here’s how you can backup your WordPress database using PHP:
/*
* Snippet: How to Backup Your WordPress Website Programmatically Without a Plugin – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1163
* Tested with WooCommerce 10.7.0
* "Programmatically backup WordPress database"
*/
function wcsuccess_backup_database() {
include_once('wp-config.php');
$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$tables = array();
$sql = "SHOW TABLES";
$result = $conn->query($sql);
while ($row = $result->fetch_row()) {
$tables[] = $row[0];
}
$return = '';
foreach ($tables as $table) {
$result = $conn->query("SELECT * FROM " . $table);
$num_fields = $result->field_count;
$return .= 'DROP TABLE IF EXISTS ' . $table . ';';
$row2 = $conn->query('SHOW CREATE TABLE ' . $table)->fetch_row();
$return .= "\n\n" . $row2[1] . ";\n\n";
while ($row = $result->fetch_row()) {
$return .= 'INSERT INTO ' . $table . ' VALUES(';
for ($i = 0; $i < $num_fields; $i++) {
$row[$i] = addslashes($row[$i]);
$row[$i] = preg_replace("/\n/", "\\n", $row[$i]);
if (isset($row[$i])) {
$return .= '"' . $row[$i] . '"';
} else {
$return .= '""';
}
if ($i < ($num_fields - 1)) $return .= ',';
}
$return .= ");\n";
}
$return .= "\n\n\n";
}
$backup_file = 'db-backup-' . time() . '-' . (md5(implode(',', $tables))) . '.sql';
$handle = fopen($backup_file, 'w+');
fwrite($handle, $return);
fclose($handle);
echo "Database backup successfully saved as " . $backup_file;
}Step 2: Backup Your Files
Backing up your WordPress files involves saving copies of all themes, plugins, uploads, and scripts.
PHP Code to Backup Files:
/*
* Snippet: How to Backup Your WordPress Website Programmatically Without a Plugin – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1163
* Tested with WooCommerce 10.7.0
* "Programmatically backup WordPress files"
*/
function wcsuccess_backup_files() {
$rootPath = realpath('/path/to/your/wordpress/root');
$zip = new ZipArchive();
$zipFile = '/path/to/save/backup-' . date("Y-m-d-H-i-s") . '.zip';
$zip->open($zipFile, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($rootPath), RecursiveIteratorIterator::LEAVES_ONLY);
foreach ($files as $name => $file) {
if (!$file->isDir()) {
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
$zip->addFile($filePath, $relativePath);
}
}
$zip->close();
echo "Files backup successfully saved as " . $zipFile;
}Step 3: Automate the Backup Process
To ensure regular backups, automate the process using a cron job. This can be set up through your hosting control panel or by accessing the server command line.
Setting up a Cron Job:
Here’s an example cron command to run the backup functions daily:
bash 0 2 * * * /usr/bin/php /path/to/your/script.php
This cron job will trigger the backup script every day at 2 AM.
Testing Your Backup System
- Restore Test: Periodically test restoring your site from a backup to ensure the backups are functional.
- Security Test: Make sure your backup files are stored securely and are not accessible publicly.
- Verification Test: Regularly verify that the backups include all necessary files and complete database dumps.
Conclusion
By implementing a programmatic backup solution for your WordPress website, you gain control over the backup process, ensuring your site’s data is safely stored and can be restored in any emergency. Regular updates to your backup scripts will keep them secure and functional as your site evolves.
Further Steps
Consider encrypting your backup files for added security, especially if they are stored off-site or in the cloud. Also, regularly review your backup and restore procedures to ensure they meet current technological standards and business requirements.
I have been working with WordPress and WooCommerce since 2012 and have developed a deep knowledge of the content management system. Since 2012, I have developed several plugins and designed dozens of websites utilising different frameworks, CMS’s and programming languages. I am proficient in PHP, Python, Java, C, C++, R and JavaScript with limited experience in Go, Kotlin and Swift.
Educationally, I have a Master’s degree in cyber security a Bachelor’s (Hons, First Class) in Applied Research and a Graduate Certificate in Data Science. I’m currently undertaking PhD studies investigating IoT cybersecurity. I recently graduated with First Class Honours and Masters of Information Technology, receiving the Executive Dean’s Award for studies undertaken in the 2021 and 2022 academic years. I have worked in the information technology industry for the past 11 years primarily as a software/web developer specific to design, optimisation, network management and security. My research interests are in the areas of Internet of Things (IoT), 5G and Beyond Networks, information security for wireless networks and software development.
Stay In Touch

