Penguin Crumbs

Notes and tips from a Linux user

home about this blog

Your personal file hosting service in few lines of code

Ok, let’s say you have a personal server, always on, connected to the internet, with a public IP or always reachable using any dynamic DNS service. Let’s also say that for some reason you don’t want to use anymore a well known cloud storage, e.g. Dropbox, or Google Drive, or Microsoft OneDrive,....

Well, if you need just a simple, light, secure and personal backup service, without multi-user sharing features, rsync is exactly what you need. This solution is secure because you can send data using ssh, light because its synchronization algorithm is very efficient, simple because it needs just few lines of code and personal, allowing you to avoid to rely on an external service: your data will be stored in your server.

The bash script. The core is the rsync command and it’s executed twice: the first run is for simulating the backup process: this can produce file deletion too and that’s why it’simportant to simulate first. The script acts on an entire directory: if it isn’t passed as an argument, it will ask the user. The ssh password is asked just one time and then it’s handled in memory (ok, it’s not the top of the security, anyway it just works on a humble desktop).


# configure here your remote backup server
readonly REMOTE_SERVER="" # name or IP
readonly REMOTE_PORT="21743" # port
readonly REMOTE_USER="bck_user" # user on the server
readonly REMOTE_DIR="/home/bck_user/BACKUP" # backup dir

# echoes with time
function echo_log() {
    echo "$(date +%H:%M:%S) - $1"

# checks for missing commands
if "$(which sshpass)" == "" -o \
    "$(which rsync)" == "" -o \
    "$(which ssh)" == "" ]; then
    echo "this script needs ssh, sshpass and rsync; exit"
    exit 1

# get the directory name
if "$1" != "" ]; then
    read -p "souce dir? " source_dir
if "$source_dir" == "" ]; then
    echo "no dir, no sync; exit"
    exit 2
if "${source_dir:(-1)}" == "/" ]; then
    # strip slash at the end
if [ ! -d "$source_dir" ]; then
    echo "dir does not exist; exit"
    exit 3

# asks for ssh password
echo -n "remote password for $REMOTE_SERVER: "
read -s sshpasswordecho

while "$repeat" == "y" -o "$repeat" == "Y" ]; do
    echo_log "SIMULATION - start"
    rsync -aze "sshpass -p $sshpassword ssh -p $REMOTE_PORT" \
        --progress --dry-run --delete-after "$source_dir" \
    if "$status" -ne ]; then
        echo "connection failed; check network, password,..."
        exit 4
    echo_log "SIMULATION - end"

    read -p "Proceed (y/n)? " answer
    if "$answer" == "y" -o "$answer" == "Y" ]; then
        echo_log "SYNCH - start"
        rsync -aze "sshpass -p $sshpassword ssh -p $REMOTE_PORT" \
        --progress --delete-after "$source_dir" \
        echo_log "SYNCH - end"
        echo "no action"
    read -p "Repeat (y/n)? " repeat
done just leave it running on a terminal and launch the synchronization when needed.

Posted on 2019-05-19  
⇦  back
Copyright © 2019-2024 Marcello Zaniboni