1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/restic_ynh.git synced 2024-09-03 20:16:22 +02:00

fix: multi-instance

This commit is contained in:
Lionel Coupouchetty-Ramouchetty 2021-02-28 16:38:35 +01:00
parent e486b6adbe
commit 4e43fe1035
10 changed files with 67 additions and 38 deletions

View file

@ -16,6 +16,7 @@ A [Restic](https://restic.net/) package for YunoHost (heavily inspired by [the B
Restic is a backup tool that can make local and remote backups. Restic is a backup tool that can make local and remote backups.
This package uses restic to make backups to a sftp server. This package uses restic to make backups to a sftp server.
The package does not handle local backups yet but you can work around that by using the local sftp server as target server (see my comment [here](https://forum.yunohost.org/t/sauvegarde-yunohost-avec-restic/10275/33)).
## Usage ## Usage

View file

@ -5,17 +5,17 @@ set -e
### ###
# Fetch information from YNH settings # Fetch information from YNH settings
### ###
RESTIC_SERVER=$(yunohost app setting restic server) RESTIC_SERVER=$(yunohost app setting {{ app }} server)
RESTIC_SERVER_PORT=$(yunohost app setting restic port) RESTIC_SERVER_PORT=$(yunohost app setting {{ app }} port)
RESTIC_SERVER_USER=$(yunohost app setting restic ssh_user) RESTIC_SERVER_USER=$(yunohost app setting {{ app }} ssh_user)
RESTIC_PATH=$(yunohost app setting restic backup_path) RESTIC_PATH=$(yunohost app setting {{ app }} backup_path)
RESTIC_PASSWORD="$(yunohost app setting restic passphrase)" RESTIC_PASSWORD="$(yunohost app setting {{ app }} passphrase)"
RESTIC_REPOSITORY_BASE=sftp://$RESTIC_SERVER_USER@$RESTIC_SERVER:$RESTIC_SERVER_PORT/$RESTIC_PATH/ RESTIC_REPOSITORY_BASE=sftp://$RESTIC_SERVER_USER@$RESTIC_SERVER:$RESTIC_SERVER_PORT/$RESTIC_PATH/
RESTIC_COMMAND=/usr/local/bin/restic RESTIC_COMMAND=/usr/local/bin/{{ app }}
LOGFILE=/var/log/restic_backup.log LOGFILE=/var/log/restic_backup_{{ app }}.log
ERRFILE=/var/log/restic_backup.err ERRFILE=/var/log/restic_backup_{{ app }}.err
do_need_mount() { do_need_mount() {
work_dir="$1" work_dir="$1"

View file

@ -20,13 +20,13 @@ CHECK_READ_DATA=${1:-0}
# Check system part conf # Check system part conf
conf=$(yunohost app setting {{ app }} conf) conf=$(yunohost app setting {{ app }} conf)
if [ $conf -eq 1 ];then if [ $conf -eq 1 ];then
{{final_path}}/check_method auto_conf ${CHECK_READ_DATA} {{final_path}}/check_method_{{ app }} auto_conf ${CHECK_READ_DATA}
fi fi
# Check system data # Check system data
data=$(yunohost app setting {{ app }} data) data=$(yunohost app setting {{ app }} data)
if [ $data -eq 1 ];then if [ $data -eq 1 ];then
{{final_path}}/check_method auto_data ${CHECK_READ_DATA} {{final_path}}/check_method_{{ app }} auto_data ${CHECK_READ_DATA}
fi fi
# Check all apps independently # Check all apps independently
@ -40,7 +40,7 @@ for app in $(ls /etc/yunohost/apps/*/scripts/backup | cut -d / -f 5); do
fi fi
done done
if [ "$check_app" == "true" ];then if [ "$check_app" == "true" ];then
{{final_path}}/check_method auto_${app} ${CHECK_READ_DATA} {{final_path}}/check_method_{{ app }} auto_${app} ${CHECK_READ_DATA}
fi fi
done done
rm "$LOCK_FILE" rm "$LOCK_FILE"

View file

@ -2,15 +2,15 @@
set -e set -e
RESTIC_SERVER=$(yunohost app setting restic server) RESTIC_SERVER=$(yunohost app setting {{ app }} server)
RESTIC_SERVER_PORT=$(yunohost app setting restic port) RESTIC_SERVER_PORT=$(yunohost app setting {{ app }} port)
RESTIC_SERVER_USER=$(yunohost app setting restic ssh_user) RESTIC_SERVER_USER=$(yunohost app setting {{ app }} ssh_user)
RESTIC_PATH=$(yunohost app setting restic backup_path) RESTIC_PATH=$(yunohost app setting {{ app }} backup_path)
RESTIC_PASSWORD="$(yunohost app setting restic passphrase)" RESTIC_PASSWORD="$(yunohost app setting {{ app }} passphrase)"
RESTIC_REPOSITORY_BASE=sftp://$RESTIC_SERVER_USER@$RESTIC_SERVER:$RESTIC_SERVER_PORT/$RESTIC_PATH/ RESTIC_REPOSITORY_BASE=sftp://$RESTIC_SERVER_USER@$RESTIC_SERVER:$RESTIC_SERVER_PORT/$RESTIC_PATH/
RESTIC_COMMAND=/usr/local/bin/restic RESTIC_COMMAND=/usr/local/bin/{{ app }}
do_check() { do_check() {
@ -18,8 +18,8 @@ do_check() {
local check_read_data="$2" local check_read_data="$2"
export RESTIC_PASSWORD export RESTIC_PASSWORD
export RESTIC_REPOSITORY=${RESTIC_REPOSITORY_BASE}/$name export RESTIC_REPOSITORY=${RESTIC_REPOSITORY_BASE}/$name
LOGFILE=/var/log/restic_check.log LOGFILE=/var/log/restic_check_{{ app }}.log
ERRFILE=/var/log/restic_check.err ERRFILE=/var/log/restic_check_{{ app }}.err
current_date=$(date +"%d_%m_%y_%H:%M") current_date=$(date +"%d_%m_%y_%H:%M")
echo -e "\n==============\n${current_date}\n==============\n" | tee -a ${LOGFILE} | tee -a ${ERRFILE} echo -e "\n==============\n${current_date}\n==============\n" | tee -a ${LOGFILE} | tee -a ${ERRFILE}
if [ "$check_read_data" -eq "1" ];then if [ "$check_read_data" -eq "1" ];then

View file

@ -5,7 +5,7 @@ After=network.target
[Service] [Service]
Type=oneshot Type=oneshot
ExecStart=/usr/local/bin/backup-with-__APP__ ExecStart=/usr/local/bin/backup-with-__APP__
ExecStartPost=/opt/yunohost/__APP__/restic_log ExecStartPost=/opt/yunohost/__APP__/restic_log___APP__
User=root User=root
Group=root Group=root

View file

@ -6,7 +6,7 @@
"en": "Backup your server with restic.", "en": "Backup your server with restic.",
"fr": "Sauvegardez votre serveur avec restic." "fr": "Sauvegardez votre serveur avec restic."
}, },
"version": "0.12.0~ynh1", "version": "0.12.0~ynh2",
"url": "https://restic.net/", "url": "https://restic.net/",
"license": "BSD-2-Clause", "license": "BSD-2-Clause",
"maintainer": { "maintainer": {

View file

@ -32,8 +32,8 @@ install_restic () {
sum=$(sha256sum /tmp/restic.bz2 | awk '{print $1}') sum=$(sha256sum /tmp/restic.bz2 | awk '{print $1}')
if [ "$sum" == "$expected_sum" ];then if [ "$sum" == "$expected_sum" ];then
pkill restic || true pkill restic || true
bunzip2 /tmp/restic.bz2 -f -c > /usr/local/bin/restic bunzip2 /tmp/restic.bz2 -f -c > /usr/local/bin/${app}
chmod +x /usr/local/bin/restic chmod +x /usr/local/bin/${app}
else else
ynh_die --message="\nDownloaded file does not match expected sha256 sum, aborting" ynh_die --message="\nDownloaded file does not match expected sha256 sum, aborting"
fi fi

View file

@ -48,14 +48,14 @@ mkdir -p /usr/share/yunohost/backup_method
#================================================= #=================================================
ynh_script_progression --message="Setting up backup methods" ynh_script_progression --message="Setting up backup methods"
ynh_configure backup_method "/etc/yunohost/hooks.d/backup_method/05-${app}_app" ynh_configure backup_method "/etc/yunohost/hooks.d/backup_method/05-${app}_app"
ynh_configure check_method "${final_path}/check_method" ynh_configure check_method "${final_path}/check_method_${app}"
#================================================= #=================================================
# SETUP LOG SCRIPT # SETUP LOG SCRIPT
#================================================= #=================================================
ynh_script_progression --message="Setting up log script" ynh_script_progression --message="Setting up log script"
ynh_configure restic_log "${final_path}/restic_log" ynh_configure restic_log "${final_path}/restic_log_${app}"
chmod u+x "${final_path}/restic_log" chmod u+x "${final_path}/restic_log_${app}"
#================================================= #=================================================
# CONFIGURE CRON # CONFIGURE CRON
@ -65,7 +65,7 @@ ynh_configure backup-with-restic "/usr/local/bin/backup-with-${app}"
ynh_configure check-restic "${final_path}/check-${app}" ynh_configure check-restic "${final_path}/check-${app}"
chmod u+x "/usr/local/bin/backup-with-${app}" chmod u+x "/usr/local/bin/backup-with-${app}"
chmod u+x "${final_path}/check-${app}" chmod u+x "${final_path}/check-${app}"
chmod u+x "${final_path}/check_method" chmod u+x "${final_path}/check_method_${app}"
ynh_add_systemd_config --service=${app} --template=systemd.service ynh_add_systemd_config --service=${app} --template=systemd.service
ynh_add_systemd_config --service=${app}_check --template=systemd_check.service ynh_add_systemd_config --service=${app}_check --template=systemd_check.service
ynh_add_systemd_config --service=${app}_check_read_data --template=systemd_check_read_data.service ynh_add_systemd_config --service=${app}_check_read_data --template=systemd_check_read_data.service
@ -92,8 +92,9 @@ test -f $private_key || ssh-keygen -q -t ed25519 -N "" -f $private_key
#================================================= #=================================================
# GENERATE SSH CONFIG # GENERATE SSH CONFIG
#================================================= #=================================================
ynh_script_progression --message="Generating ssh config for ${server}" ynh_script_progression --message="Generating ssh config for ${app} server ${server}"
grep -q "${server}" /root/.ssh/config 2>/dev/null || cat << EOCONF >> ~/.ssh/config grep -q "${app}" /root/.ssh/config 2>/dev/null || cat << EOCONF >> ~/.ssh/config
# begin $app ssh config
Host ${server} Host ${server}
Hostname ${server} Hostname ${server}
Port ${port} Port ${port}
@ -101,6 +102,7 @@ Host ${server}
IdentityFile ${private_key} IdentityFile ${private_key}
StrictHostKeyChecking no StrictHostKeyChecking no
UserKnownHostsFile /dev/null UserKnownHostsFile /dev/null
# end $app ssh config
EOCONF EOCONF
#================================================= #=================================================
@ -119,7 +121,7 @@ ynh_print_OFF
message="You should now allow the following public key for user ${ssh_user} on server ${server}: message="You should now allow the following public key for user ${ssh_user} on server ${server}:
$(cat ${private_key}.pub) $(cat ${private_key}.pub)
Do so by running this command on ${server} with user ${ssh_user}: Do so by running those commands on ${server} with user ${ssh_user}:
mkdir ~/.ssh 2>/dev/null mkdir ~/.ssh 2>/dev/null
touch ~/.ssh/authorized_keys touch ~/.ssh/authorized_keys
@ -130,7 +132,7 @@ EOPKEY
$(if [ "$backup_path" != "./" ];then echo "Also make sure ${backup_path} exists and is writable by ${ssh_user}";fi) $(if [ "$backup_path" != "./" ];then echo "Also make sure ${backup_path} exists and is writable by ${ssh_user}";fi)
If you facing an issue or want to improve this app, please open a new issue in this project: https://github.com/YunoHost-Apps/restic_ynh" If you're facing an issue or want to improve this app, please open a new issue in this project: https://github.com/YunoHost-Apps/restic_ynh"
ynh_send_readme_to_admin "$message" "root" ynh_send_readme_to_admin "$message" "root"
ynh_print_ON ynh_print_ON

View file

@ -36,7 +36,7 @@ ynh_secure_remove "/etc/systemd/system/${app}_check.timer"
ynh_secure_remove "/etc/systemd/system/${app}_check_read_data.timer" ynh_secure_remove "/etc/systemd/system/${app}_check_read_data.timer"
ynh_secure_remove "/usr/local/bin/backup-with-${app}" ynh_secure_remove "/usr/local/bin/backup-with-${app}"
ynh_secure_remove "/etc/yunohost/hooks.d/backup_method/05-${app}_app" ynh_secure_remove "/etc/yunohost/hooks.d/backup_method/05-${app}_app"
ynh_secure_remove "${final_path}/check_method" ynh_secure_remove "${final_path}/check_method_${app}"
ynh_secure_remove "${final_path}/check-${app}" ynh_secure_remove "${final_path}/check-${app}"
ynh_secure_remove "${final_path}" ynh_secure_remove "${final_path}"
@ -44,4 +44,4 @@ ynh_secure_remove "${final_path}"
# REMOVE SSH CONFIG # REMOVE SSH CONFIG
#================================================= #=================================================
ynh_script_progression --message="Removing ssh config" --last ynh_script_progression --message="Removing ssh config" --last
sed -e "/Host ${server}/,+6d" /root/.ssh/config -i || true sed -e "/begin ${app}/,/end ${app}/{/.*/d}" /root/.ssh/config -i || true

View file

@ -48,7 +48,7 @@ ynh_clean_setup () {
ynh_abort_if_errors ynh_abort_if_errors
if grep "restic.timer" /etc/yunohost/services.yml > /dev/null ; then if grep "${app}.timer" /etc/yunohost/services.yml > /dev/null ; then
yunohost service remove $app.timer yunohost service remove $app.timer
systemctl --quiet enable $app.timer systemctl --quiet enable $app.timer
systemctl start $app.timer systemctl start $app.timer
@ -72,14 +72,14 @@ mkdir -p /usr/share/yunohost/backup_method
#================================================= #=================================================
ynh_script_progression --message="Setting up backup methods" ynh_script_progression --message="Setting up backup methods"
ynh_configure backup_method "/etc/yunohost/hooks.d/backup_method/05-${app}_app" ynh_configure backup_method "/etc/yunohost/hooks.d/backup_method/05-${app}_app"
ynh_configure check_method "${final_path}/check_method" ynh_configure check_method "${final_path}/check_method_${app}"
#================================================= #=================================================
# SETUP LOG SCRIPT # SETUP LOG SCRIPT
#================================================= #=================================================
ynh_script_progression --message="Setting up log script" ynh_script_progression --message="Setting up log script"
ynh_configure restic_log "${final_path}/restic_log" ynh_configure restic_log "${final_path}/restic_log_${app}"
chmod u+x "${final_path}/restic_log" chmod u+x "${final_path}/restic_log_${app}"
#================================================= #=================================================
# CONFIGURE CRON # CONFIGURE CRON
@ -89,7 +89,7 @@ ynh_configure backup-with-restic "/usr/local/bin/backup-with-${app}"
ynh_configure check-restic "${final_path}/check-${app}" ynh_configure check-restic "${final_path}/check-${app}"
chmod u+x "/usr/local/bin/backup-with-${app}" chmod u+x "/usr/local/bin/backup-with-${app}"
chmod u+x "${final_path}/check-${app}" chmod u+x "${final_path}/check-${app}"
chmod u+x "${final_path}/check_method" chmod u+x "${final_path}/check_method_${app}"
ynh_add_systemd_config --service=${app} --template=systemd.service ynh_add_systemd_config --service=${app} --template=systemd.service
ynh_add_systemd_config --service=${app}_check --template=systemd_check.service ynh_add_systemd_config --service=${app}_check --template=systemd_check.service
ynh_add_systemd_config --service=${app}_check_read_data --template=systemd_check_read_data.service ynh_add_systemd_config --service=${app}_check_read_data --template=systemd_check_read_data.service
@ -107,3 +107,29 @@ systemctl start ${app}_check.timer
systemctl start ${app}_check_read_data.timer systemctl start ${app}_check_read_data.timer
ynh_script_progression --message="End of upgrade process" --last ynh_script_progression --message="End of upgrade process" --last
#=================================================
# UPGRADE SSH CONFIG
#=================================================
# old versions did not have delimiters in /root/.ssh/config
# making removal in multi-instance cases break the remaining
# instances.
# So we need to add the delimiters if they are missing
grep -q "begin ${app}" /root/.ssh/config
missing_delimiters="$?"
if [ "$missing_delimiters" -eq 1 ];then
# did not find delimiters so removing old configuration
sed -e "/Host ${server}/,+6d" /root/.ssh/config -i || true
cat << EOCONF >> ~/.ssh/config
# begin $app ssh config
Host ${server}
Hostname ${server}
Port ${port}
User ${ssh_user}
IdentityFile ${private_key}
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
# end $app ssh config
EOCONF
fi