-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflatpak-backup.sh
executable file
·119 lines (94 loc) · 3.28 KB
/
flatpak-backup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/bash
source <(curl -s https://codeberg.org/f1uff3h/scripts/raw/branch/main/bash_handlers.sh)
user_home=$(grep 1000 /etc/passwd | cut -d ":" -f6)
readonly env_vars=('NFS_SHARE')
readonly flatback_binary="${user_home}/bin/flatback.sh"
handle_root
handle_environment "${env_vars[@]}"
log info "Installing flatpak backup script"
mkdir -p "${flatback_binary%/*}"
echo "#!/bin/bash" >"${flatback_binary}"
curl https://codeberg.org/f1uff3h/scripts/raw/branch/main/bash_handlers.sh >>"${flatback_binary}"
cat <<-EOT >>"${flatback_binary}"
readonly env_vars=('NFS_SHARE')
user_home="${user_home}"
readonly user_home
handle_root
handle_environment "\${env_vars[@]}"
restore_switch=false
backup_dir="\${user_home}/.backup"
flatpak_backup_dir="\${backup_dir}/flatpak"
usage() {
echo "Usage: \${0} [-h|--help] [r|--restore]"
echo -e "\nOptions:"
echo -e "\n\t --help -h\tprint this message"
echo -e "\t --restore -r\trestore flatpaks"
echo -e "\nNote:"
echo "- running this script without any flags will initiate a backup of all installed flatpaks"
}
for arg in "\${@}"; do
case \$arg in
-r | --restore)
restore_switch=true
;;
-h | --help)
usage
exit 1
;;
*)
usage
log error "Unknown flag: \${arg}"
exit 1
;;
esac
done
log info "Prevent clobbering of \${backup_dir} directory"
mkdir -p "\${backup_dir}"
umount "\${backup_dir}" || log info "\${backup_dir} not mounted"
log info "Mounting \${NFS_SHARE} at \${backup_dir}"
mount -t nfs "\${NFS_SHARE}" "\${backup_dir}"
if [[ \$restore_switch = false ]]; then
log info "Backing up flatpak application list"
mkdir -p "\${flatpak_backup_dir}"
flatpak list --columns=application --app >"\${flatpak_backup_dir}/apps.list"
log info "Backing up flatpak application data"
rsync --archive --delete --human-readable --partial --progress --verbose --exclude="cache" "\${user_home}/.var/app" "\${flatpak_backup_dir}/"
log info "Backing up flatpak overrides"
rsync --archive --delete --human-readable --partial --progress --verbose "\${user_home}/.local/share/flatpak/overrides" "\${flatpak_backup_dir}/"
else
log info "Restoring flatpak application data"
rsync --archive --human-readable --progress --verbose "\${flatpak_backup_dir}/app" "\${user_home}/.var/"
log info "Restoring flatpak overrides"
rsync --archive --human-readable --progress --verbose "\${flatpak_backup_dir}/overrides" "\${user_home}/.local/share/flatpak/"
fi
log info "Unmounting \${backup_dir}"
umount "\${backup_dir}" || log info "\${backup_dir} not mounted"
log success "Completed successfully"
EOT
chmod 770 "${flatback_binary}"
chown 1000:1000 "${_}"
log info "Installing flatback service"
cat <<-EOT >/usr/lib/systemd/system/flatback.service
[Unit]
Description=Backup flatpak data
[Service]
Type=oneshot
Environment="NFS_SHARE=${NFS_SHARE}"
ExecStart="${flatback_binary}"
[Install]
WantedBy=multi-user.target
EOT
log info "Installing flatback timer"
cat <<-EOT >/usr/lib/systemd/system/flatback.timer
[Unit]
Description=Run flatback every hour
[Timer]
OnCalendar=hourly
Persistent=true
[Install]
WantedBy=timers.target
EOT
log info "Enabling and starting flatback.timer"
systemctl daemon-reload
systemctl enable --now flatback.timer
log success "Completed successfully"