Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| scripts [2025/05/28 14:02] – [MYSQL DATABASE BACKUP] huracan | scripts [2025/12/04 14:31] (Version actuelle) – [PYTHON] huracan | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| ====== SCRIPTS ====== | ====== SCRIPTS ====== | ||
| + | |||
| + | |||
| + | |||
| Ligne 8: | Ligne 11: | ||
| ---- | ---- | ||
| + | |||
| + | |||
| + | ===== PYTHON ===== | ||
| + | |||
| + | |||
| + | Script pour commandes multithread sur plusieurs switchs | ||
| + | |||
| + | # | ||
| + | from concurrent.futures import ThreadPoolExecutor, | ||
| + | from datetime import datetime | ||
| + | from getpass import getpass | ||
| + | import threading | ||
| + | from pathlib import Path | ||
| + | import os | ||
| + | import socket | ||
| + | from netmiko import ConnectHandler | ||
| + | | ||
| + | SWITCH_FILE = " | ||
| + | MAX_WORKERS = 20 | ||
| + | | ||
| + | print_lock = threading.Lock() | ||
| + | | ||
| + | | ||
| + | def safe_print(*a, | ||
| + | with print_lock: | ||
| + | print(*a, | ||
| + | | ||
| + | | ||
| + | def load_hosts(path): | ||
| + | p = Path(path) | ||
| + | if not p.exists(): | ||
| + | return [] | ||
| + | return [x.strip() for x in p.read_text().splitlines() if x.strip()] | ||
| + | | ||
| + | | ||
| + | # --------- AJOUT : TEST SSH AVANT CONNEXION ----------- | ||
| + | def test_ssh_port(host, | ||
| + | try: | ||
| + | with socket.create_connection((host, | ||
| + | return True | ||
| + | except: | ||
| + | return False | ||
| + | # ------------------------------------------------------- | ||
| + | | ||
| + | | ||
| + | def run_on_switch(host, | ||
| + | result = { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | | ||
| + | # --- Test SSH avant toute connexion --- | ||
| + | if not test_ssh_port(host): | ||
| + | result[" | ||
| + | result[" | ||
| + | return result | ||
| + | | ||
| + | try: | ||
| + | device = { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | | ||
| + | conn = ConnectHandler(**device) | ||
| + | | ||
| + | except Exception as e: | ||
| + | result[" | ||
| + | result[" | ||
| + | return result | ||
| + | | ||
| + | try: | ||
| + | conn.send_command(" | ||
| + | | ||
| + | for cmd in commands: | ||
| + | out = conn.send_command(cmd, | ||
| + | for line in out.splitlines(): | ||
| + | safe_print(f" | ||
| + | result[" | ||
| + | | ||
| + | result[" | ||
| + | | ||
| + | except Exception as e: | ||
| + | result[" | ||
| + | | ||
| + | finally: | ||
| + | conn.disconnect() | ||
| + | result[" | ||
| + | | ||
| + | return result | ||
| + | | ||
| + | | ||
| + | def choose_switches(hosts): | ||
| + | while True: | ||
| + | safe_print(" | ||
| + | for i, h in enumerate(hosts, | ||
| + | safe_print(f" | ||
| + | | ||
| + | safe_print(" | ||
| + | safe_print(" | ||
| + | safe_print(" | ||
| + | safe_print(" | ||
| + | safe_print(" | ||
| + | safe_print(" | ||
| + | | ||
| + | choice = input(" | ||
| + | | ||
| + | if choice == " | ||
| + | return None | ||
| + | | ||
| + | if choice == " | ||
| + | return hosts | ||
| + | | ||
| + | try: | ||
| + | indices = [int(x.strip()) for x in choice.split("," | ||
| + | selected = [hosts[i - 1] for i in indices if 1 <= i <= len(hosts)] | ||
| + | if selected: | ||
| + | return selected | ||
| + | except: | ||
| + | pass | ||
| + | | ||
| + | safe_print(" | ||
| + | | ||
| + | | ||
| + | def main(): | ||
| + | hosts = load_hosts(SWITCH_FILE) | ||
| + | if not hosts: | ||
| + | safe_print(f" | ||
| + | return | ||
| + | | ||
| + | safe_print(f" | ||
| + | | ||
| + | username = input(" | ||
| + | password = getpass(" | ||
| + | | ||
| + | current_selection = hosts # par défaut : tous | ||
| + | | ||
| + | while True: | ||
| + | safe_print(" | ||
| + | safe_print(" | ||
| + | for h in current_selection: | ||
| + | safe_print(f" | ||
| + | | ||
| + | safe_print(" | ||
| + | safe_print(" | ||
| + | safe_print(" | ||
| + | safe_print(" | ||
| + | safe_print(" | ||
| + | safe_print(" | ||
| + | | ||
| + | choice = input("> | ||
| + | | ||
| + | if choice == " | ||
| + | return | ||
| + | | ||
| + | if choice == " | ||
| + | os.system(" | ||
| + | continue | ||
| + | | ||
| + | if choice == " | ||
| + | new_sel = choose_switches(hosts) | ||
| + | if new_sel: | ||
| + | current_selection = new_sel | ||
| + | continue | ||
| + | | ||
| + | if choice == " | ||
| + | safe_print(" | ||
| + | commands = [] | ||
| + | | ||
| + | while True: | ||
| + | cmd = input("> | ||
| + | if cmd == "": | ||
| + | if commands: | ||
| + | break | ||
| + | else: | ||
| + | continue | ||
| + | commands.append(cmd) | ||
| + | | ||
| + | safe_print(" | ||
| + | | ||
| + | start_all = datetime.now() | ||
| + | | ||
| + | results = [] | ||
| + | with ThreadPoolExecutor(max_workers=min(MAX_WORKERS, | ||
| + | future_map = { | ||
| + | pool.submit(run_on_switch, | ||
| + | for h in current_selection | ||
| + | } | ||
| + | | ||
| + | for f in as_completed(future_map): | ||
| + | results.append(f.result()) | ||
| + | | ||
| + | # --- Résumé synthétique --- | ||
| + | safe_print(" | ||
| + | for r in results: | ||
| + | host = r[" | ||
| + | status = " | ||
| + | lines = len(r[" | ||
| + | dur = (r[" | ||
| + | safe_print(f" | ||
| + | if r[" | ||
| + | safe_print(f" | ||
| + | | ||
| + | total = (datetime.now() - start_all).total_seconds() | ||
| + | safe_print(f" | ||
| + | safe_print(" | ||
| + | | ||
| + | | ||
| + | if __name__ == " | ||
| + | main() | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | ---- | ||
| + | |||
| + | |||
| ===== WINDOWS ===== | ===== WINDOWS ===== | ||
| Ligne 14: | Ligne 239: | ||
| ---- | ---- | ||
| + | ==== CONVERSION M4A EN FLAC AVEC FFMPEG ==== | ||
| + | |||
| + | 🪟 Sur Windows (PowerShell ou CMD) : | ||
| + | |||
| + | Dans le dossier contenant les .m4a exécuter : | ||
| + | |||
| + | #for %a in (*.m4a) do ffmpeg -i " | ||
| + | |||
| + | |||
| + | ---- | ||
| Ligne 1113: | Ligne 1348: | ||
| #echo `test $machin = ' | #echo `test $machin = ' | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ==== CONVERSION M4A EN FLAC AVEC FFMPEG ==== | ||
| + | |||
| + | Dans le dossier contenant les .m4a, exécuter : | ||
| + | |||
| + | 🐧 Sur Linux (terminal bash) : | ||
| + | |||
| + | #for f in *.m4a; do ffmpeg -i " | ||
| ---- | ---- | ||
| Ligne 1351: | Ligne 1596: | ||
| Here’s the rsync command you can add to the end of your backup script: | Here’s the rsync command you can add to the end of your backup script: | ||
| - | # Sync backup to remote server (optional but recommended) | + | |
| - | + | ||
| - | SSH_KEY="/ | + | SSH_KEY="/ |
| - | REMOTE_USER=" | + | REMOTE_USER=" |
| - | REMOTE_HOST=" | + | REMOTE_HOST=" |
| - | REMOTE_DIR="/ | + | REMOTE_DIR="/ |
| - | + | ||
| - | rsync -avz \ | + | rsync -avz \ |
| - | -e "ssh -i $SSH_KEY -o StrictHostKeyChecking=no" | + | -e "ssh -i $SSH_KEY -o StrictHostKeyChecking=no" |
| - | --delete-after \ | + | --delete-after \ |
| - | " | + | " |
| - | " | + | " |
| Make sure the remote server is reachable, and the destination directory exists with proper write permissions. | Make sure the remote server is reachable, and the destination directory exists with proper write permissions. | ||
| Ligne 1368: | Ligne 1613: | ||
| Once you’ve saved your script, make it executable by running chmod command: | Once you’ve saved your script, make it executable by running chmod command: | ||
| - | chmod +x backup_mysql.sh | + | |
| - | Step 2: Testing MySQL Backup Script | + | === Step 2: Testing MySQL Backup Script |
| Before setting up the Cron job, it’s a good idea to test the script manually to make sure everything is working as expected. | Before setting up the Cron job, it’s a good idea to test the script manually to make sure everything is working as expected. | ||
| - | ./ | + | |
| Check your backup directory to ensure the backups are created successfully. If everything looks good, proceed to the next step. | Check your backup directory to ensure the backups are created successfully. If everything looks good, proceed to the next step. | ||
| - | Step 3: Automating MySQL Backups with Cron Jobs | + | |
| + | === Step 3: Automating MySQL Backups with Cron Jobs === | ||
| Now that we have the backup script, the next step is to automate it by using Cron, a tool that runs commands at scheduled times. | Now that we have the backup script, the next step is to automate it by using Cron, a tool that runs commands at scheduled times. | ||
| - | crontab -e | + | |
| Add a Cron job to run the backup script automatically. For example, to run the script every day at 2 AM, add the following line: | Add a Cron job to run the backup script automatically. For example, to run the script every day at 2 AM, add the following line: | ||
| - | 0 2 * * * /bin/bash / | + | |
| Here’s how the Cron schedule works: | Here’s how the Cron schedule works: | ||
| - | | + | * 0: The minute (0th minute). |
| - | 2: The hour (2 AM). | + | |
| - | *: Every day of the month. | + | |
| - | *: Every month. | + | |
| - | *: Every day of the week. | + | |
| To verify that your Cron job is running, you can check the system logs or temporarily set the script to run at a closer time to see if it works. | To verify that your Cron job is running, you can check the system logs or temporarily set the script to run at a closer time to see if it works. | ||
| - | grep CRON / | + | |
| - | Additional Considerations | + | **//Additional Considerations//** |
| - | | + | * Security: Storing your MySQL password in the script is convenient but not secure. For better security, you can store your credentials in a .my.cnf file in your home directory and configure the script to read from there. |
| - | Backup Location: Make sure that the backup directory has enough space for your backups. If you’re running multiple backups, it’s a good idea to set up a separate storage location (like an external hard drive or cloud storage). | + | |
| - | Backup Frequency: Depending on how often your data changes, you might want to adjust the Cron job schedule. For example, you could run the backup every hour, every week, or only on certain days. | + | |