Fish

Flowonyx
Flowonyx

The fish shell gives me much greater productivity than bash in its default configuration.

# install fish
sudo apt-add-repository ppa:fish-shell/release-3
sudo apt-get update
sudo apt-get install fish
# set it as the default shell, where `ubuntu` is the username
sudo chsh -s /usr/bin/fish ubuntu
# start it or exit and login again
fish
set -U fish_user_paths ~/.local/bin $fish_user_paths

Functions

The fish shell has different syntax than bash or zsh but it is fairly easy to pick up. I have written a couple of functions for backing up and restoring volumes in docker. These functions are based on the official docker documentation for how to backup a volume.

functions.fish

#!/usr/bin/fish

function backup_docker

    if not test (count $argv) = 2
        echo "expected: backup_docker container /dir"
        return 1
    end

    set container $argv[1]
    set dir $argv[2]
    set path /

    set parts (string split -m 1 -r -n / $dir)

    if not test (count $parts) = 1
        if test (count $parts) = 0
            echo "dir must be an absolute path"
            return 1
        end
        set path $parts[1]
        set dir $parts[2]

        # in case the given dir ends with /
        if test (string length $dir) = 0
            echo "dir must not end with /"
            return 1
        end
    end

    echo "backing up $dir from $container to $container.tar"

    if test -f $container.tar
        echo "$container.tar already exits"
        return 1
    end

    # user 1000:1000 is ubuntu:ubuntu on the host...in the container it is just a non-existent user without privileges
    docker run --rm --user 1000:1000 --volumes-from $container -v (pwd):/backup ubuntu tar cf /backup/$container.tar -C $path $dir
end

funcsave backup_docker

function restore_docker
    if test (count $argv) != 3
        echo "expected: restore_docker container tarfile /dir"
        return 1
    end

    set container $argv[1]
    set tarfile $argv[2]
    set dir $argv[3]

    docker run --rm --volumes-from $container -v (pwd):/backup ubuntu bash -c "cd /$dir && tar xvf /backup/$tarfile --strip 1"
end

funcsave restore_docker

Executing this script will save these functions where they can be used anytime later. They are used like this:

backup_docker

backup_docker <container> <dir>

This will create <container>.tar in the current directory, which is a backup of the directory within the container that was specified. <dir> should be the path to the mount point for the volume you want to backup.

restore_docker

restore_docker <container> <tarfile> <dir>

This will extract the given <tarfile> in the given <dir> path within the given <container>.