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

Step 1 — Setting up your DreamObjects bucket

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

  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 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/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.
    • — 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 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 — Install aws-cli

aws-cli is a command line client that allows you to manage your DreamObjects data. Most servers at DreamHost have installed this by default, so there is nothing for you to install. However, if your server is running an older version of Ubuntu 14, you will need to install this manually. 

View the following article for instructions on how to check if you need to install this.

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/

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 with the following code.

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

sh ~/backup/

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/

If you installed AWS in a virtualenv

If your server is running Ubuntu 14, you must create a virtualenv and install AWS manually as detailed in step #3 above. If so, add the source line to your cron job to activate your virtualenv.

Make sure to change my-user-bucket to the name of your virtual environment

source ~/my-user-bucket/bin/activate
sh ~/backup/

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?