Backup WordPress on VPS

Backup Wordpress on VPS

Making regular backup WordPress of your site is very important for several reasons.

  • Protects You from Accidents. Sometimes, things go wrong — maybe you accidentally delete a page, change the wrong setting, or install a plugin that breaks your site. A backup lets you undo those mistakes easily.
  • Guards Against Hacking and Malware. Even secure websites can be hacked. If your site is ever infected with malware or defaced, a backup helps you restore a clean, working version without starting from scratch.
  • Server or Hosting Problem. Your hosting company’s server could crash, have technical issues, or lose your data. If you have your own backup, you’re not totally dependent on your hosting provider to get your site back online.
  • Plugin or Theme Updates Can Break Things. Sometimes updates cause conflicts and break parts of your site. If you backed up before updating, you can quickly roll back to the working version.
  • Saves Time and Stress. Instead of trying to fix major problems manually, you can restore your backup and have your site working again in minutes.
  • Helps with Moving or Migrating Your Site. Backups are also useful if you want to move your site to a new host or domain. It’s much easier to copy your site using a backup.

To make sure your WordPress site is safely backed up, you need to do two important things:

Backup your WordPress database. The database stores all of your site’s content, including posts, pages, comments, user information, settings, and plugin configurations. Exporting this database ensures that your site’s content and structure can be restored if needed.

Backup your WordPress site files. This includes copying the entire WordPress directory (commonly located in the public_html folder), which contains all your themes, plugins, media uploads, and core WordPress files. These files are necessary for the site to function properly and to preserve its appearance and functionality.

Backup your WordPress database.

To back up your WordPress database with mysqldump without typing the password every time, you can store your MySQL credentials in a hidden configuration file in your home directory..

  • Create/Edit the .my.cnf file in your home directory:
nano ~/.my.cnf
  • Add the following:
[client]
user=your_db_user
password=your_db_password
  • Secure the file:
chmod 600 ~/.my.cnf
  • Now, you can run mysqldump without entering the password:
mysqldump your_db_name > wordpress_backup.sql

Since the .my.cnf file stores credentials, MySQL reads them automatically. Works for mysqldump, mysql, and other MySQL tools.

Backup your WordPress site files.

Backing up your WordPress site files using SSH is a fast and powerful method — ideal if you have command-line access to your server (like with a VPS or cloud hosting).

  • Go to the WordPress directory

This is where all your site files are located:

cd /path/to/your/wordpress

Common locations:

  • /home/youruser/public_html
  • /var/www/html

Use ls to list files and confirm you’re in the right directory (should see folders like wp-content, wp-admin).

  • Create a compressed backup (tar + gzip)

Run this command to archive and compress everything:

tar -czvf wordpress_backup_2025_07_22.tar.gz .

Explanation:

  • tar = archive tool
  • -c = create
  • -z = compress with gzip
  • -v = verbose (shows progress)
  • -f = filename
  • . = current directory (everything in it)

This will create a file like wordpress_backup_2025_07_22.tar.gz.

For backup you need:

  • Backup your site wordpress folder
  • Backup your wrodpress database

Final Script for backup

#!/bin/bash
currentdt=$(date '+%Y%m%d%H%M%S')
dbfile="myblog-db-$currentdt.sql"
archfile="myblog-wp-$currentdt.tar.gz"
mysqldump --no-tablespaces myblogdb > $dbfile
tar -zcf $archfile $dbfile -C /var/www/ mybloghtmlpath
echo "Wordpress Blog backup completed."

Explanation:

currentdt=$(date '+%Y%m%d%H%M%S')
  • This gets the current date and time in the format YYYYMMDDHHMMSS
  • Example result: 20250722150330
  • It stores that value in a variable called currentdt, so each backup file has a unique timestamp.
mysqldump --no-tablespaces myblogdb > $dbfile
  • This creates a backup of the MySQL database called myblogdb
  • --no-tablespaces avoids errors on some MySQL versions (safe to use)
  • The SQL dump is saved into the file named in $dbfile (with timestamp)
tar -zcf $archfile $dbfile -C /var/www/ mybloghtmlpath
  • This creates a compressed archive (tar.gz) that includes:
    • The SQL dump file ($dbfile)
    • The entire WordPress site directory called mybloghtmlpath located inside /var/www/
  • -C /var/www/ tells tar to go to that folder first before looking for mybloghtmlpath

So this archive includes:

  • Your database (.sql file)
  • Your WordPress files (themes, plugins, uploads, etc.)

Conclusion

Backing up your WordPress site is one of the most important steps you can take to protect your hard work, content, and online presence. Whether it’s a simple manual backup of your files and database or an automated solution using plugins and scripts, having a reliable backup plan ensures that you can quickly recover from accidents, hacks, or server failures. Regular backups give you peace of mind and save you time and stress if anything goes wrong. So don’t wait—start backing up your WordPress site today and keep your website safe and secure for the future.