1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/archivist_ynh.git synced 2024-09-03 18:15:55 +02:00

Add actions and config-panel

This commit is contained in:
Maniack Crudelis 2018-07-21 19:42:17 +02:00
parent 8ac40c58b1
commit ee8fd67827
5 changed files with 318 additions and 0 deletions

22
actions.json Normal file
View file

@ -0,0 +1,22 @@
[{
"id": "force_backup",
"name": "Create a new backup",
"command": "/bin/bash scripts/actions/force_backup",
"user": "root",
"accepted_return_codes": [0],
"description": {
"en": "Run Archivist to create a new backup.",
"fr": "Lance Archivist pour créer un nouveau backup."
}
},
{
"id": "clean_backups",
"name": "Clean all previous backup files",
"command": "/bin/bash scripts/actions/clean_backups",
"user": "root",
"accepted_return_codes": [0],
"description": {
"en": "Remove all previous backup files made by Archivist.",
"fr": "Supprime tout les précédents backups créés par Archivist."
}
}]

59
config_panel.json Normal file
View file

@ -0,0 +1,59 @@
{
"name": "Archivist configuration panel",
"version": "0.1",
"panel": [{
"name": "Archivist configuration",
"id": "main",
"sections": [{
"name": "Encryption",
"id": "encryption",
"options": [{
"name": "Do you want to encrypt your backups ?",
"id": "encrypt",
"type": "bool",
"default": true
}, {
"name": "Set the password for encryption",
"help": "A password is needed if encryption is activated.",
"id": "encryption_pwd",
"type": "password",
"optional": true
}]
},
{
"name": "Backup",
"id": "backup_types",
"options": [{
"name": "Would you like to backup your YunoHost core ?",
"id": "core_backup",
"type": "bool",
"default": true
}, {
"name": "Would you like to backup your apps ?",
"help": "WARNING: Changing this value will either remove backup for all apps or add all current apps to the backup.",
"id": "apps_backup",
"type": "bool",
"default": true
}]
},
{
"name": "Backup options",
"id": "backup_options",
"options": [{
"name": "Choose the frequency of your backups ?",
"help": "We can't use a choices field for now. In the meantime please choose between one of this values:<br>Daily, Each 3 days, Weekly, Biweekly, Monthly.",
"id": "frequency",
"type": "text",
"//": "\"choices\" : [\"Daily\", \"Each 3 days\", \"Weekly\", \"Biweekly\", \"Monthly\"]",
"default" : "Weekly"
}, {
"name": "Max size for each backup in Mb",
"help": "Specify the max size of each backup for the following option file_to_backup.</br>This option is a soft limit, that means the script will try to limit each backup to this max size if it can.</br>But there's 2 limitations, for a single directory, it can't makes more than one backup file, even if the files in this directory exceed this maximum size.</br>And, if there's some files in a directory, next to subdirectories, it'll make only one backup for this files.</br>So this limit will be applied to split the backup by its subdirectories to avoid to have only one big backup.",
"id": "max_size",
"type": "number",
"default": 500
}]
}]
}
]
}

44
scripts/actions/clean_backups Executable file
View file

@ -0,0 +1,44 @@
#!/bin/bash
#=================================================
# GENERIC STARTING
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source scripts/_common.sh
source /usr/share/yunohost/helpers
#=================================================
# RETRIEVE ARGUMENTS
#=================================================
app=$YNH_APP_ID
final_path=$(ynh_app_setting_get $app final_path)
#=================================================
# CHECK IF ARGUMENTS ARE CORRECT
#=================================================
#=================================================
# CHECK IF AN ACTION HAS TO BE DONE
#=================================================
#=================================================
# SPECIFIC ACTION
#=================================================
# CLEAN ALL BACKUP FILES
#=================================================
# Get the backup directory from the config file
backup_dir="$(grep "^backup_dir=" "$final_path/Backup_list.conf" | cut -d= -f2)"
while read directory <&3
do
if [ -n "$directory" ]
then
ynh_print_info "Clean backup directory $directory" >&2
ynh_secure_remove "$directory"
fi
done 3<<< $(find "$backup_dir" -maxdepth 1 -mindepth 1 -type d)

34
scripts/actions/force_backup Executable file
View file

@ -0,0 +1,34 @@
#!/bin/bash
#=================================================
# GENERIC STARTING
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source scripts/_common.sh
source /usr/share/yunohost/helpers
#=================================================
# RETRIEVE ARGUMENTS
#=================================================
app=$YNH_APP_ID
final_path=$(ynh_app_setting_get $app final_path)
#=================================================
# CHECK IF ARGUMENTS ARE CORRECT
#=================================================
#=================================================
# CHECK IF AN ACTION HAS TO BE DONE
#=================================================
#=================================================
# SPECIFIC ACTION
#=================================================
# FORCE A NEW BACKUP
#=================================================
ynh_exec_warn nice -n10 $final_path/archivist.sh

159
scripts/config Normal file
View file

@ -0,0 +1,159 @@
#!/bin/bash
#=================================================
# GENERIC STARTING
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# RETRIEVE ARGUMENTS
#=================================================
app=$YNH_APP_ID
final_path=$(ynh_app_setting_get $app final_path)
#=================================================
# SPECIFIC CODE
#=================================================
# DECLARE GENERIC FUNCTION
#=================================================
config_file="$final_path/Backup_list.conf"
passkey="$final_path/passkey"
get_config_value() {
option_name="$1"
# Get the value of this option in the config file
grep "^$option_name=" "$config_file" | cut -d= -f2
}
#=================================================
# LOAD VALUES
#=================================================
# Load the real value from the app config or elsewhere.
# Then get the value from the form.
# If the form has a value for a variable, take the value from the form,
# Otherwise, keep the value from the app config.
# Encryption
old_encrypt="$(get_config_value encrypt)"
encrypt="${YNH_CONFIG_MAIN_ENCRYPTION_ENCRYPT:-$old_encrypt}"
# Encryption password
ynh_print_OFF
old_encrypt_password="$(cat $passkey)"
encrypt_password="${YNH_CONFIG_MAIN_ENCRYPTION_ENCRYPTION_PWD:-$old_encrypt_password}"
ynh_print_ON
# ynh_core_backup
old_ynh_core_backup="$(get_config_value ynh_core_backup)"
ynh_core_backup="${YNH_CONFIG_MAIN_BACKUP_TYPES_CORE_BACKUP:-$old_ynh_core_backup}"
# ynh_app_backup
if [ -n "$(get_config_value ynh_app_backup)" ]
then
old_ynh_app_backup="true"
else
old_ynh_app_backup="false"
fi
ynh_app_backup="${YNH_CONFIG_MAIN_BACKUP_TYPES_APPS_BACKUP:-$old_ynh_app_backup}"
# Frequency
old_frequency="$(grep "^frequency: " "/etc/yunohost/apps/$app/settings.yml" | cut -d' ' -f2)"
frequency="${YNH_CONFIG_MAIN_BACKUP_OPTIONS_FREQUENCY:-$old_frequency}"
# Max size
old_max_size="$(get_config_value max_size)"
max_size="${YNH_CONFIG_MAIN_BACKUP_OPTIONS_MAX_SIZE:-$old_max_size}"
#=================================================
# SHOW_CONFIG FUNCTION FOR 'SHOW' COMMAND
#=================================================
show_config() {
# here you are supposed to read some config file/database/other then print the values
# echo "YNH_CONFIG_${PANEL_ID}_${SECTION_ID}_${OPTION_ID}=value"
echo "YNH_CONFIG_MAIN_ENCRYPTION_ENCRYPT=$encrypt"
echo "YNH_CONFIG_MAIN_ENCRYPTION_ENCRYPTION_PWD="
echo "YNH_CONFIG_MAIN_BACKUP_TYPES_CORE_BACKUP=$ynh_core_backup"
echo "YNH_CONFIG_MAIN_BACKUP_TYPES_APPS_BACKUP=$ynh_app_backup"
echo "YNH_CONFIG_MAIN_BACKUP_OPTIONS_FREQUENCY=$frequency"
echo "YNH_CONFIG_MAIN_BACKUP_OPTIONS_MAX_SIZE=$max_size"
}
#=================================================
# MODIFY THE CONFIGURATION
#=================================================
apply_config() {
# Change the password if needed
if [ "$encrypt" = "true" ]; then
ynh_print_OFF
test -n "$encrypt_password" || ynh_die "The password for encryption can't be empty if you choose to enable encryption."
ynh_print_ON
# Replace the password by the previous one
passkey="$final_path/passkey"
ynh_print_OFF; echo "$encrypt_password" > "$passkey"; ynh_print_ON
chmod 400 "$passkey"
ynh_replace_string "^cryptpass=.*" "cryptpass=$passkey" "$config_file"
fi
# Change encrypt in the config file
ynh_replace_string "^encrypt=.*" "encrypt=$encrypt" "$config_file"
# Change ynh_core_backup in the config file
ynh_replace_string "^ynh_core_backup=.*" "ynh_core_backup=$ynh_core_backup" "$config_file"
# Change ynh_app_backup in the config file
if [ "$ynh_app_backup" = "true" ] && [ "$old_ynh_app_backup" = "false" ]
then
# If ynh_app_backup changed from false to true.
# Add all current applications to the backup
while read backup_app
do
ynh_print_info "Add a backup for the app $backup_app." >&2
ynh_replace_string "^ynh_app_backup=$" "ynh_app_backup=$backup_app\n&" "$config_file"
done <<< "$(yunohost app list -i | grep id: | sed 's/.*id: //')"
else
# Remove all app currently backup
# By deleting all line starting by 'ynh_app_backup=' and having something after '='
sed -i "/^ynh_app_backup=.\+$/d" "$config_file"
fi
# Change frequency in the cron file and store the value into the settings
ynh_app_setting_set $app frequency "$frequency"
if [ "$frequency" = "Daily" ]; then
cron_freq="0 2 * * *"
run_freq="every day"
elif [ "$frequency" = "Each 3 days" ]; then
cron_freq="0 2 */3 * *"
run_freq="each 3 days"
elif [ "$frequency" = "Weekly" ]; then
cron_freq="0 2 * * 0"
run_freq="once a week on sunday"
elif [ "$frequency" = "Biweekly" ]; then
cron_freq="0 2 * * 0/2"
run_freq="one sunday out of two"
else # Monthly
cron_freq="0 2 1 * *"
run_freq="once a month on the first sunday"
fi
ynh_replace_string ".* root" "$cron_freq root" /etc/cron.d/$app
# Change max_size in the config file
ynh_replace_string "^max_size=.*" "max_size=$max_size" "$config_file"
}
#=================================================
# GENERIC FINALIZATION
#=================================================
# SELECT THE ACTION FOLLOWING THE GIVEN ARGUMENT
#=================================================
case $1 in
show) show_config;;
apply) apply_config;;
esac