How To Create Backup Server With Digital Ocean?

Emna Fakhfakh
4 min readJan 2, 2024

Introduction:

Creating a backup server is crucial for protecting your data and ensuring continuity of your business. In this guide, we will show you how to set up a reliable backup solution using Digital Ocean’s cloud infrastructure. The process involves setting up a backup server on your own server first, and then on Digital Ocean. Let’s get started!

DigitalOcean Spaces is a great solution for backing up your data. Here are the steps you can follow to configure your backup script using s3cmd:

  1. Create Space object storage on digital Ocean :

2. Spaces access keys:

To connect with third-party clients or to access the Spaces API, you need to generate API keys from the DigitalOcean control panel under the API section. Once generated, keep your keys secure and use them with s3cmd or any other third-party client that supports the S3 API to interact with your Spaces.

3. Install and Configure s3cmd:

To interact with DigitalOcean Spaces, you need to install and configure s3cmd, a command-line tool. This tool enables you to manage your Spaces, including uploading and downloading files, creating buckets, and setting access permissions.

To install s3cmd, you first need to update your package list by running the following command:

sudo apt-get update

After updating your package list, you can install s3cmd by running the following command:

sudo apt-get install s3cmd

Once s3cmd is installed, you need to configure it for use with DigitalOcean Spaces. You can do this by running the following command and following the prompts:

s3cmd --configure

4. Create a Backup Script:

Next, you need to create a backup script that defines the backup parameters, creates a backup, and uploads it to Digital Ocean Spaces. Here’s an example of a backup script:

cd /usr/local/bin
nano ~/backup_script.sh
#!/bin/bash
#Define backup parameters
##The location and name of the backup file you want to create
backup_file="/path/to/your/backup.tar.gz"
### The directory you want to back up
source_directory="/path/to/directory"
# Create a backup
tar -czvf "$backup_file" "$source_directory"
# Upload to DigitalOcean Spaces
s3cmd put "$backup_file" s3://your-space-name/backup-folder/

This line #tar -czvf “$backup_file” “$source_directory” … uses the tar command to create a compressed archive of the specified source directory and save it to the specified backup file. The -czvf flags stand for "create," "compress," "verbose," and "file."

The tar command will create the compressed backup archive ("$backup_file") but leave the original files intact in "$source_directory". In this case we have a copie of backups files on our server.

Attention ! Using this approach may rapidly fill up your server’s storage space.

The last line uses the s3cmd command to upload the created backup file to a DigitalOcean Spaces bucket. You need to replace "s3://your-space-name/backup-folder/" with the actual URL of your DigitalOcean Spaces bucket and the desired backup folder path.

  • Make the Script Executable
chmod +x ~/backup_script.sh
  • Test script
~/backup_script.sh
  • Schedule Your Script

To schedule your backup script to run automatically at regular intervals, you can use `cron`. Here’s how you can do it:

Open the `crontab` editor by running the following command in your terminal:

crontab -e

In the editor, add a line like the following to run your script every 3 days at midnight:

 0 0 */3 * * /path/to/your/backup_script.sh

This will run your backup script every 3 days at midnight, using the absolute path to your script file.

Once you’ve added the line, save and exit the editor. Your backup script will now run automatically at the scheduled intervals.

5. The new script incremental server backup on Digital Ocean

An incremental backup only saves the data that has changed since the last backup (whether that last backup was a full backup or an incremental backup).

#!/bin/bash
# Define backup parameters
source_directory="/path/to/directory"
snapshot_file="/path/to/snapshotFile.file"
date_stamp=$(date +%Y%m%d_%H%M%S)
backup_file="NameOfBackupServer_$date_stamp.tar.gz"

# Stream the incremental backup directly to DigitalOcean Spaces
tar -czf - --listed-incremental="$snapshot_file" "$source_directory" | s3cmd put - s3://your-space-name/backup-folder/$backup_file

This script performs the following actions:

Creates a variable date_stamp to store the current date and time for the backup file name.

  • Uses the tar command with — as the file name to create a compressed archive and write it to standard output instead of a file.
  • Pipes the output of tar directly into s3cmd put with — as the source, which tells s3cmd to read from standard input.
  • The backup file is then uploaded directly to your specified DigitalOcean Space without being stored on your local filesystem.

Finally we have this results

Conclusion:

In summary, establishing a backup server with DigitalOcean demands careful planning and ongoing upkeep. Adherence to best practices and effective use of DigitalOcean’s services will safeguard your data, ensuring operational resilience. Continuously test your backups and update your procedures to maintain data security.

--

--