Introduction

rsync seems to be the ideal choice for my backups for the following reasons:

  • It comes already installed on most systems (Linux, Android)
  • It is cross-platform (cwRsync on Windows)
  • Server and backup configuration is quick and minimal
  • Advanced options for experts are available (security, backup options)

We will present an easy solution for creating a backup server and scripts for Linux, Android, and Windows to help automate backup tasks. It is up to the user preference to further automate this solution by including the server start in system startup or creating scheduled jobs using the backup scripts included here.

 

Installing Backup Server

In our example, my backup directory is: /data/backup

Step 1 – Configure Server

Edit your /etc/rsyncd.config file to create a backup directory and backup user (requires root):

hosts allow = 192.168.0.0/24
[HomeBackup]
path = /data/backup
comment = backup server
uid = buser
gid = users
read only = false

For uid, use a username that is already on your system, or create a specific backup user (buser) and include the user into the users group.

Step 2 – Start Server

Start your rsync backup service (requires root):

# rsync --daemon

Step 3 – Test Server

Test your setup:

> rsync rsync://buser@hostname/

You should get the backup title and comment as configured previously:

HomeBackup backup server

Ready

With the above steps, your server is prepared to receive backups.

 

Backup From Linux

To backup the local folder ~/bin/ to your server backup directory /data/backup/bin/:

> rsync -va ~/bin/ rsync://buser@hostname/HomeBackup/bin/

To create a custom directory structure on a rsync server, you need to create the tree directory by directory:

> rsync /dev/null rsync://buser@hostname/HomeBackup/today/
> rsync /dev/null rsync://buser@hostname/HomeBackup/today/bin/

This would be required to be able sync a local directory which has a different name from the target directory, e.g.:

> rsync -va ~/bin/ rsync://buser@hostname/HomeBackup/today/bin/

See further below for a script to automate this task.

 

Backup From Android

Android, being essentially a Linux platform, requires the same commands as in the previous chapter.

To backup a local folder on your sdcard /storage/sdcard0/userdata/ to your backup directory /data/backup/userdata/:

> rsync -va /storage/sdcard0/userdata/ rsync://buser@hostname/HomeBackup/userdata/

To create a custom directory structure which is different from your source directory, you will need to create the tree directory by directory on a rsync server as already described previously.

See further below for a script to automate this task.

 

Linux and Android Backup Script

The following script will help you automate the backup tasks. It allows for backups to arbitrary server paths that may be different from the source path. The shell script is the same on both platforms, Android and Linux. It does not require root permissions to run, however may require root permissions to set executable permissions (# chmod 775), at least on the Android platform, as on Linux you could place it in your home ~/bin directory.

Depending on the intended target platform, you may need to delete either the first or the second line:

#! /system/bin/sh # for android
#! /bin/bash # for linux
# file: runbackup
# version: 0.00.06 | 2017-04-12
# author: pyramid
# init: 2017-03-29
# run backup to the specified rsync backup server and share
#---

user=buser
server=hostname
share=HomeBackup
device=mydevice

#---
# create target path on rsync server
# inputs: $1 - target path
createPath() {
    splits=$(echo $1 | tr "/" "\n")
    cbpath=''
    for addr in $splits
    do
        cbpath="$cbpath/$addr"
        rsync /dev/null rsync://$user@$server/$share/$cbpath/ > null
    done
}

# execute file transfer to rsync server
# inputs: $1 - source path; $2 - target path
backupPath() {
    rsync -av --progress $1 rsync://$user@$server/$share/$2
}

# execute backup of a path to rsync server
# inputs: $1 - source path; $2 - target path
doBackup() {
    createPath $2
    backupPath $1 $2
}

#--- MAIN

drive=/storage/sdcard0
SOURCE=$drive/userdata/transfer/
TARGET="install.config/app.user.data/android/$user/$device/transfer/"
doBackup $SOURCE $TARGET

To add further paths to you backup, just add the following three statements with adjusted paths as often as necessary and adjust the source and target paths accordingly:

SOURCE=$drive/userdata/projects/
TARGET="install.config/app.user.data/android/$user/$device/userdata/projects/"
doBackup $SOURCE $TARGET

 

Backup From Windows

A Windows specific implementation of the rsync protocol is CwRsync. Check here for the latest version.

Assuming that cwrsync is on your path, the command to execute a backup is similar to previous platforms:

cwrsync -av ~/bin/ rsync://buser@hostname/HomeBackup/bin/

To help you automate the backup tasks including the backup to arbitrary server paths, the following command script may be of help:

@echo off
:: file: runbackup.cmd
:: version: 0.00.05 | 2017-04-12
:: author: pyramid
:: init: 2017-04-11
:: windows command script to run backup to the specified rsync backup server and share
setlocal

set user=buser
set server=hostname
set share=HomeBackup
set device=mydevice
set rsync=C:\programs\cwRsync\bin\rsync.exe

:: ----------------------------------------------------------
:: MAIN
:: ----------------------------------------------------------

set drive=/cygdrive/c/

set SOURCE=%drive%/userdata/transfer/
set TARGET=/install.config/app.user.data/windows/anyuser/%device%/userdata/transfer/
call :doBackup %SOURCE% %TARGET%

:: force execution to quit at the end of the "main" logic
EXIT /B %ERRORLEVEL%

:: ----------------------------------------------------------
:: FUNCTIONS
:: ----------------------------------------------------------
:: execute backup of a path to rsync server
:: inputs: $1 - source path; $2 - target path
:doBackup
call :createPath %2
call :backupPath %1 %2
exit /B 0

:: create target path on rsync server
:: inputs: $1 - target path
:createPath
set t=%1
set cbpath=
:loop
for /f "tokens=1* delims=/" %%a in ("%t%") do (
    set appendix=%%a
    set t=%%b
)
set cbpath=%cbpath%%appendix%/
%rsync% . rsync://%user%@%server%/%share%/%cbpath% > nul
if defined t goto loop
exit /B 0

:: execute file transfer to rsync server
:: inputs: $1 - source path; $2 - target path
:backupPath
%rsync% -av --progress %1 rsync://%user%@%server%/%share%/%2
exit /B 0

To add further paths to you backup, just add the following three statements with adjusted paths as often as necessary and adjust the source and target paths accordingly:

set SOURCE=%drive%/userdata/projects/
set TARGET=/install.config/app.user.data/windows/anyuser/%device%/userdata/projects/
call :doBackup %SOURCE% %TARGET%

 

Summary

Basic rsync backup server installation can be easily accomplished in a few steps.
Using the provided scripts, one can automate backup tasks with little effort.
The adventurous user may further automate backups by:

  • Adding the start server command upon system startup
  • Making the backup script a scheduled job.

 

References

Tutorial on Using rsync
rsync on ArchWiki
10 Practical Examples of Rsync Command in Linux
rsync documentation
DOS Command Index

Advertisements