Using a script to backup your website and database to DreamObjects


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/
  • uploads the database to your DreamObjects bucket
  • zips your website directory in the format of to the directory /home/username/backup/
  • uploads the website .zip file to DreamObjects
  • deletes the database and website zip file on the web server to conserve space


The following information is required to complete the steps in this article:

Step 1 — Setting up your DreamObjects bucket

  1. View the following article to create a DreamObjects account, add a user, and create a bucket.
    DHO backup script
  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:

    DHO backup script
  3. Select the bucket you wish to upload objects to.
    DHO backup script
  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

In the following examples, username would be your Shell user and your website.

  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 Make sure to change to the site you're backing up.
    [server]$ touch ~/backup/
  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/username/backup/. 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.
    • — This is the name of your backup file in your /home/username/backup directory.
  5. Add the following code to this file. Make sure to update any code that's highlighted.

    domain=( ) sqldb=( your-db-name ) opath=$HOME/backup/ username=db-username password=db-user-password
    webdir=$HOME/ 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 s3 sync $opath s3://bucket-name/backups/ --exclude "$opath" #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 — Configure AWS CLI credentials

The script above uses AWS CLI to connect to DreamObjects. You must configure AWS CLI before running the script or it will not be able to connect. View the following article for further information:

Step 4 — Running the file

Option 1 — Running it manually

Run the file manually with the following command:

[server]$ sh ~/backup/

Option 2 — Running it from a cron job

You can also create a cron job to run this automatically at specific times. This would require you to create a file with the commands to run and then create the cron job in your panel.

Step 1 — Create a file to run the command

Create a new file named something like with the following code.

Make sure to change to the name of your backup file.

sh ~/backup/

Step 2 — Create the cron job in your panel

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/

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?