WordPress Database Backup
A commandline tool for backing up your WordPress site to local folder, remote location using SFTP (SCP) and Amazon S3.
How it works
This is just a shell script that uses the combination of WP-CLI to backup your WordPress database, Linux SCP (Secure Copy) to move the backup to a remote destination of SSH, and s3cmd - a CLI tool to copy files to Amazon S3. The backup file will be gzipped automatically.
Just run the command in your terminal, the required dependencies will be installed.
wget -O - https://raw.github.com/tareq1988/wp-db-backup/master/setup.sh | bash
This is just as simple as executing a shell script, which is stored in
/usr/local/bin/wp-db-backup path. So you can invoke the script anywhere using just
-h --help [--path=<path>] Path to the WordPress installation. Required if the current directory is not a WordPress installation. [--type=<type>] Type of backup. Default: dir. Possible values include 'dir', 'scp', 's3'. [--backup-dir=<dir>] Path to your backup directory if --type=dir. [--ssh=<path>] Path to your remote directory if --type=scp. e.g. --ssh=user@host:/path/to/dir [--s3path=<path>] Path to your s3 directory if --type=s3. e.g. --s3path=path/to/dir. s3:// will be prepended automatically. [--filename=<name>] The file name for the backup file. The date name will be automatically appended. .e.g <filename>-<year-month-date>.sql.gz. Default is 'database'
Types of Backup:
- Local Backup.
--type=dir, this is the default one.
- Remote Backup using SFTP.
- Remote Backup to Amazon S3.
If you want to backup your database to the same machine, this is the default backup type and you don't need to pass the additional
By default the backup will be stored in
Backups folder in your home directory (
~/Backups). But you can override that with the
--backup-dir=/path/to/your/backup, this has to be the absolute path.
wp-db-backup --path=/var/www/example.com/htdocs --backup-dir=/home/user/dir
Remote Backup - SFTP
We are using the SCP command to push the backup file to a remote location accessible by your host machine.
wp-db-backup --path=/var/www/example.com/htdocs --type=scp --ssh=user@host:/path/to/directory
Remote Backup - Amazon S3
s3cmd - A very popular Amazon S3 client is being used for this type of backup. The setup script should automatically install the script for you if you're using debian based distributions. Otherwise you can install the tool manually.
After installation, please configure your S3 client using
s3cmd --configure and make sure you can upload files to your S3 bucket. Please take a look at these S3 policies to securely configure your S3 bucket.
s3path: Let's say your S3 bucket name is
my-backup and the backup directory is
sitename, the s3cmd compatible way of putting a file is:
s3cmd put filename.zip s3://my-backup/sitename/. So your backup command will be:
wp-db-backup --path=/var/www/example.com/htdocs --type=s3 --s3path=my-backup/sitename/
You don't need to put the full
s3:// will be automatically prepended for you.
Example cronjob for 3 types of backup.
0 0 * * * /usr/local/bin/wp-db-backup --path=/var/www/example.com/htdocs --backup-dir=/home/user/dir > /dev/null 2>&1 0 0 * * * /usr/local/bin/wp-db-backup --path=/var/www/example.com/htdocs --type=scp --ssh=user@host:/path/to/directory > /dev/null 2>&1 0 0 * * * /usr/local/bin/wp-db-backup --path=/var/www/example.com/htdocs --type=s3 --s3path=my-backup/sitename/ > /dev/null 2>&1
0 0 * * 0 /usr/local/bin/wp-db-backup --path=/var/www/example.com/htdocs --backup-dir=/home/user/dir > /dev/null 2>&1 0 0 * * 0 /usr/local/bin/wp-db-backup --path=/var/www/example.com/htdocs --type=scp --ssh=user@host:/path/to/directory > /dev/null 2>&1 0 0 * * 0 /usr/local/bin/wp-db-backup --path=/var/www/example.com/htdocs --type=s3 --s3path=my-backup/sitename/ > /dev/null 2>&1
This is an open-source project developed by Tareq Hasan. You are free to contribute to improve the project :)