#!/bin/bash # # rm2-backup # John Simpson 2023-06-29 # # Back up the reMarkable 2 tablet, using rsync with the '--link-dest' option # so each backup only "contains" the files which changed from the previous # backup. # # Requirements: # # - SSH access to the tablet. Ideally you should have key-based authentication # set up, otherwise you'll have to type the tablet's password when running # the script. # # - The location where you're storing the backups should be on a UNIX-type # filesystem which supports "hard links" (i.e. multiple filenames pointing # to the same inode). This is true of most Linux filesytems, as well as the # macOS "HFS+" and "APFS" filesystems. # # - rsync version 3.1.1 or higher. In earlier versions (such as the version # that Apple includes with macOS), the '--link-dest' option didn't use hard # links, and every backup conained a full copy of the tablet's filesystem, # which took longer and used a LOT more disk space. # # If you're using macOS and are stuck with its ancient version of rsync, you # can install a newer/working version from Homebrew. https://brew.sh/ # # 2023-07-17 jms1 - Add 'latest' symlink in the backup directory. # # 2023-08-04 jms1 - Add serial number to backup directory # # 2023-08-05 jms1 - *always* use serial number in backup directory name # # 2023-08-18 jms1 - Adding a LIST of default backup directory locations. # ############################################################################### # # The MIT License (MIT) # # Copyright (C) 2023 John Simpson # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the “Software”), # to deal in the Software without restriction, including without limitation # the rights to use, copy, modify, merge, publish, distribute, sublicense, # and/or sell copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. # ############################################################################### ######################################## # How to access the tablet. TABLET="10.11.99.1" ######################################## # Where you plan to store the backup images. # - The script walks through this list. The first location which exists as a # directory, will be used. # - If none of them exist, the script will fail. # - The '-b' option overrides this list. BACKUP_LOCATIONS=( "/Volumes/rm2-backups" "$HOME/rm2-backups" ) ############################################################################### # # Coloured line functions function blueline { if [[ -t 1 ]] then printf "\e[0;1;37;44m%s\e[0K\e[0m\n" "$*" else echo "===== $* =====" fi } ############################################################################### # # usage function usage { MSG="${*:-}" cat </dev/null" ) ######################################## # Make sure the tablet's serial number directory exists mkdir -p "$BACKUPS/$SERIAL" ######################################## # Get current timestamp, used for the backup directory name. NOW="$( date -u '+%Y-%m-%dT%H%M%SZ' )" ######################################## # Find the most recent previous backup. # - This assumes that the timestamps all start with "2", and that the 'ls' # command sorts the names correctly. THIS WILL BREAK in the year 3000. PREV="$( ls -d1 "$BACKUPS/$SERIAL"/2* | tail -1 )" if [[ -n "$PREV" ]] then LINKPREV="--link-dest=$PREV" else LINKPREV="" fi ############################################################################### # # Back up the files set -x rsync -avzHl $LINKPREV \ --exclude /dev \ --exclude /proc \ --exclude /run \ --exclude /sys \ "root@$TABLET:/" \ "$BACKUPS/$SERIAL/$NOW/" rm -f "$BACKUPS/$SERIAL/latest" ln -s "$NOW" "$BACKUPS/$SERIAL/latest"