/system script add dont-require-permissions=no name=bkp owner=admin policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source="# Backup automatico para FTP - RoS v7\r\ \n##################################################\r\ \n\r\ \n# Configuracao FTP\r\ \n:local ftphost \"172.31.68.10\"\r\ \n:local ftpuser \"mikrotik\"\r\ \n:local ftppassword \"AN4RtNm5*hK5VHLky!0JBzR0\"\r\ \n\r\ \n# Matriz de meses\r\ \n:local months (\"jan\",\"feb\",\"mar\",\"apr\",\"may\",\"jun\",\"jul\",\"aug\",\"sep\",\"oct\",\"nov\",\"dec\");\r\ \n\r\ \n# 1. TRATAMENTO DA HORA\r\ \n:local ts [/system clock get time]\r\ \n# Remove os \":\" da hora para evitar problemas em sistemas de arquivos\r\ \n:set ts ([:pick \$ts 0 2] . [:pick \$ts 3 5] . [:pick \$ts 6 8])\r\ \n\r\ \n# 2. TRATAMENTO DA DATA (Dinamico para v7)\r\ \n:local ds [/system clock get date]\r\ \n:local day; :local month; :local year; :local mm\r\ \n\r\ \n# Se a data contiver h\EDfen (formato yyyy-mm-dd)\r\ \n:if (\$ds ~ \"-\") do={\r\ \n :set year [:pick \$ds 0 4]\r\ \n :set mm [:pick \$ds 5 7]\r\ \n :set day [:pick \$ds 8 10]\r\ \n} else={\r\ \n # Se for formato mes/dia/ano (jan/15/2026)\r\ \n :set month [:pick \$ds 0 3]\r\ \n :set mm ([:find \$months \$month -1] + 1)\r\ \n :if (\$mm < 10) do={ :set mm (\"0\" . \$mm) }\r\ \n :set day [:pick \$ds 4 6]\r\ \n :set year [:pick \$ds 7 11]\r\ \n}\r\ \n\r\ \n:local datenew (\$day . \$mm . \$year)\r\ \n\r\ \n# 3. DEFINICAO DOS NOMES (Sem \"/\" no inicio)\r\ \n:local identity [/system identity get name]\r\ \n:local fname (\"MK-\" . \$identity . \"-\" . \$datenew . \"-\" . \$ts . \".backup\")\r\ \n:local fname1 (\"MK-\" . \$identity . \"-\" . \$datenew . \"-\" . \$ts . \".rsc\")\r\ \n\r\ \n# 4. EXECUCAO DO BACKUP\r\ \n:log info \"Iniciando Backup: \$fname\"\r\ \n/system backup save name=\"\$fname\"\r\ \n:delay 2s;\r\ \n\r\ \n:log info \"Iniciando Export: \$fname1\"\r\ \n/export file=\"\$fname1\"\r\ \n:delay 5s;\r\ \n\r\ \n# 5. ENVIO PARA O FTP\r\ \n:log info \"Enviando arquivos para FTP \$ftphost...\"\r\ \n/tool fetch address=\"\$ftphost\" src-path=\"\$fname\" user=\"\$ftpuser\" mode=ftp password=\"\$ftppassword\" dst-path=\"\$fname\" upload=yes\r\ \n:delay 2s;\r\ \n/tool fetch address=\"\$ftphost\" src-path=\"\$fname1\" user=\"\$ftpuser\" mode=ftp password=\"\$ftppassword\" dst-path=\"\$fname1\" upload=yes\r\ \n\r\ \n# 6. LIMPEZA LOCAL\r\ \n:delay 5s;\r\ \n:foreach i in=[/file find where name~\"MK-\"] do={\r\ \n /file remove \$i\r\ \n}\r\ \n:log info \"Backup e envio concluidos com sucesso.\""