Category: Ops

  • Back up your server to Backblaze B2 with Duplicity

    Back up your server to Backblaze B2 with Duplicity

    Amazon S3 has been around for more than ten years now and I have been happily using it for offsite backups of my servers for a long time. Backblaze’s cloud backup service has been around for about the same length of time and I have been happily using it for offsite backups of my laptop, also for a long time.

    In September 2015, Backblaze launched a new product, B2 Cloud Storage, and while S3 standard pricing is pretty cheap (Glacier is even cheaper) B2 claims to be “the lowest cost high performance cloud storage in the world”. The first 10 GB of storage is free as is the first 1 GB of daily downloads. For my small server backup requirements this sounds perfect.

    My backup tool of choice is duplicity, a command line backup tool that supports encrypted archives and a whole load of different storage services, including S3 and B2. It was a simple matter to create a new bucket on B2 and update my backup script to send the data to Backblaze instead of Amazon.

    Here is a simple backup script that uses duplicity to keep one month’s worth of backups. In this example we dump a few MySQL databases but it could easily be expanded to back up any other data you wish.

    #!/bin/bash
    
    ########################################################################
    # A b2 backup script
    # Uses duplicity (http://duplicity.nongnu.org/)
    #
    # Run this daily and keep 1 month's worth of backups
    ########################################################################
    
    # b2 variables
    B2_ACCOUNT_ID=account_id
    B2_APPLICATION_KEY=application_key
    BUCKET=my-bucket-name
    
    # GPG
    ENCRYPT_KEY=gpg_key_id
    export PASSPHRASE=key_passphrase
    
    # Database credentials
    DB_USER='root'
    DB_PASS='password'
    
    # Backup these databases
    DATABASES=(my_db_1 my_db_2 my_db_3) 
    
    # Working directory
    WORKING_DIR=/root/bak
    
    ########################################################################
    
    # Make the working directory
    mkdir $WORKING_DIR
    
    #
    # Dump the databases
    #
    for database in ${DATABASES[@]}; do
      mysqldump -u$DB_USER -p$DB_PASS $database > $WORKING_DIR/$database.sql
    done
    
    # Send them to b2
    duplicity --full-if-older-than 7D --encrypt-key="$ENCRYPT_KEY" $WORKING_DIR b2://$B2_ACCOUNT_ID:$B2_APPLICATION_KEY@$BUCKET
    
    # Verify
    duplicity verify --encrypt-key="$ENCRYPT_KEY" b2://$B2_ACCOUNT_ID:$B2_APPLICATION_KEY@$BUCKET $WORKING_DIR
    
    # Cleanup
    duplicity remove-older-than 30D --force --encrypt-key=$ENCRYPT_KEY b2://$B2_ACCOUNT_ID:$B2_APPLICATION_KEY@$BUCKET
    
    # Remove the working directory
    rm -rf $WORKING_DIR
    

    Run this via a cron job, something like this:

    0 4 * * * /root/b2_backup.sh >>/var/log/duplicity/backup.log

     

     

  • A systemd unit file for Play Framework

    A systemd unit file for Play Framework

    New for Ubuntu 16.04 (Xenial Xerus) is systemd which replaces Upstart as the default init system.

    systemd basics

    The basic object that systemd manages is the unit which can be of different types but when it comes to running our Play app we will need to create a service.

    The systemctl command is used to manage systemd services, similar to the old service command. E.g. to start Nginx you now type:

    sudo systemctl start nginx.service

    Custom unit files should go in the /etc/systemd/system directory and you should always run the following command after creating new unit files or modifying existing ones:

    sudo systemctl daemon-reload

    Run levels have been replaced with systemd units called targets. Target unit files end with the .target file extension and they are used to group other units.

    A Play Framework unit file

    Here is a basic Play Framework systemd unit file. It makes use of the EnvironmentFile directive which can be used to set environment variables. It is possible to set environment variables in the unit file directly but using a separate file takes some of the clutter out.


    ADDRESS=127.0.0.1
    PORT=9000
    APPLY_EVOLUTIONS=true
    APPLICATION_SECRET=my_secret

    view raw

    env

    hosted with ❤ by GitHub


    [Unit]
    Description=My play app
    After=network.target
    [Service]
    EnvironmentFile=/path/to/app/conf/env
    PIDFile=/path/to/app/RUNNING_PID
    WorkingDirectory=/path/to/app
    ExecStart=/path/to/app/bin/play -Dhttp.address=${ADDRESS} -Dhttp.port=${PORT} -Dplay.evolutions.db.default.autoApply=${APPLY_EVOLUTIONS} -J-server
    Restart=on-failure
    User=play_user
    Group=play_user
    # See http://serverfault.com/a/695863
    SuccessExitStatus=143
    [Install]
    WantedBy=multi-user.target

    view raw

    play.service

    hosted with ❤ by GitHub

    Deploying the unit file

    • Create the env file in your app’s conf directory and unit file in /etc/systemd/system
    • Set the file permissions
    sudo chmod 664 /etc/systemd/system/play.service
    • Reload systemd
    sudo systemctl daemon-reload

    You can now start and stop your play app using systemctl

    sudo systemctl start play.service
    sudo systemctl stop play.service

     

  • VirtualBox

    VirtualBox

    One the very few things that I need Windows for now is JavaME development. The Netbeans mobility pack runs under Linux but unfortunately all the manufacturers’ emulators require Windows. So now that I have sold my Windows laptop I needed some other way of running Windows. I considered using bootcamp on my MacBook or even repartitioning my linux box to dual boot Windows but I am now using some VM software called VirtualBox.

    I actually have a copy of VMWare 4.5 but that doesn’t play nice with the latest Fedora versions so rather than pay to upgrade VMWare I am using VirtualBox which is free for personal use. So far it runs really nicely on my dual-core Linux machine.

    A few things I needed to get it going under FC6:

    • Install the version for all distributions
    • Install compat-libstdc++-33
    • Make sure you put any users into the vboxusers group
    • Mess around with SELinux settings as described here