2023-01-28 22:07:01 -08:00
|
|
|
{{- if ne .host.distro.family "windows" -}}
|
|
|
|
#!/usr/bin/env bash
|
2023-04-11 13:30:35 -07:00
|
|
|
# @file Rclone S3 Mounts
|
|
|
|
# @brief This script configures Rclone to provide several S3-compliant mounts by leveraging CloudFlare R2
|
|
|
|
# @description
|
|
|
|
# Install Doctor leverages Rclone and CloudFlare R2 to provide S3-compliant bucket mounts that allow you to retain stateful files and configurations.
|
|
|
|
# In general, these buckets are used for backing up files like your browser profiles, Docker backup files, and other files that cannot be stored as
|
|
|
|
# as code in your Install Doctor fork.
|
|
|
|
#
|
|
|
|
# This script sets up Rclone to provide several folders that are synchronized with S3-compliant buckets (using CloudFlare R2 by default).
|
|
|
|
# The script ensures required directories are created and that proper permissions are applied. This script will only run if `rclone` is
|
|
|
|
# available in the `PATH`. It also requires the user to provide `CLOUDFLARE_R2_ID` and `CLOUDFLARE_R2_SECRET` as either environment variables
|
|
|
|
# or through the encrypted repository-fork-housed method detailed in the [Secrets documentation](https://install.doctor/docs/customization/secrets).
|
|
|
|
#
|
|
|
|
# ## Mounts
|
|
|
|
#
|
|
|
|
# The script will setup five mounts by default and enable / start `systemd` services on Linux systems so that the mounts are available
|
|
|
|
# whenever the device is turned on. The mounts are:
|
|
|
|
#
|
|
|
|
# | Mount Location | Description |
|
|
|
|
# |-----------------------|-----------------------------------------------------------------------------------------------------------------------|
|
2023-07-16 03:10:04 -07:00
|
|
|
# | `/mnt/Private` | Private system-wide bucket used for any private files that should not be able to be accessed publicly over HTTPS |
|
|
|
|
# | `/mnt/Public` | Public system-wide bucket that can be accessed by anyone over HTTPS with the bucket's URL (provided by CloudFlare R2) |
|
|
|
|
# | N/A | Private system-wide bucket used for storing Docker-related backups / files |
|
|
|
|
# | N/A | Private system-wide bucket similar to `/mnt/Private` but intended for system file backups |
|
2023-11-04 20:56:58 -07:00
|
|
|
# | `$HOME/Public` | Private user-specific bucket (used for backing up application settings) |
|
2023-04-11 13:30:35 -07:00
|
|
|
#
|
|
|
|
# ## Permissions
|
|
|
|
#
|
|
|
|
# The system files are all assigned proper permissions and are owned by the user `rclone` with the group `rclone`. The exception to this is the
|
|
|
|
# user-specific mount which uses the user's user name and user group.
|
|
|
|
#
|
|
|
|
# ## Samba
|
|
|
|
#
|
|
|
|
# If Samba is installed, then by default Samba will create two shares that are symlinked to the `/mnt/s3-private` and `/mnt/s3-public`
|
|
|
|
# buckets. This feature allows you to easily access the two buckets from other devices in your local network. If Rclone buckets are not
|
|
|
|
# available then the Samba setup script will just create regular empty folders as shares.
|
|
|
|
#
|
|
|
|
# ## Notes
|
|
|
|
#
|
|
|
|
# * The mount services all leverage the executable found at `$HOME/.local/bin/rclone-mount` to mount the shares.
|
|
|
|
#
|
|
|
|
# ## Links
|
|
|
|
#
|
|
|
|
# * [Rclone mount script](https://github.com/megabyte-labs/install.doctor/tree/master/home/dot_local/bin/executable_rclone-mount)
|
|
|
|
# * [Rclone default configurations](https://github.com/megabyte-labs/install.doctor/tree/master/home/dot_config/rclone)
|
|
|
|
# * [Rclone documentation](https://rclone.org/docs/)
|
2023-01-28 22:07:01 -08:00
|
|
|
|
|
|
|
{{ includeTemplate "universal/profile" }}
|
|
|
|
{{ includeTemplate "universal/logg" }}
|
|
|
|
|
2023-01-29 20:44:55 -08:00
|
|
|
if command -v rclone > /dev/null; then
|
2023-07-31 11:40:18 -07:00
|
|
|
{{- if and (or (and (stat (joinPath .host.home ".config" "age" "chezmoi.txt")) (stat (joinPath .chezmoi.sourceDir ".chezmoitemplates" "secrets" "CLOUDFLARE_R2_ID"))) (env "CLOUDFLARE_R2_ID")) (or (and (stat (joinPath .host.home ".config" "age" "chezmoi.txt")) (stat (joinPath .chezmoi.sourceDir ".chezmoitemplates" "secrets" "CLOUDFLARE_R2_SECRET"))) (env "CLOUDFLARE_R2_SECRET")) (ne .user.cloudflare.r2 "") }}
|
2023-11-06 22:53:36 -08:00
|
|
|
logg info 'Removing ~/.config/rclone/rclone.conf Install Doctor managed block'
|
2023-08-07 22:29:21 -07:00
|
|
|
CONFIG_FILE="${XDG_CONFIG_HOME:-$HOME/.config}/rclone/rclone.conf"
|
2023-11-06 22:53:36 -08:00
|
|
|
if cat "$CONFIG_FILE" | grep '# INSTALL DOCTOR MANAGED S3 START' > /dev/null; then
|
2023-07-30 23:20:47 -07:00
|
|
|
# TODO: Remove old block
|
2023-08-30 14:01:07 -07:00
|
|
|
START_LINE="$(echo `grep -n -m 1 "# INSTALL DOCTOR MANAGED S3 START" "$CONFIG_FILE" | cut -f1 -d ":"`)"
|
|
|
|
END_LINE="$(echo `grep -n -m 1 "# INSTALL DOCTOR MANAGED S3 END" "$CONFIG_FILE" | cut -f1 -d ":"`)"
|
2023-07-30 23:20:47 -07:00
|
|
|
if command -v gsed > /dev/null; then
|
2023-11-06 23:06:49 -08:00
|
|
|
gsed -i "$START_LINE,${END_LINE}d" "$CONFIG_FILE" > /dev/null
|
2023-07-30 23:20:47 -07:00
|
|
|
else
|
2023-11-06 23:06:49 -08:00
|
|
|
sed -i "$START_LINE,${END_LINE}d" "$CONFIG_FILE" > /dev/null
|
2023-07-30 23:20:47 -07:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
logg info 'Adding ~/.config/rclone/rclone.conf INSTALL DOCTOR managed block'
|
|
|
|
tee -a "$CONFIG_FILE" > /dev/null <<EOT
|
2023-08-30 14:01:07 -07:00
|
|
|
# INSTALL DOCTOR MANAGED S3 START
|
2023-07-30 23:20:47 -07:00
|
|
|
[{{ .user.username}}-s3]
|
|
|
|
access_key_id = {{ if (stat (joinPath .chezmoi.sourceDir ".chezmoitemplates" "secrets" "CLOUDFLARE_R2_ID")) }}{{- includeTemplate "secrets/CLOUDFLARE_R2_ID" | decrypt | trim -}}{{ else }}{{- env "CLOUDFLARE_R2_ID" -}}{{ end }}
|
|
|
|
acl = private
|
|
|
|
endpoint = {{ .user.cloudflare.r2 }}.r2.cloudflarestorage.com
|
|
|
|
provider = Cloudflare
|
|
|
|
region = auto
|
|
|
|
secret_access_key = {{ if (stat (joinPath .chezmoi.sourceDir ".chezmoitemplates" "secrets" "CLOUDFLARE_R2_SECRET")) }}{{- includeTemplate "secrets/CLOUDFLARE_R2_SECRET" | decrypt | trim -}}{{ else }}{{- env "CLOUDFLARE_R2_SECRET" -}}{{ end }}
|
|
|
|
type = s3
|
2023-08-30 14:01:07 -07:00
|
|
|
# INSTALL DOCTOR MANAGED S3 END
|
2023-07-30 23:20:47 -07:00
|
|
|
EOT
|
2023-07-31 11:40:18 -07:00
|
|
|
{{- end }}
|
2023-07-30 23:20:47 -07:00
|
|
|
|
2023-01-28 22:07:01 -08:00
|
|
|
logg info 'Ensuring /var/cache/rclone exists'
|
|
|
|
sudo mkdir -p /var/cache/rclone
|
2023-07-16 03:10:04 -07:00
|
|
|
sudo chmod 770 /var/cache/rclone
|
2023-06-22 21:39:22 -07:00
|
|
|
# Add user / group with script in ~/.local/bin/add-user, if it is available
|
|
|
|
if command -v add-user > /dev/null; then
|
|
|
|
sudo add-user rclone
|
|
|
|
fi
|
2023-11-26 22:46:35 -08:00
|
|
|
sudo chown -Rf rclone:rclone /var/cache/rclone 2> /dev/null || sudo chown -Rf rclone:$(id -g -n) /var/cache/rclone
|
2023-01-28 22:07:01 -08:00
|
|
|
|
2023-01-28 22:55:46 -08:00
|
|
|
logg info 'Ensuring /var/log/rclone exists'
|
|
|
|
sudo mkdir -p /var/log/rclone
|
2023-07-16 03:10:04 -07:00
|
|
|
sudo chmod 770 /var/log/rclone
|
2023-11-26 22:46:35 -08:00
|
|
|
sudo chown -Rf rclone:rclone /var/log/rclone 2> /dev/null || sudo chown -Rf rclone:$(id -g -n) /var/log/rclone
|
2023-01-28 22:07:01 -08:00
|
|
|
|
|
|
|
logg info 'Adding ~/.local/bin/rclone-mount to /usr/local/bin'
|
|
|
|
sudo cp -f "$HOME/.local/bin/rclone-mount" /usr/local/bin/rclone-mount
|
|
|
|
sudo chmod +x /usr/local/bin/rclone-mount
|
|
|
|
|
2023-01-28 22:55:46 -08:00
|
|
|
logg info 'Adding ~/.config/rclone/rcloneignore to /etc/rcloneignore'
|
|
|
|
sudo cp -f "${XDG_CONFIG_HOME:-$HOME/.config}/rclone/rcloneignore" /etc/rcloneignore
|
2023-07-16 03:10:04 -07:00
|
|
|
sudo chmod 640 /etc/rcloneignore
|
2023-01-28 22:55:46 -08:00
|
|
|
|
2023-01-29 20:44:55 -08:00
|
|
|
logg info 'Adding ~/.config/rclone/system-rclone.conf to /etc/rclone.conf'
|
2023-01-31 22:02:56 -08:00
|
|
|
sudo cp -f "${XDG_CONFIG_HOME:-$HOME/.config}/rclone/system-rclone.conf" /etc/rclone.conf
|
2023-07-16 03:10:04 -07:00
|
|
|
sudo chmod 640 /etc/rclone.conf
|
2023-01-28 22:07:01 -08:00
|
|
|
|
2023-01-29 20:44:55 -08:00
|
|
|
### Add / configure service files
|
|
|
|
if [ -d /etc/systemd/system ]; then
|
|
|
|
find "${XDG_CONFIG_HOME:-$HOME/.config}/rclone/system" -mindepth 1 -maxdepth 1 -type f | while read RCLONE_SERVICE; do
|
|
|
|
### Add systemd service file
|
|
|
|
logg info "Adding S3 system mount service defined at $RCLONE_SERVICE"
|
|
|
|
FILENAME="$(basename "$RCLONE_SERVICE")"
|
|
|
|
SERVICE_ID="$(echo "$FILENAME" | sed 's/.service//')"
|
|
|
|
sudo cp -f "$RCLONE_SERVICE" "/etc/systemd/system/$(basename "$RCLONE_SERVICE")"
|
|
|
|
|
|
|
|
### Ensure mount folder is created
|
|
|
|
logg info "Ensuring /mnt/$SERVICE_ID is created with proper permissions"
|
|
|
|
sudo mkdir -p "/mnt/$SERVICE_ID"
|
|
|
|
sudo chmod 770 "/mnt/$SERVICE_ID"
|
2023-11-26 22:46:35 -08:00
|
|
|
sudo chown -Rf rclone:rclone "/mnt/$SERVICE_ID" 2> /dev/null || sudo chown -Rf rclone:$(id -g -n) "/mnt/$SERVICE_ID"
|
2023-01-29 20:44:55 -08:00
|
|
|
|
|
|
|
### Enable / restart the service
|
2023-01-31 22:02:56 -08:00
|
|
|
logg info "Enabling / restarting the $SERVICE_ID S3 service"
|
2023-01-29 20:44:55 -08:00
|
|
|
sudo systemctl enable "$SERVICE_ID"
|
|
|
|
sudo systemctl restart "$SERVICE_ID"
|
|
|
|
done
|
|
|
|
|
|
|
|
### Add user Rclone mount
|
2023-01-28 22:55:46 -08:00
|
|
|
logg info 'Adding user S3 rclone mount (available at ~/.local/mnt/s3)'
|
2023-02-04 20:01:40 -08:00
|
|
|
sudo cp -f "${XDG_CONFIG_HOME:-$HOME/.config}/rclone/s3-user.service" "/etc/systemd/system/s3-${USER}.service"
|
2023-01-28 22:55:46 -08:00
|
|
|
logg info 'Enabling / restarting the S3 user mount'
|
2023-02-04 20:01:40 -08:00
|
|
|
sudo systemctl enable "s3-${USER}"
|
|
|
|
sudo systemctl restart "s3-${USER}"
|
2023-01-28 22:55:46 -08:00
|
|
|
fi
|
2023-07-17 00:28:38 -07:00
|
|
|
|
|
|
|
if [ -d /Applications ] && [ -d /System ]; then
|
|
|
|
### Enable Rclone mounts
|
|
|
|
logg info 'Ensuring Rclone mount-on-reboot definitions are in place'
|
|
|
|
if [ -f "$HOME/Library/LaunchDaemons/rclone.private.plist" ] && [ ! -f "/Library/LaunchDaemons/rclone.private.plist" ]; then
|
|
|
|
logg info 'Adding /Volumes/Private as S3 bucket mount, enabled at boot'
|
|
|
|
sudo mkdir -p /Library/LaunchDaemons
|
|
|
|
sudo cp -f "$HOME/Library/LaunchDaemons/rclone.private.plist" '/Library/LaunchDaemons/rclone.private.plist'
|
|
|
|
sudo launchctl load '/Library/LaunchDaemons/rclone.private.plist' && logg success 'launchctl load successful'
|
|
|
|
fi
|
|
|
|
if [ -f "$HOME/Library/LaunchDaemons/rclone.public.plist" ] && [ ! -f "/Library/LaunchDaemons/rclone.public.plist" ]; then
|
|
|
|
logg info 'Adding /Volumes/Public as S3 bucket mount, enabled at boot'
|
|
|
|
sudo mkdir -p /Library/LaunchDaemons
|
|
|
|
sudo cp -f "$HOME/Library/LaunchDaemons/rclone.public.plist" '/Library/LaunchDaemons/rclone.public.plist'
|
|
|
|
sudo launchctl load '/Library/LaunchDaemons/rclone.public.plist' && logg success 'launchctl load successful'
|
|
|
|
fi
|
|
|
|
if [ -f "$HOME/Library/LaunchDaemons/rclone.user.plist" ] && ! launchctl list | grep 'rclone.user' > /dev/null; then
|
|
|
|
logg info 'Adding ~/Public as S3 bucket mount, enabled at boot'
|
|
|
|
launchctl load "$HOME/Library/LaunchDaemons/rclone.user.plist" && logg success 'user launchctl load successful'
|
|
|
|
fi
|
|
|
|
fi
|
2023-01-28 22:07:01 -08:00
|
|
|
else
|
2023-11-04 18:46:18 -07:00
|
|
|
logg info 'rclone is not available'
|
2023-01-28 22:07:01 -08:00
|
|
|
fi
|
|
|
|
|
|
|
|
{{ end -}}
|