Using a script to backup your website and database to DreamObjects

Overview

Using a simple shell script, you can automate website and database backups to DreamObjects. This script does the following:

  • dumps a database in the format "mm-dd-yyyy_db-name_.sql" to the directory "/home/username/backup/example.com”
  • uploads the database to your DreamObjects bucket
  • zips your website directory in the format of "mm-dd-yyyy_example.com.zip" to the directory "/home/username/backup/example.com”
  • uploads the website .zip file to DreamObjects
  • deletes the database and website zip file on the web server to conserve space

Step 1 — Setting up your DreamObjects bucket

  1. View the following article to create a DreamObjects account, add a user, and create a bucket.
    01_DHO_Panel_Upload.fw.png
  2. On the DreamObjects page, locate the User that contains the bucket you wish to work with, then click the View Objects button.

    The available buckets appear in the following window:

    02_DHO_Panel_Upload.fw.png
  3. Select the bucket you wish to upload objects to.
  4. Click the 'Add Folder' link and add a new folder called 'backups'. This folder will be used to upload your website and database backups.

Step 2 — Creating the backup script

  1. Log into your server via SSH.
  2. Create a new directory named 'backup' in your user's home directory.
    [server]$ mkdir ~/backup
  3. In this directory, create a file named example.com-backup.sh. Make sure to change example.com to the site you're backing up.
    [server]$ touch ~/backup/example.com-backup.sh
  4. In the script below, you must fill in the first six variables that are highlighted:
    • domains — The script uses this field to create a directory under $HOME/backups/. This is the directory that will store your database and website files, so make sure it's named the same as your domain name.
    • sqldbs — The name of your database.
    • opath — The directory you want to back up to.
    • mysqlhost — Your MySQL database hostname.
    • username — Your MySQL username.
    • password — Your MySQL password.
    • webdir — This is the path to your web directory. Change the domain name to your site.
    • bucket-name — The name of the DreamObjects bucket you're backing up to.
    • example.com-backup.sh — This is the name of your backup file in your $HOME/backup directory.
  5. Add the following code to this file. Make sure to update any code that's bold.
    domain=( example.com )
    sqldb=( your-db-name )
    opath=$HOME/backup/
    mysqlhost=mysql.example.com
    username=db-username
    password=db-user-password
    webdir=$HOME/example.com today=$(date +%m-%d-%Y) for (( i = 0; i < ${#domain[@]}; i++)) do cpath=$opath${domain[$i]} if [ -d $cpath ] then filler="just some action to prevent syntax error" else echo Creating $cpath mkdir -p $cpath fi mysqldump -c -h $mysqlhost --user $username --password=$password ${sqldb[$i]} > ${cpath}/"$today"_${sqldb[$i]}.sql zip -r ${cpath}/"$today"_"$domain".zip $webdir done #use aws-cli to upload to DreamObjects aws --endpoint-url https://objects-us-east-1.dream.io s3 sync $opath s3://bucket-name/backups/ --exclude "$opath"example.com-backup.sh #delete backup from web server find $HOME/backup/* -type d -exec rm -rf {} \; 2> /dev/null

The above code backs up your database and website files to your DreamObjects bucket inside a folder named /backups.

Step 3 — Install aws-cli

aws-cli is a command line client that allows you to manage your DreamObjects data. To install, you'll need to create a Python virtual environment. In that, you can install aws-cli.

View the following article for instructions on how to install and configure aws-cli for your bucket.

Step 4 — Running the file

Running it manually

After activating your virtual environment, you can run the file manually with the following command:

(my-user-bucket) sh ~/backup/example.com-backup.sh

Running it with a cron job

You can also create a cron job to run this automatically at specific times. To do this, create a new file named something like 'websitebackup.sh' with the following code.

Make sure to change 'my-user-bucket' to the name of your virtual environment, and example.com-backup.sh to the name of your backup file.

#!/bin/bash
source ~/my-user-bucket/bin/activate
sh ~/backup/example.com-backup.sh

View the How do I create a cron job? article to create your cron job. In the text box, enter the following command (change the username to your Shell user).

/bin/bash /home/username/websitebackup.sh

See also

Did this article answer your questions?

Article last updated PST.