aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Odersky <jakob@odersky.com>2018-11-11 14:07:43 -0800
committerJakob Odersky <jakob@odersky.com>2018-11-11 14:07:43 -0800
commita686d0105185fb5ec578879fd6160eefb951d83c (patch)
treeee793aee8c1a2ca0a105a61566cbe4b56929e956
parent475362d7625306facdbaec36f8143464c8734d74 (diff)
downloaddotfiles-a686d0105185fb5ec578879fd6160eefb951d83c.tar.gz
dotfiles-a686d0105185fb5ec578879fd6160eefb951d83c.tar.bz2
dotfiles-a686d0105185fb5ec578879fd6160eefb951d83c.zip
Add backup scripts, including systemd service and timer
-rwxr-xr-xhome/.config/i3/status/backup44
-rw-r--r--home/.config/systemd/user/backup.service8
-rw-r--r--home/.config/systemd/user/backup.timer12
-rw-r--r--home/.config/systemd/user/dbusmonitor.service9
-rw-r--r--home/.config/systemd/user/unmetered-internet.target2
-rwxr-xr-xhome/.local/bin/dbusmonitor27
-rwxr-xr-xhome/bin/backup19
-rwxr-xr-xinstallpackages1
8 files changed, 83 insertions, 39 deletions
diff --git a/home/.config/i3/status/backup b/home/.config/i3/status/backup
index 48a8a3e..dd27225 100755
--- a/home/.config/i3/status/backup
+++ b/home/.config/i3/status/backup
@@ -1,29 +1,25 @@
#!/bin/bash
-uuid=05b7770e-1f01-4b17-83de-ede0a04495a1
-instance="$(systemd-escape $uuid)"
+systemctl="systemctl --user"
-if systemctl --quiet is-active "cryo@$instance.service"; then
- echo ""
- echo "backup running"
- echo "#00ff00"
-elif systemctl --quiet is-failed "cryo@$instance.service"; then
- echo ""
- echo "backup failed"
- echo "#ff0000"
-elif [[ -f /var/lib/cryo/$uuid/alpha ]]; then
- last_run=$(date --reference "/var/lib/cryo/$uuid/alpha" +%s)
- now=$(date +%s)
- days=$(( (now - last_run) / 60 / 60 / 24 ))
- if [[ $days -gt 30 ]]; then
- echo ">30d"
- echo "backup last run over 30 days ago"
- echo "#ff0000"
+function systemd_status() {
+ unit="$1"
+ if $systemctl --quiet is-active "$unit"; then
+ echo ""
+ elif $systemctl --quiet is-failed "$unit"; then
+ echo ""
+ elif ! $systemctl --quiet is-enabled "$unit"; then
+ # note that !enabled includes units that have not been found
+ echo ""
else
- echo "${days}d"
- echo "backup last run $days days ago"
+ echo ""
fi
-else
- echo "no backup"
- echo "no recent backup found"
-fi
+}
+
+status=""
+status+="$(systemd_status unmetered-internet.target)"
+status+="$(systemd_status backup.timer)"
+status+="$(systemd_status backup.service)"
+
+echo "$status"
+echo "$status"
diff --git a/home/.config/systemd/user/backup.service b/home/.config/systemd/user/backup.service
new file mode 100644
index 0000000..3901d64
--- /dev/null
+++ b/home/.config/systemd/user/backup.service
@@ -0,0 +1,8 @@
+[Unit]
+Description=copy home data to remote storage
+Requisite=unmetered-internet.target
+After=unmetered-internet.target
+
+[Service]
+Type=simple
+ExecStart=%h/bin/backup
diff --git a/home/.config/systemd/user/backup.timer b/home/.config/systemd/user/backup.timer
new file mode 100644
index 0000000..cd8c3c3
--- /dev/null
+++ b/home/.config/systemd/user/backup.timer
@@ -0,0 +1,12 @@
+[Unit]
+Description=run backup script periodically
+Requisite=unmetered-internet.target
+After=unmetered-internet.target
+
+[Timer]
+OnCalendar=0/2:00:00
+RandomizedDelaySec=10min
+Persistent=true
+
+[Install]
+WantedBy=unmetered-internet.target
diff --git a/home/.config/systemd/user/dbusmonitor.service b/home/.config/systemd/user/dbusmonitor.service
new file mode 100644
index 0000000..0fdd942
--- /dev/null
+++ b/home/.config/systemd/user/dbusmonitor.service
@@ -0,0 +1,9 @@
+[Unit]
+Description=user service to listen to system dbus sevents
+
+[Service]
+Type=simple
+ExecStart=%h/.local/bin/dbusmonitor
+
+[Install]
+WantedBy=default.target \ No newline at end of file
diff --git a/home/.config/systemd/user/unmetered-internet.target b/home/.config/systemd/user/unmetered-internet.target
new file mode 100644
index 0000000..4e7907c
--- /dev/null
+++ b/home/.config/systemd/user/unmetered-internet.target
@@ -0,0 +1,2 @@
+[Unit]
+Description=current connection to the internet is not metered \ No newline at end of file
diff --git a/home/.local/bin/dbusmonitor b/home/.local/bin/dbusmonitor
new file mode 100755
index 0000000..f3b3d4a
--- /dev/null
+++ b/home/.local/bin/dbusmonitor
@@ -0,0 +1,27 @@
+#!/usr/bin/python3
+# dbus-monitor --system "path=/org/freedesktop/NetworkManager,interface=org.freedesktop.DBus.Properties,type=signal,member=PropertiesChanged"
+
+from gi.repository import GLib
+import pydbus
+loop = GLib.MainLoop()
+systemd = pydbus.SessionBus().get(".systemd1")
+nm = pydbus.SystemBus().get("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager")
+
+# https://developer.gnome.org/NetworkManager/stable/nm-dbus-types.html#NMMetered
+# 0 = The metered status is unknown
+# 1 = Metered, the value was statically set
+# 2 = Not metered, the value was statically set
+# 3 = Metered, the value was guessed
+# 4 = Not metered, the value was guessed
+def check_metered(changed):
+ value=nm.Metered
+ if (value == 2 or value == 4):
+ # start unit (replace any conflicting requests that might have been
+ # enqueued to systemctl but not yet run)
+ systemd.StartUnit("unmetered-internet.target", "replace")
+ else:
+ systemd.StopUnit("unmetered-internet.target", "replace")
+
+nm.PropertiesChanged.connect(check_metered)
+loop.run()
+
diff --git a/home/bin/backup b/home/bin/backup
index eb70d1d..b96fa33 100755
--- a/home/bin/backup
+++ b/home/bin/backup
@@ -1,17 +1,6 @@
#!/bin/bash
-set -e
+set -o errexit
-uuid=05b7770e-1f01-4b17-83de-ede0a04495a1
-instance="$(systemd-escape $uuid)"
-
-case "$1" in
- mount)
- sudo cryo "$uuid" mount
- ;;
- umount)
- sudo cryo "$uuid" umount
- ;;
- *)
- sudo systemctl start "cryo@$instance.service"
- ;;
-esac
+echo "backing up gdrive" >&2
+rclone sync --verbose ~/gdrive gdrive:laptop
+echo "backup successful" >&2
diff --git a/installpackages b/installpackages
index 0e73267..c6a4526 100755
--- a/installpackages
+++ b/installpackages
@@ -22,6 +22,7 @@ sudo apt install \
network-manager \
pandoc \
pass \
+ python3-pydbus \
ranger \
redshift \
restic \