Skip to content

Instantly share code, notes, and snippets.

@jgillula
Last active November 19, 2022 18:19
Show Gist options
  • Save jgillula/36de2f8a41a27cdaf148ca722342bffe to your computer and use it in GitHub Desktop.
Save jgillula/36de2f8a41a27cdaf148ca722342bffe to your computer and use it in GitHub Desktop.
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
07 02 * * * root run_backups.sh 2>&1 | logger -t run_backups
#!/bin/bash
#Enable for debugging
#set -x
# Export Paperless
echo "Exporting Paperless..."
PAPERLESS_SHA256_FILENAME="/media/raidencrypted/backups/paperless_backup/.sha256sum"
cd /var/local/paperless-ngx
/usr/bin/docker-compose exec -T webserver document_exporter ../export -f 2> /dev/null
if [ $? -eq 0 ]; then
# We skip backups if the manifest hasn't changed, so let's record its sha256sum
sha256sum /media/raidencrypted/backups/paperless_backup/manifest.json /media/raidencrypted/backups/paperless_backup/version.json > "${PAPERLESS_SHA256_FILENAME}"
echo "Paperless exported successfully"
else
>&2 echo "Error exporting Paperless"
fi
# Run Timeshift
echo "Running Timeshift..."
max_retries=10
counter=1
wait_time_seconds=60
/usr/bin/timeshift --check --scripted
if [ $? -eq 0 ]; then
echo "Timeshift run successfully"
else
while [ $counter -lt $max_retries ]; do
>&2 echo "Couldn't start Timeshift, waiting $wait_time_seconds seconds to try again..."
sleep $wait_time_seconds
((counter++))
echo "Trying to start Timeshift again. Try $counter"
/usr/bin/timeshift --check --scripted
if [ $? -eq 0 ]; then
echo "Timeshift run successfully"
break
fi
done
fi
if [ $counter -eq $max_retries ]; then
>&2 echo "Tried to run Timeshift too many times, giving up!"
fi
# Perform the borgmatic backups one at a time
/usr/bin/borgmatic -c /etc/borgmatic/config.yaml --verbosity 1 --syslog-verbosity 1
# For home directories, we skip backups if there haven't been any changes
LAST_HOME_DIR_CHANGE_TIME=`find /home/ -type f -print0 | xargs -0 stat --format '%Y' | sort -nr | head -n 1`
LAST_HOME_DIR_BACKUP_TIME=`date -d "\`borgmatic --config /etc/borgmatic.d/home_dirs.yaml list --last 1 | tail -n 2 | sed -E 's/.*, (.*?) \[.*/\1/' | sed 's/\x1B\[[0-9;]*[JKmsu]//g' | sed 's/\n//'\`" +%s`
if [ $LAST_HOME_DIR_CHANGE_TIME -gt $LAST_HOME_DIR_BACKUP_TIME ]; then
/usr/bin/borgmatic -c /etc/borgmatic.d/home_dirs.yaml --verbosity 1 --syslog-verbosity 1
else
echo "No changes to home directories, skipping backup"
fi
# For paperless, we skip backups if there haven't been any changes
# So extract the latest version from the backup
/usr/bin/borgmatic -c /etc/borgmatic.d/paperless.yaml extract --archive latest --path "${PAPERLESS_SHA256_FILENAME}" --destination /tmp
# And compare it, and run the backup if they're different
diff -q "${PAPERLESS_SHA256_FILENAME}" /tmp/"${PAPERLESS_SHA256_FILENAME}" > /dev/null
if [ $? -eq 1 ]; then
/usr/bin/borgmatic -c /etc/borgmatic.d/paperless.yaml --verbosity 1 --syslog-verbosity 1
else
echo "No changes to paperless, skipping backup"
fi
# Finally we backup to the cloud
for repo_name in home_dirs paperless system ; do
echo "Syncing borg repo $repo_name to borgbase using rclone"
sudo rclone sync --config /etc/borgmatic/rclone.conf /media/raidencrypted/backups/borg-backup/repos/server.local/$repo_name borgbase-server-$repo_name:repo
echo "Done syncing borg repo $repo_name to borgbase using rclone"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment