|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Sync Nextcloud BTRFS snapshots |
| 4 | +# |
| 5 | +# Copyleft 2017 by Ignacio Nunez Hernanz <nacho _a_t_ ownyourbits _d_o_t_ com> |
| 6 | +# GPL licensed (see end of file) * Use at your own risk! |
| 7 | +# |
| 8 | +# Usage: |
| 9 | +# |
| 10 | +# ./installer.sh nc-snapshot-sync.sh <IP> (<img>) |
| 11 | +# |
| 12 | +# See installer.sh instructions for details |
| 13 | +# |
| 14 | +# More at https://ownyourbits.com/2017/02/13/nextcloud-ready-raspberry-pi-image/ |
| 15 | +# |
| 16 | + |
| 17 | +ACTIVE_=no |
| 18 | +SNAPDIR_=/media/USBdrive/ncp-snapshots |
| 19 | +DESTINATION_=/media/myBackupDrive/ncp-snapshots |
| 20 | +COMPRESSION_=no |
| 21 | +SYNCDAYS_=1 |
| 22 | +DESCRIPTION="Sync BTRFS snapshots to USBdrive or remote machine" |
| 23 | + |
| 24 | +INFO="Use format user@ip:/path/to/snapshots for remote sync |
| 25 | +'user' needs permissions for the 'btrfs' command at 'ip' |
| 26 | +'user' needs SSH autologin from the NCP 'root' user at 'ip' |
| 27 | +Only use compression for internet transfer, because it uses many resources" |
| 28 | + |
| 29 | +install() |
| 30 | +{ |
| 31 | + apt-get update |
| 32 | + apt-get install -y --no-install-recommends pv |
| 33 | + wget https://raw.githubusercontent.com/nachoparker/btrfs-sync/master/btrfs-sync -O /usr/local/bin/btrfs-sync |
| 34 | + chmod +x /usr/local/bin/btrfs-sync |
| 35 | + ssh-keygen -N "" -f /root/.ssh/id_rsa |
| 36 | +} |
| 37 | + |
| 38 | +configure() |
| 39 | +{ |
| 40 | + [[ $ACTIVE_ != "yes" ]] && { |
| 41 | + rm /etc/cron.d/ncp-snapsync-auto |
| 42 | + service cron restart |
| 43 | + echo "snapshot sync disabled" |
| 44 | + return 0 |
| 45 | + } |
| 46 | + |
| 47 | + # checks |
| 48 | + [[ -d "$SNAPDIR_" ]] || { echo "$SNAPDIR_ does not exist"; return 1; } |
| 49 | + |
| 50 | + [[ "$DESTINATION_" =~ : ]] && { |
| 51 | + local NET="$( sed 's|:.*||' <<<"$DESTINATION_" )" |
| 52 | + local DST="$( sed 's|.*:||' <<<"$DESTINATION_" )" |
| 53 | + local SSH=( ssh -o "BatchMode=yes" "$NET" ) |
| 54 | + ${SSH[@]} : || { echo "SSH non-interactive not properly configured"; return 1; } |
| 55 | + } || DST="$DESTINATION_" |
| 56 | + [[ "$( ${SSH[@]} stat -fc%T "$DST" )" != "btrfs" ]] && { |
| 57 | + echo "$DESTINATION_ is not in a BTRFS filesystem" |
| 58 | + return 1 |
| 59 | + } |
| 60 | + |
| 61 | + [[ "$COMPRESSION_" == "yes" ]] && ZIP="-z" |
| 62 | + |
| 63 | + echo "30 4 */${SYNCDAYS_} * * root /usr/local/bin/btrfs-sync -qd $ZIP \"$SNAPDIR_\" \"$DESTINATION_\"" > /etc/cron.d/ncp-snapsync-auto |
| 64 | + service cron restart |
| 65 | + echo "snapshot sync enabled" |
| 66 | +} |
| 67 | + |
| 68 | +# License |
| 69 | +# |
| 70 | +# This script is free software; you can redistribute it and/or modify it |
| 71 | +# under the terms of the GNU General Public License as published by |
| 72 | +# the Free Software Foundation; either version 2 of the License, or |
| 73 | +# (at your option) any later version. |
| 74 | +# |
| 75 | +# This script is distributed in the hope that it will be useful, |
| 76 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 77 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 78 | +# GNU General Public License for more details. |
| 79 | +# |
| 80 | +# You should have received a copy of the GNU General Public License |
| 81 | +# along with this script; if not, write to the |
| 82 | +# Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
| 83 | +# Boston, MA 02111-1307 USA |
| 84 | + |
0 commit comments