diff --git a/locales/es_ES b/locales/es_ES new file mode 100644 index 0000000..363a333 --- /dev/null +++ b/locales/es_ES @@ -0,0 +1,18 @@ +#!/bin/bash +# shellcheck disable=SC2034 +first_be_sure="Primero asegurate de que el disco USB donde guardar la copia está conectado" +no_config="No hay archivo de configuración. Vamos a crearlo. Elige el dispositivo USB que iniciará la copia" +devices="Dispositivos" +select_path="Elige el directorio en el disco (debe estar montado) para guardar la copia" +select_folder="Elige la carpeta a la que hacer las copias" +encrypt="¿Quieres encriptar las copias? (Recomendado)" +initializing="Inicializando el repository" +backing_up="Realizando la copia" +to="en" +there_was_error="Hubo un error" +backup_succeeded="Copia finalizada sin errores.\n Resultado: " +doing_backup="haciendo la copia" +initializing_low="inicializando el repositorio" +waiting="Esperando otros 3 segundos" +waited="He esperado por 60 segundos para que la carpeta de destino estuviera disponible sin exito. Comprueba la configuración y que la carpeta de destino existe." +another_copy="Hay otra copia ejecutandose" diff --git a/simple_backup.sh b/simple_backup.sh new file mode 100755 index 0000000..1eaa15a --- /dev/null +++ b/simple_backup.sh @@ -0,0 +1,116 @@ +#!/bin/bash +first_be_sure="First be sure that the USB device to backup to is connected now" +no_config="There is no configuration file. Let's create it. Choose the USB device that will trigger the backup" +devices="Devices" +select_path="Select the path in the disk (must be mounted) to place the backup" +select_folder="Select the folder to backup" +encrypt="Do you want to encrypt the backups? (Recommended)" +initializing="Initializing repository" +backing_up="Backing up" +to="to" +there_was_error="There was an error" +backup_succeeded="Backup finished without errors.\n Result: " +doing_backup="doing the backup" +initializing_low="initializing repository" +waiting="Waiting another 3 seconds" +waited="Waited for 60 seconds for the destination to be available without sucess. Check your configuration and that the destination folder exists" +another_copy="There is another copy running" +function configure() { + zenity --info --text "${first_be_sure}" + # shellcheck disable=SC2046 + device=$(zenity --list --text="${no_config}" --column="${devices}" --separator='\n' $(lsusb | sed 's/.* ID //g' | sed 's/ /_/g')) + echo "device='${device}'" > "${HOME}/.config/simple_backup.conf" + zenity --info --text "${select_path}" + destination=$(zenity --file-selection --directory) + echo "destination='${destination}'" >> "${HOME}/.config/simple_backup.conf" + zenity --info --text "${select_folder}" + folder=$(zenity --file-selection --directory) + echo "folder='${folder}'" >> "${HOME}/.config/simple_backup.conf" + if zenity --question --text "${encrypt}"; then + password=$(zenity --password) + echo "${password}" > "${HOME}/.config/simple_backup.password" + chmod go-rwx "${HOME}/.config/simple_backup.password" + else + rm "${HOME}/.config/simple_backup.password" + fi +} +script_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +script_name=$(basename "${BASH_SOURCE[0]}") + +# shellcheck disable=SC2001 +locale=$(echo "${LANG}" | sed 's/\..*$//') +if [ -e "${script_dir}/locales/${locale}" ]; then + # shellcheck disable=SC1090 + source "${script_dir}/locales/${locale}" +fi +if [ ! -r "${HOME}/.config/simple_backup.conf" ]; then + configure +fi + +if [ $(pgrep -f -c "${script_name}") -gt 1 ]; then + echo "${another_copy}" + exit 3 +fi + +# shellcheck disable=SC1091 +source "${HOME}/.config/simple_backup.conf" + +if [ -r "${HOME}/.config/simple_backup.password" ]; then + password_file=(--password-file "${HOME}/.config/simple_backup.password") +else + password_file=() +fi + +mkdir -p "${HOME}/.logs" + +vendor="${device:0:4}" +product_id="${device:5:4}" +if [ ! -e /etc/udev/rules.d/10_simple_backup.rules ]; then + sudo touch /etc/udev/rules.d/10_simple_backup.rules + echo "SUBSYSTEM==\"usb\" ACTION==\"add\" ATTRS{idVendor}==\"${vendor}\" ATTRS{idProduct}==\"${product_id}\" ATTR{partscan}==\"1\" TAG+=\"systemd\" ENV{SYSTEMD_USER_WANTS}=\"simple_backup@\$devnode.service\"" | sudo tee /etc/udev/rules.d/10_simple_backup.rules +fi +if [ ! -e "${HOME}/.config/systemd/user/simple_backup@.service" ]; then + mkdir -p "${HOME}/.config/systemd/user/" + cat << EOF > "${HOME}/.config/systemd/user/simple_backup@.service" +[Unit] +Description=Simple backup USB serial device at %I + +[Service] +Type=simple +ExecStart=bash -c 'sleep \$((1 + \$RANDOM % 10)); ${script_dir}/${script_name} %IEOF' +EOF + systemctl --user daemon-reload + systemctl --user enable simple_backup@.service +fi + +if [ ! -e "${destination}" ]; then + count=0 + while [ ! -e "${destination}" ] + do + sleep 3 + echo "${waiting}" + count=$((count + 1)) + if [ ${count} -gt 20 ]; then + zenity --error --text "${waited}" + exit 2 + fi + done +fi +if [ ! -e "${destination}/config" ]; then + echo "${initializing} '${destination}'..." + if ! restic init "${password_file[@]}" -r "${destination}"; then + zenity --error --text "${there_was_error} ${return_code} ${initializing_low}" + exit 1 + fi +fi +echo "${backing_up} '${folder}' ${to} '${destination}'..." +current_date=$(date +%Y.%m.%d_%H_%M_%S) +restic backup "${password_file[@]}" -r "${destination}" "${folder}" --json > "${HOME}/.logs/simple_backup_${current_date}.json" +return_code=${?} +cp "${HOME}/.logs/simple_backup_${current_date}.json" "${HOME}/.logs/simple_backup_last.json" -f +if [ "${return_code}" != "0" ]; then + zenity --error --text "${there_was_error} ${return_code} ${doing_backup}" +else + result=$(tail -n1 "${HOME}/.logs/simple_backup_${current_date}.json" | jq '.') + zenity --info --text "${backup_succeeded} ${result}" +fi