├── backup.sh ├── readme.md └── setup.sh /backup.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | WP_PATH=$(pwd) 4 | BACKUP_DIR=~/Backups 5 | FILE_NAME=database 6 | TYPE=dir 7 | SSH= 8 | S3_PATH= 9 | 10 | function usage() { 11 | echo "WordPress Database backup tool." 12 | echo "" 13 | echo -e " -h \t--help\n" 14 | 15 | echo " [--path=]" 16 | echo -e "\tPath to the WordPress installation. Required if the current directory is not a WordPress installation." 17 | echo "" 18 | 19 | echo " [--type=]" 20 | echo -e "\tType of backup. Default: dir. Possible values include 'dir', 'scp', 's3'." 21 | echo "" 22 | 23 | echo " [--backup-dir=]" 24 | echo -e "\tPath to your backup directory if --type=dir." 25 | echo "" 26 | 27 | echo " [--ssh=]" 28 | echo -e "\tPath to your remote directory if --type=scp. e.g. --ssh=user@host:/path/to/dir" 29 | echo "" 30 | 31 | echo " [--s3path=]" 32 | echo -e "\tPath to your s3 directory if --type=s3. e.g. --s3path=path/to/dir. s3:// will be prepended automatically." 33 | echo "" 34 | 35 | echo " [--filename=]" 36 | echo -e "\tThe file name for the backup file. The date name will be automatically appended." 37 | echo -e "\t.e.g -.sql.gz. Default is 'database'" 38 | echo "" 39 | } 40 | 41 | while [ "$1" != "" ]; do 42 | PARAM=`echo $1 | awk -F= '{print $1}'` 43 | VALUE=`echo $1 | awk -F= '{print $2}'` 44 | case $PARAM in 45 | -h | --help) 46 | usage 47 | exit 48 | ;; 49 | --path) 50 | WP_PATH=$VALUE 51 | ;; 52 | --type) 53 | TYPE=$VALUE 54 | ;; 55 | --ssh) 56 | SSH=$VALUE 57 | ;; 58 | --s3path) 59 | S3_PATH=$VALUE 60 | ;; 61 | --backup-dir) 62 | BACKUP_DIR=${VALUE%/*} # remove traling slash from value 63 | ;; 64 | --name) 65 | FILE_NAME=$VALUE 66 | ;; 67 | *) 68 | echo "ERROR: unknown parameter \"$PARAM\"" 69 | usage 70 | exit 1 71 | ;; 72 | esac 73 | shift 74 | done 75 | 76 | FULL_NAME=$FILE_NAME-$(date +%Y-%m-%d-%H%M%S).sql.gz 77 | 78 | if [ "$TYPE" == "dir" ]; then 79 | echo "Backing up database to directory..." 80 | 81 | # Create backup directory if not present 82 | if [ ! -d "$BACKUP_DIR" ]; then 83 | echo "No backup directory present, creating in: $BACKUP_DIR" 84 | mkdir $BACKUP_DIR 85 | fi 86 | 87 | wp db export - --path=$WP_PATH --allow-root | gzip > "$BACKUP_DIR/$FULL_NAME" 88 | echo "Backup complete and stored in: $BACKUP_DIR/$FULL_NAME" 89 | 90 | elif [ "$TYPE" == "scp" ]; then 91 | echo "Copying to remote SSH. Backing up..." 92 | 93 | if [[ -z "$SSH" ]]; then 94 | echo "Please provide your SFTP details. Example: --ssh=user@host:path" 95 | exit 1 96 | fi 97 | 98 | wp db export - --path=$WP_PATH --allow-root | gzip > "./$FULL_NAME" 99 | 100 | echo "Backup created, copying to remote host..." 101 | scp "./$FULL_NAME" "$SSH" 102 | 103 | if [ "$?" -eq "0" ]; 104 | then 105 | echo "Successfully copied to remote host." 106 | else 107 | echo "Copying backup to remote host failed." 108 | fi 109 | 110 | echo "Deleting local copy" 111 | rm "./$FULL_NAME" 112 | 113 | elif [ "$TYPE" == "s3" ]; then 114 | 115 | if [[ -z "$S3_PATH" ]]; then 116 | echo "Please provide your S3 path. Example: --s3path=path/to/dir. s3:// will be prepended automatically." 117 | exit 1 118 | fi 119 | 120 | echo "Copying to S3 destination" 121 | 122 | wp db export - --path=$WP_PATH --allow-root | gzip > "./$FULL_NAME" 123 | s3cmd put "./$FULL_NAME" "s3://$S3_PATH" 124 | 125 | echo "Deleting local copy" 126 | rm "./$FULL_NAME" 127 | else 128 | echo "Not a valid backup type." 129 | exit 1 130 | fi 131 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # WordPress Database Backup 2 | 3 | A commandline tool for backing up your WordPress site to local folder, remote location using SFTP (SCP) and Amazon S3. 4 | 5 | Quick Links: [Installation](#installation) | [Using](#using) | [Cron Job](#cron-job) | [Wiki](https://github.com/tareq1988/wp-db-backup/wiki) | [FAQ](https://github.com/tareq1988/wp-db-backup/wiki/FAQ) 6 | 7 | ## How it works 8 | 9 | This is just a shell script that uses the combination of [WP-CLI](https://wp-cli.org/) to backup your WordPress database, Linux [SCP](http://manpages.ubuntu.com/manpages/bionic/man1/scp.1.html) (Secure Copy) to move the backup to a remote destination of SSH, and [s3cmd](https://github.com/s3tools/s3cmd) - a CLI tool to copy files to Amazon S3. The backup file will be gzipped automatically. 10 | 11 | ## Installation 12 | 13 | Just run the command in your terminal, the required dependencies will be installed. 14 | 15 | **Dependencies:** 16 | 17 | * [WP-CLI](https://github.com/wp-cli/wp-cli) 18 | * [s3cmd](https://github.com/s3tools/s3cmd) 19 | 20 | ```bash 21 | wget -O - https://raw.github.com/tareq1988/wp-db-backup/master/setup.sh | bash 22 | ``` 23 | 24 | ## Using 25 | 26 | 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 `wp-db-backup` command. 27 | 28 | ~~~ 29 | wp-db-backup 30 | ~~~ 31 | 32 | **OPTIONS** 33 | 34 | -h --help 35 | 36 | [--path=] 37 | Path to the WordPress installation. Required if the current directory is not a WordPress installation. 38 | 39 | [--type=] 40 | Type of backup. Default: dir. Possible values include 'dir', 'scp', 's3'. 41 | 42 | [--backup-dir=] 43 | Path to your backup directory if --type=dir. 44 | 45 | [--ssh=] 46 | Path to your remote directory if --type=scp. e.g. --ssh=user@host:/path/to/dir 47 | 48 | [--s3path=] 49 | Path to your s3 directory if --type=s3. e.g. --s3path=path/to/dir. s3:// will be prepended automatically. 50 | 51 | [--filename=] 52 | The file name for the backup file. The date name will be automatically appended. 53 | .e.g -.sql.gz. Default is 'database' 54 | 55 | **Types of Backup:** 56 | 57 | 1. Local Backup. `--type=dir`, this is the default one. 58 | 2. Remote Backup using SFTP. `--type=scp` 59 | 3. Remote Backup to Amazon S3. `--type=s3` 60 | 61 | ### Local Backup 62 | 63 | **Type:** 64 | 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 `--type=dir` parameter. 65 | 66 | **Directory:** 67 | 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. 68 | 69 | ~~~ 70 | wp-db-backup --path=/var/www/example.com/htdocs --backup-dir=/home/user/dir 71 | ~~~ 72 | 73 | ### Remote Backup - SFTP 74 | 75 | We are using the [SCP](http://manpages.ubuntu.com/manpages/bionic/man1/scp.1.html) command to push the backup file to a remote location accessible by your host machine. 76 | 77 | ~~~ 78 | wp-db-backup --path=/var/www/example.com/htdocs --type=scp --ssh=user@host:/path/to/directory 79 | ~~~ 80 | 81 | ### Remote Backup - Amazon S3 82 | 83 | [s3cmd](https://github.com/s3tools/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. 84 | 85 | 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](https://github.com/tareq1988/wp-db-backup/wiki/S3-Policies) to securely configure your S3 bucket. 86 | 87 | **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: 88 | 89 | ~~~ 90 | wp-db-backup --path=/var/www/example.com/htdocs --type=s3 --s3path=my-backup/sitename/ 91 | ~~~ 92 | 93 | You don't need to put the full `s3://my-backup/sitename/` path, `s3://` will be automatically prepended for you. 94 | 95 | ## Cron Job 96 | 97 | Example cronjob for 3 types of backup. 98 | 99 | **Daily Backup:** 100 | 101 | ~~~ 102 | 0 0 * * * /usr/local/bin/wp-db-backup --path=/var/www/example.com/htdocs --backup-dir=/home/user/dir > /dev/null 2>&1 103 | 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 104 | 0 0 * * * /usr/local/bin/wp-db-backup --path=/var/www/example.com/htdocs --type=s3 --s3path=my-backup/sitename/ > /dev/null 2>&1 105 | ~~~ 106 | 107 | **Weekly Backup:** 108 | 109 | ~~~ 110 | 0 0 * * 0 /usr/local/bin/wp-db-backup --path=/var/www/example.com/htdocs --backup-dir=/home/user/dir > /dev/null 2>&1 111 | 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 112 | 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 113 | ~~~ 114 | 115 | ## Credits 116 | 117 | This is an open-source project developed by [Tareq Hasan](https://github.com/tareq1988). You are free to contribute to improve the project :) -------------------------------------------------------------------------------- /setup.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | BACKUP_DIR="$HOME/Backups" 4 | 5 | if [ ! -d "$DIRECTORY" ]; then 6 | echo "No backup directory present, creating in: $BACKUP_DIR" 7 | mkdir $BACKUP_DIR 8 | fi 9 | 10 | if [ ! -f /usr/local/bin/wp ]; then 11 | printf "\n" 12 | echo "Installing WP-CLI..." 13 | printf "\n" 14 | wget -qO /usr/local/bin/wp https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar 15 | 16 | # Executable permission 17 | chmod a+x /usr/local/bin/wp 18 | 19 | # Download auto completion 20 | wget -qO /etc/bash_completion.d/wp-completion.bash https://raw.githubusercontent.com/wp-cli/wp-cli/master/utils/wp-completion.bash 21 | 22 | echo "WP-CLI Installed!" 23 | fi 24 | 25 | if ! command -v s3cmd --version; then 26 | printf "\n" 27 | echo "Installing s3cmd..." 28 | printf "\n" 29 | sudo apt-get install -y s3cmd python-magic 30 | 31 | printf "\n" 32 | echo "s3cmd Installed!" 33 | echo "Now please configure using: s3cmd --configure" 34 | fi 35 | 36 | if [ ! -f /usr/local/bin/wp-db-backup ]; then 37 | printf "\n" 38 | echo "Installing Backup Script..." 39 | printf "\n" 40 | wget -qO /usr/local/bin/wp-db-backup https://raw.githubusercontent.com/tareq1988/wp-db-backup/master/backup.sh 41 | 42 | # Executable permission 43 | chmod a+x /usr/local/bin/wp-db-backup 44 | 45 | echo "Backup Script Installed!" 46 | fi 47 | 48 | echo "Finished Installation!" 49 | --------------------------------------------------------------------------------