Using a script to back up a database to DreamObjects

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

  • dumps a database in the format "yourusername_mm-dd-yyyy.sql" to the directory "/home/username/backup/”.
  • assumes that all the included databases run on the same database server and use the same database user.
  • uploads the database to your DreamObjects bucket
  • deletes the database 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 contain your 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
    [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. It makes sense to name it after the domain that uses the database. However, it will be deleted when running the final command in the script.
    • sqldbs — The name of your database.
    • opath — The directory you want to back up to. You can change this if you like.
    • mysqlhost — Your MySQL database hostname.
    • username — Your MySQL username.
    • password — Your MySQL password.
    In the boto-rsync line at the bottom, you must edit the following:
    • Your bucket's Access and Secret Keys
    • Specify the correct path to the backup script on your web server
    • Specify the location you wish to backup to within your DreamObjects bucket

    The script shows how to backup two databases at once that are under the same MySQL username. If you're only backing up a single database, just remove '' and 'yourdb2'.

  5. Add the following code to this file.
    domains=( )
    sqldbs=( yourdb1 yourdb2 )
    suffix=$(date +%m-%d-%Y)
    for (( i = 0; i < ${#domains[@]}; i++))
    	if [ -d $cpath ]
    		filler="just some action to prevent syntax error"
    		echo Creating $cpath
    		mkdir -p $cpath
    	mysqldump -c -h $mysqlhost --user $username --password=$password ${sqldbs[$i]} > ${cpath}/${sqldbs[$i]}_$suffix.sql
    #use boto-rsync to upload to DreamObjects boto-rsync -a ACCESS_KEY -s SECRET_KEY --endpoint /home/username/backup/*/ s3://dreamobjects-bucket/backups

    #delete backup from web server
    find $HOME/backup/* -type d -exec rm -rf {} \; 2> /dev/null
  6. The above code will backup your database to your DreamObjects bucket inside a folder named /backups.

Running the file

You can now run the file manually with the following command:

[server]$ sh ~/backup/

You can also create a cron job to run this automatically at specific times.

See also

External links

Did this article answer your questions?

Article last updated PST.