Aug 6, 2023

Wireguard and Nextcloud server at home

Wireguard and nextcloud server at home. Goes to trusted geodiverse location, Wireguard back to the first, cronjob rsync to create geodiverse backups of the nextcloud data.

To achieve this, follow these steps:

  1. Set up the NextCloud Server on the First Raspberry Pi (RPi1):

    Install the required packages:

    sudo apt update
    sudo apt upgrade
    sudo apt install apache2 mariadb-server libapache2-mod-php7.3
    sudo apt install php7.3-gd php7.3-json php7.3-mysql php7.3-curl php7.3-mbstring
    sudo apt install php7.3-intl php-imagick php7.3-xml php7.3-zip
    

    Now download and install NextCloud:

    cd /var/www/html/
    sudo wget https://download.nextcloud.com/server/releases/nextcloud-22.0.0.zip
    sudo unzip nextcloud-22.0.0.zip
    sudo chown -R www-data:www-data nextcloud
    sudo chmod 755 -R nextcloud
    

    Create a new database for NextCloud:

    sudo mariadb
    CREATE DATABASE nextcloud;
    CREATE USER 'nextcloud'@'localhost' IDENTIFIED BY 'password';
    GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextcloud'@'localhost';
    FLUSH PRIVILEGES;
    QUIT;
    

    Now, configure the Apache server to work with NextCloud:

    sudo nano /etc/apache2/sites-available/nextcloud.conf
    

    Paste the following:

    Alias /nextcloud "/var/www/html/nextcloud/"
    <Directory /var/www/html/nextcloud/>
    Require all granted
    AllowOverride All
    Options FollowSymLinks MultiViews
    <IfModule mod_dav.c>
    Dav off
    </IfModule>
    SetEnv HOME /var/www/html/nextcloud
    SetEnv HTTP_HOME /var/www/html/nextcloud
    </Directory>
    

    Enable the site, rewrite module, and restart Apache:

    sudo a2ensite nextcloud.conf
    sudo a2enmod rewrite
    sudo systemctl restart apache2
    
  2. Set up WireGuard on Both Raspberry Pis (RPi1 & RPi2):

    WireGuard setup involves generating public and private keys on both servers, and then exchanging these keys between the servers.

    On each Pi, install WireGuard:

    sudo apt install wireguard
    

    Generate the public and private keys:

    umask 077
    wg genkey > privatekey
    wg pubkey < privatekey > publickey
    

    On RPi1, configure WireGuard:

    sudo nano /etc/wireguard/wg0.conf
    

    Add the following configuration, replacing the placeholders with your values:

    [Interface]
    Address = 10.0.0.1/24
    SaveConfig = true
    PrivateKey = RPi1_PRIVATE_KEY
    ListenPort = 51820
    
    [Peer]
    PublicKey = RPi2_PUBLIC_KEY
    AllowedIPs = 10.0.0.2/32
    

    On RPi2, configure WireGuard:

    sudo nano /etc/wireguard/wg0.conf
    

    Add the following configuration, replacing the placeholders with your values:

    [Interface]
    Address = 10.0.0.2/24
    SaveConfig = true
    PrivateKey = RPi2_PRIVATE_KEY
    ListenPort = 51820
    
    [Peer]
    PublicKey = RPi1_PUBLIC_KEY
    AllowedIPs = 10.0.0.1/32
    Endpoint = RPi1_PUBLIC_IP:51820
    

    Start and enable WireGuard on both Pis:

    sudo systemctl start wg-quick@wg0
    sudo systemctl enable wg-quick@wg0
    
  3. Set Up Rsync for Backups:

    Install rsync on both Pis:

    sudo apt-get install rsync
    

    On RPi1, set up SSH keys for passwordless login:

    ssh-keygen -t rsa
    ssh-copy-id username@RPi2
    

    Now create a cronjob to perform the backup. Open the crontab editor:

    crontab -e
    

    Add a new line to your crontab that calls rsync:

    0 2 * * * rsync -az /path/to/nextcloud/data username@RPi2:/path/to/backup/location
    

    This will perform a backup of the NextCloud data folder from RPi1 to RPi2 every day at 2 AM.

This provides a basic setup for a geodiverse backup strategy for NextCloud using WireGuard for secure connections and rsync for data transfer. You might need to further adjust configurations to suit your particular needs, for example by hardening server security, optimizing performance, or handling exceptions during data transfer.