Erreur32 hat die Gist bearbeitet 6 months ago. Zu Änderung gehen
Keine Änderungen
Erreur32 hat die Gist bearbeitet 6 months ago. Zu Änderung gehen
Keine Änderungen
Erreur32 hat die Gist bearbeitet 1 year ago. Zu Änderung gehen
1 file changed, 235 insertions
rsync_backup_all_direction_cli.sh(Datei erstellt)
| @@ -0,0 +1,235 @@ | |||
| 1 | + | #!/bin/bash | |
| 2 | + | # rsync_backup_alldirection_cli.sh | |
| 3 | + | ||
| 4 | + | # Variables de base | |
| 5 | + | UTILISATEUR="debian" | |
| 6 | + | IP_DISTANTE="666.66.66.66" | |
| 7 | + | PORT_SSH="22" | |
| 8 | + | LOCAL_BASE_DIR="/home" # Répertoire de base local | |
| 9 | + | MOTDEPASSE="YOURPASS" # Votre mot de passe | |
| 10 | + | ||
| 11 | + | # Fonction d'affichage de la progression avec animation et human-readable (-h) | |
| 12 | + | afficher_progression() { | |
| 13 | + | echo -e "\e[1;32m🚀 En cours...\e[0m" | |
| 14 | + | sshpass -p "$MOTDEPASSE" rsync -avzh --progress -e "ssh -p $PORT_SSH" "$1" "$2" | pv -l -N "Transfert" | |
| 15 | + | } | |
| 16 | + | ||
| 17 | + | # =============================== | |
| 18 | + | # Fonctions pour l'envoi | |
| 19 | + | # =============================== | |
| 20 | + | ||
| 21 | + | interactive_send_file() { | |
| 22 | + | echo -e "\e[1;32m[SEND] Liste des fichiers locaux dans $LOCAL_BASE_DIR :\e[0m" | |
| 23 | + | IFS=$'\n' | |
| 24 | + | local_files=( $(find "$LOCAL_BASE_DIR" -maxdepth 1 -type f -printf '%p (%kK)\n') ) | |
| 25 | + | unset IFS | |
| 26 | + | if [ ${#local_files[@]} -eq 0 ]; then | |
| 27 | + | echo -e "\e[1;31m❌ Aucun fichier trouvé dans $LOCAL_BASE_DIR\e[0m" | |
| 28 | + | exit 1 | |
| 29 | + | fi | |
| 30 | + | select lfile in "${local_files[@]}"; do | |
| 31 | + | if [ -n "$lfile" ]; then | |
| 32 | + | local_file=$(echo "$lfile" | awk '{print $1}') | |
| 33 | + | break | |
| 34 | + | else | |
| 35 | + | echo -e "\e[1;31m❌ Choix invalide, réessayez.\e[0m" | |
| 36 | + | fi | |
| 37 | + | done | |
| 38 | + | ||
| 39 | + | echo -e "\e[1;32m[SEND] Liste des répertoires distants (dans /home) :\e[0m" | |
| 40 | + | remote_dirs=$(sshpass -p "$MOTDEPASSE" ssh -p $PORT_SSH $UTILISATEUR@$IP_DISTANTE "find /home -maxdepth 1 -type d") | |
| 41 | + | select rdir in $remote_dirs; do | |
| 42 | + | if [ -n "$rdir" ]; then | |
| 43 | + | remote_dest="$rdir" | |
| 44 | + | break | |
| 45 | + | else | |
| 46 | + | echo -e "\e[1;31m❌ Choix invalide, réessayez.\e[0m" | |
| 47 | + | fi | |
| 48 | + | done | |
| 49 | + | ||
| 50 | + | echo -e "\e[1;32m[SEND] Envoi du fichier '$local_file' vers $UTILISATEUR@$IP_DISTANTE:$remote_dest...\e[0m" | |
| 51 | + | afficher_progression "$local_file" "$UTILISATEUR@$IP_DISTANTE:$remote_dest" | |
| 52 | + | } | |
| 53 | + | ||
| 54 | + | interactive_send_directory() { | |
| 55 | + | read -p "Spécifiez le chemin complet du répertoire local à envoyer : " local_dir | |
| 56 | + | if [ ! -d "$local_dir" ]; then | |
| 57 | + | echo -e "\e[1;31m❌ Répertoire non trouvé.\e[0m" | |
| 58 | + | exit 1 | |
| 59 | + | fi | |
| 60 | + | echo -e "\e[1;32m[SEND] Liste des répertoires distants (dans /home) :\e[0m" | |
| 61 | + | remote_dirs=$(sshpass -p "$MOTDEPASSE" ssh -p $PORT_SSH $UTILISATEUR@$IP_DISTANTE "find /home -maxdepth 1 -type d") | |
| 62 | + | select rdir in $remote_dirs; do | |
| 63 | + | if [ -n "$rdir" ]; then | |
| 64 | + | remote_dest="$rdir" | |
| 65 | + | break | |
| 66 | + | else | |
| 67 | + | echo -e "\e[1;31m❌ Choix invalide, réessayez.\e[0m" | |
| 68 | + | fi | |
| 69 | + | done | |
| 70 | + | echo -e "\e[1;32m[SEND] Envoi du répertoire '$local_dir' vers $UTILISATEUR@$IP_DISTANTE:$remote_dest...\e[0m" | |
| 71 | + | afficher_progression "$local_dir" "$UTILISATEUR@$IP_DISTANTE:$remote_dest" | |
| 72 | + | } | |
| 73 | + | ||
| 74 | + | # =============================== | |
| 75 | + | # Fonctions pour la récupération | |
| 76 | + | # =============================== | |
| 77 | + | ||
| 78 | + | interactive_receive_file() { | |
| 79 | + | echo -e "\e[1;34m[RECEIVE] Liste des répertoires distants (dans /home) :\e[0m" | |
| 80 | + | remote_dirs=$(sshpass -p "$MOTDEPASSE" ssh -p $PORT_SSH $UTILISATEUR@$IP_DISTANTE "find /home -maxdepth 1 -type d") | |
| 81 | + | select rdir in $remote_dirs; do | |
| 82 | + | if [ -n "$rdir" ]; then | |
| 83 | + | chosen_remote_dir="$rdir" | |
| 84 | + | break | |
| 85 | + | else | |
| 86 | + | echo -e "\e[1;31m❌ Choix invalide, réessayez.\e[0m" | |
| 87 | + | fi | |
| 88 | + | done | |
| 89 | + | ||
| 90 | + | echo -e "\e[1;34m[RECEIVE] Liste des fichiers dans '$chosen_remote_dir' (human readable) :\e[0m" | |
| 91 | + | IFS=$'\n' | |
| 92 | + | remote_files=( $(sshpass -p "$MOTDEPASSE" ssh -p $PORT_SSH $UTILISATEUR@$IP_DISTANTE "find $chosen_remote_dir -maxdepth 1 -type f -printf '%p (%kK)\n'") ) | |
| 93 | + | unset IFS | |
| 94 | + | if [ ${#remote_files[@]} -eq 0 ]; then | |
| 95 | + | echo -e "\e[1;31m❌ Aucun fichier trouvé dans $chosen_remote_dir\e[0m" | |
| 96 | + | exit 1 | |
| 97 | + | fi | |
| 98 | + | select rfile in "${remote_files[@]}"; do | |
| 99 | + | if [ -n "$rfile" ]; then | |
| 100 | + | remote_file=$(echo "$rfile" | awk '{print $1}') | |
| 101 | + | break | |
| 102 | + | else | |
| 103 | + | echo -e "\e[1;31m❌ Choix invalide, réessayez.\e[0m" | |
| 104 | + | fi | |
| 105 | + | done | |
| 106 | + | ||
| 107 | + | echo -e "\e[1;34m[RECEIVE] Liste des répertoires locaux (dans $LOCAL_BASE_DIR) :\e[0m" | |
| 108 | + | local_dirs=$(find "$LOCAL_BASE_DIR" -maxdepth 1 -type d) | |
| 109 | + | select ldir in $local_dirs; do | |
| 110 | + | if [ -n "$ldir" ]; then | |
| 111 | + | local_dest="$ldir" | |
| 112 | + | break | |
| 113 | + | else | |
| 114 | + | echo -e "\e[1;31m❌ Choix invalide, réessayez.\e[0m" | |
| 115 | + | fi | |
| 116 | + | done | |
| 117 | + | ||
| 118 | + | echo -e "\e[1;34m[RECEIVE] Récupération du fichier '$remote_file' vers $local_dest...\e[0m" | |
| 119 | + | afficher_progression "$UTILISATEUR@$IP_DISTANTE:$remote_file" "$local_dest" | |
| 120 | + | } | |
| 121 | + | ||
| 122 | + | interactive_receive_directory() { | |
| 123 | + | echo -e "\e[1;34m[RECEIVE] Liste des répertoires distants (dans /home) :\e[0m" | |
| 124 | + | remote_dirs=$(sshpass -p "$MOTDEPASSE" ssh -p $PORT_SSH $UTILISATEUR@$IP_DISTANTE "find /home -maxdepth 1 -type d") | |
| 125 | + | select rdir in $remote_dirs; do | |
| 126 | + | if [ -n "$rdir" ]; then | |
| 127 | + | remote_dir="$rdir" | |
| 128 | + | break | |
| 129 | + | else | |
| 130 | + | echo -e "\e[1;31m❌ Choix invalide, réessayez.\e[0m" | |
| 131 | + | fi | |
| 132 | + | done | |
| 133 | + | ||
| 134 | + | echo -e "\e[1;34m[RECEIVE] Liste des répertoires locaux (dans $LOCAL_BASE_DIR) :\e[0m" | |
| 135 | + | local_dirs=$(find "$LOCAL_BASE_DIR" -maxdepth 1 -type d) | |
| 136 | + | select ldir in $local_dirs; do | |
| 137 | + | if [ -n "$ldir" ]; then | |
| 138 | + | local_dest="$ldir" | |
| 139 | + | break | |
| 140 | + | else | |
| 141 | + | echo -e "\e[1;31m❌ Choix invalide, réessayez.\e[0m" | |
| 142 | + | fi | |
| 143 | + | done | |
| 144 | + | ||
| 145 | + | echo -e "\e[1;34m[RECEIVE] Récupération du répertoire '$remote_dir' vers $local_dest...\e[0m" | |
| 146 | + | afficher_progression "$UTILISATEUR@$IP_DISTANTE:$remote_dir" "$local_dest" | |
| 147 | + | } | |
| 148 | + | ||
| 149 | + | # =============================== | |
| 150 | + | # Affichage des exemples de commandes CLI | |
| 151 | + | # =============================== | |
| 152 | + | show_commands_example() { | |
| 153 | + | echo -e "\e[1;35mExemples de commandes CLI :\e[0m" | |
| 154 | + | echo -e "1. Envoyer un répertoire :" | |
| 155 | + | echo -e " sshpass -p '$MOTDEPASSE' rsync -avzh --progress -e \"ssh -p $PORT_SSH\" /chemin/vers/repertoire $UTILISATEUR@$IP_DISTANTE:/chemin/vers/destination" | |
| 156 | + | echo -e "2. Envoyer un fichier :" | |
| 157 | + | echo -e " sshpass -p '$MOTDEPASSE' rsync -avzh --progress -e \"ssh -p $PORT_SSH\" /chemin/vers/fichier $UTILISATEUR@$IP_DISTANTE:/chemin/vers/destination" | |
| 158 | + | echo -e "3. Récupérer un répertoire :" | |
| 159 | + | echo -e " sshpass -p '$MOTDEPASSE' rsync -avzh --progress -e \"ssh -p $PORT_SSH\" $UTILISATEUR@$IP_DISTANTE:/chemin/vers/répertoire /chemin/vers/destination" | |
| 160 | + | echo -e "4. Récupérer un fichier :" | |
| 161 | + | echo -e " sshpass -p '$MOTDEPASSE' rsync -avzh --progress -e \"ssh -p $PORT_SSH\" $UTILISATEUR@$IP_DISTANTE:/chemin/vers/fichier /chemin/vers/destination" | |
| 162 | + | exit 0 | |
| 163 | + | } | |
| 164 | + | ||
| 165 | + | # =============================== | |
| 166 | + | # Mode CLI avec getopts | |
| 167 | + | # =============================== | |
| 168 | + | while getopts "l:f:d:h" opt; do | |
| 169 | + | case $opt in | |
| 170 | + | l) LOCAL=$OPTARG ;; | |
| 171 | + | f) FILE=$OPTARG ;; | |
| 172 | + | d) DEST=$OPTARG ;; | |
| 173 | + | h) show_commands_example ;; | |
| 174 | + | *) show_commands_example ;; | |
| 175 | + | esac | |
| 176 | + | done | |
| 177 | + | ||
| 178 | + | if [ "$#" -gt 0 ]; then | |
| 179 | + | sshpass -p "$MOTDEPASSE" true > /dev/null | |
| 180 | + | if [ -n "$FILE" ] && [ -n "$DEST" ]; then | |
| 181 | + | LOCAL="$FILE" | |
| 182 | + | echo -e "\e[1;33mMode CLI (envoi de fichier) activé.\e[0m" | |
| 183 | + | interactive_send_file | |
| 184 | + | exit 0 | |
| 185 | + | elif [ -n "$LOCAL" ] && [ -n "$DEST" ]; then | |
| 186 | + | echo -e "\e[1;33mMode CLI (envoi de répertoire) activé.\e[0m" | |
| 187 | + | interactive_send_directory | |
| 188 | + | exit 0 | |
| 189 | + | fi | |
| 190 | + | fi | |
| 191 | + | ||
| 192 | + | # =============================== | |
| 193 | + | # Mode interactif (aucun argument CLI) | |
| 194 | + | # =============================== | |
| 195 | + | sshpass -p "$MOTDEPASSE" true > /dev/null | |
| 196 | + | ||
| 197 | + | echo -e "\e[1;32m1. Envoyer un fichier ou répertoire\e[0m" | |
| 198 | + | echo -e "\e[1;34m2. Récupérer un fichier ou répertoire\e[0m" | |
| 199 | + | echo -e "\e[1;35m3. Afficher les exemples de commandes CLI\e[0m" | |
| 200 | + | read -p "Choisissez une option (1, 2 ou 3) : " choix | |
| 201 | + | ||
| 202 | + | case $choix in | |
| 203 | + | 1) | |
| 204 | + | echo -e "\e[1;32m🔧 Voulez-vous envoyer un fichier ou un répertoire ?\e[0m" | |
| 205 | + | echo "1. Envoyer un fichier" | |
| 206 | + | echo "2. Envoyer un répertoire" | |
| 207 | + | read -p "Choisissez une option (1 ou 2) : " choix_envoi | |
| 208 | + | if [ "$choix_envoi" -eq 1 ]; then | |
| 209 | + | interactive_send_file | |
| 210 | + | elif [ "$choix_envoi" -eq 2 ]; then | |
| 211 | + | interactive_send_directory | |
| 212 | + | else | |
| 213 | + | echo -e "\e[1;31m❌ Option invalide.\e[0m" | |
| 214 | + | fi | |
| 215 | + | ;; | |
| 216 | + | 2) | |
| 217 | + | echo -e "\e[1;34m🔧 Voulez-vous récupérer un fichier ou un répertoire ?\e[0m" | |
| 218 | + | echo "1. Récupérer un fichier" | |
| 219 | + | echo "2. Récupérer un répertoire" | |
| 220 | + | read -p "Choisissez une option (1 ou 2) : " choix_recup | |
| 221 | + | if [ "$choix_recup" -eq 1 ]; then | |
| 222 | + | interactive_receive_file | |
| 223 | + | elif [ "$choix_recup" -eq 2 ]; then | |
| 224 | + | interactive_receive_directory | |
| 225 | + | else | |
| 226 | + | echo -e "\e[1;31m❌ Option invalide.\e[0m" | |
| 227 | + | fi | |
| 228 | + | ;; | |
| 229 | + | 3) | |
| 230 | + | show_commands_example | |
| 231 | + | ;; | |
| 232 | + | *) | |
| 233 | + | echo -e "\e[1;31m❌ Option invalide. Veuillez choisir 1, 2 ou 3.\e[0m" | |
| 234 | + | ;; | |
| 235 | + | esac | |
Neuer
Älter