commit 4653935be7a25a4056884fde1d28d554c36a9f9b Author: Alexandre CATTEAU Date: Mon Mar 14 21:45:57 2022 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bfdbd30 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +variables.conf diff --git a/README.md b/README.md new file mode 100644 index 0000000..961540f --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# Firefox Backup + +This script performs a backup (archiving) of Firefox profile to a backup directory (ideally located on a different volume). As its purpose is to be run at computer shutdown (see service file), it also removes, if it exists, the 10-day old archive. + +### Files +* `firefox-backup.sh` performs a save of you Firefox profile. +* `variables.conf.templates` contains examples variables definitions, and should be copied locally to `variables.conf` + (with any required modifications). +* `systemd-templates/` contains Systemd units templates for running the script unattended. +#### Installation +* `install.sh` copies Systemd templates to their destination, and enables backup for the user running the script. + +### Notes +* If your backup directory is not on root volume, you may want to add a drop-in for the service unit, with + `Requires=.mount` + +### TODO diff --git a/firefox-backup.sh b/firefox-backup.sh new file mode 100644 index 0000000..4a51e47 --- /dev/null +++ b/firefox-backup.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash + +source "$(dirname "$0")/variables.conf" + +# Remove old archive +old="$backups_directory/profile-$(date -d "-10 days" +'%Y%m%d').tgz" +if [[ -f $old ]]; then + rm $old + echo "Removed $old" +else + echo "No old archive to remove." +fi + +# Save profile +if [[ ! -f "$backups_directory/profile-$(date +'%Y%m%d').tgz" ]]; then + tar zcf "$backups_directory/profile-$(date +'%Y%m%d').tgz" $firefox_data_path/$profile_name +fi + +exit 0 diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..5b89e16 --- /dev/null +++ b/install.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash + +run_directory=$(dirname "$0") +user=$(whoami) + +echo "Copying Systemd units to system directory..." +sudo cp $run_directory/systemd-templates/firefox-backup-.service /etc/systemd/system/firefox-backup-$user.service +sudo sed -i -e "s/PH_USER/$user/g" /etc/systemd/system/firefox-backup-$user.service +sudo sed -i -e "s:PH_DIRECTORY:$(pwd):g" /etc/systemd/system/firefox-backup-$user.service + +echo "Reloading Systemd..." +sudo systemctl daemon-reload +echo "Enabling service (and starting it so a backup occurs at next shutdown)..." +sudo systemctl enable --now firefox-backup-$user.service + +echo "------------------------------------------------------------------------" +echo "Make sure you set variables.conf!" +echo "If your backup directory is not on root volume, please add a drop-in with Requires=X.mount to /etc/systemd/system/firefox-backup-$user.service." +echo "------------------------------------------------------------------------" +echo "Finished install. Exiting..." +exit 0 diff --git a/systemd-templates/firefox-backup-.service b/systemd-templates/firefox-backup-.service new file mode 100644 index 0000000..7baa2bd --- /dev/null +++ b/systemd-templates/firefox-backup-.service @@ -0,0 +1,15 @@ +[Unit] +Description=Backup PH_USER's Firefox profile before shutdown +DefaultDependencies=no +Before=halt.target shutdown.target reboot.target poweroff.target +# A drop-in with Requires=X.mount may come handy if the target is not the root volume + +[Service] +Type=oneshot +User=PH_USER +ExecStart=/usr/bin/true +ExecStop=PH_DIRECTORY/firefox-backup.sh +RemainAfterExit=yes + +[Install] +WantedBy=default.target diff --git a/variables.conf.template b/variables.conf.template new file mode 100644 index 0000000..941a306 --- /dev/null +++ b/variables.conf.template @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +# Copy this file as variables.conf, with the correct values + +backups_directory="$HOME/firefox" # FIXME make sure you change this parameter +firefox_data_path="$HOME/.mozilla/firefox" +profile_name='profile'