Maintenance Script for Userman - Export - Backup files

After running Userman on a RouterOS PC for a log time and having problem when there was a power failure to recover the database.
I decided to build a Maintenance Script to backup Userman’s database and make a export file and a system backup and then to FTP the files to my ftp server. This Script is base on what I got from read the MikroTik Forum and a lot of testing.

I have to create four scripts, one for disabling users in Userman not to be able to connect to Userman, will the maintenance is done at 01:00:00, the second one for disabling all Active Connections for all users at 01:00:01 hrs, The third scripts is where I backup all the necessary files and then it get FTP to my FTP server and then it get remove form my system at 01:00:02 you can schedule them as per your required timings. The Forth script enables all the user on Userman to reconnect if there session is active at 01:04:00. Check how long your the Userman-Backup-Export-FTP script runs and set your Forth script time according to how log it takes to run.

1st Script name: Disable_Users_For_Maintenance_Userman

# Setting Clock Time
:local CurrentTime [/system clock get time];
:log warning "Disabling Users for Userman at Current Time = $CurrentTime."

# Setting Variables for Disabling Users in Userman
:local Flag
:set Flag [/tool user-manager user find]
:if ([:len $Flag] > 0) do={/tool user-manager user disable [/tool user-manager user find];}

# Log
:log warning "Disabling Users for Userman Completed . . ."

2nd Script name: Disable_Users_For_Active_Connections

# Setting Clock Time
:local CurrentTime [/system clock get time];
:log warning "Disabling Users For Active Connections at Current Time = $CurrentTime"

# Setting Global Variables - Disabling Active Connections
:local Flag
:set Flag [/ppp active find]
:if ([:len $Flag] > 0) do={/ppp active remove [/ppp active find];}

# Log
:log warning "Disabling Users For Active Connections Completed . . ."

3rd Script name: Userman-Backup-Export-FTP

# Automated Userman backup & Export & RB Backup then FTP to External FTP site
# File name:Userman-Backup-Export-FTP
# Get identity
:local thisbox [/system identity get name]
# Get time
:local thistime [/system clock get time]
:set thistime ([:pick $thistime 0 2].[:pick $thistime 3 5].[:pick $thistime 6 8])
# Get Date
:local thisdate [/system clock get date];
:local month
   :set month [:pick $thisdate 0 3]
   :if ($month="jan") do { :set month "01"}
   :if ($month="feb") do { :set month "02"}
   :if ($month="mar") do { :set month "03"}
   :if ($month="apr") do { :set month "04"}
   :if ($month="may") do { :set month "05"}
   :if ($month="jun") do { :set month "06"}
   :if ($month="jul") do { :set month "07"}
   :if ($month="aug") do { :set month "08"}
   :if ($month="sep") do { :set month "09"}
   :if ($month="oct") do { :set month "10"}
   :if ($month="nov") do { :set month "11"}
   :if ($month="dec") do { :set month "12"}

:set thisdate ([:pick $thisdate 7 11]."_".$month."_".[:pick $thisdate 4 6])

# Export file name will be Export_RB Identity_Date_Time .rsc
:local FExport ("/Export_".$thisbox."_".$thisdate."_".$thistime.".rsc")
:delay 10s;
/export file=$FExport

# System backup file name will be BackUp_ - RB Identity - Date - Time .backup
:local FBackup ("/BackUp_".$thisbox."_".$thisdate."_".$thistime.".backup")
:delay 10s;
/system backup save name=$FBackup;

# This Userman backup file name will be Userman_ - RB Identity - Date - Time .umb
:local FName ("/Userman_".$thisbox."_".$thisdate."_".$thistime.".umb")
:delay 10s;
/tool user-manager database save name=$FName

:delay 10s;
:log info message="Backup File's is Create - $FBackup";
:log info "Backup File's is Create - $FBackup";
:log info "Export File's is Create - $FExport";
:log info "UM Database Backup File is Create - $FName";

:delay 10s;
:log info "Export File & System Backup & Userman Backup File's is now been send to your FTP Server";
# Upload the Export file to your FTP server - change address to your ftp server + user + pass + upload_path
/tool fetch address="192.168.0.48" user="mikrotik" password="mikrotik" mode=ftp src-path="$FExport" dst-path="/$FExport" upload=yes
:delay 40s;
# Upload the System Backup file to your FTP server - change address to your ftp server + user + pass+upload_path
/tool fetch address="192.168.0.48" user="mikrotik" password="mikrotik" mode=ftp src-path="$FBackup" dst-path="/$FBackup" upload=yes
:delay 40s
# Upload the Userman backup file to your FTP server - change address to your ftp server + user + pass+upload_path
/tool fetch address="192.168.0.48" user="mikrotik" password="mikrotik" mode=ftp src-path="$FName" dst-path="/$FName" upload=yes

:log info message="Export File & System Backup & Userman Backup File's is now done sending to your FTP Server";
:delay 5s;

# Removing files from system
:foreach i in=[/file find] do={:if ([:typeof [:find [/file get $i name] "Export_"]]!="nil") do={/file remove $i}}
:log info message="Export File's is removed";
:foreach i in=[/file find] do={:if ([:typeof [:find [/file get $i name] "Userman_"]]!="nil") do={/file remove $i}}
:log info message="Userman Database Backup File's is removed...";
:foreach i in=[/file find] do={:if ([:typeof [:find [/file get $i name] "BackUp_"]]!="nil") do={/file remove $i}}
:log info message="Backup File's is removed";

4th Script name: Enable_Users_For_Userman

# Setting Clock Time
:local CurrentTime [/system clock get time];
:log warning "Enable Users For Userman at Current Time = $CurrentTime"

# Setting Global Variables- Enabling Users in Userman
:local Flag
:set Flag [/tool user-manager user find]
:if ([:len $Flag] > 0) do={/tool user-manager user enable [/tool user-manager user find];}
:log warning "Enable Users For Userman Completed . . ."

Scheduler Entry to run all the scripts at given timings.

/system scheduler
add interval=1d name=Disable_Users_For_Maintenance_Userman_at_01:00:00 on-event=Disable_Users_For_Maintenance_Userman \
    policy=ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api \
    start-date=Mar/18/2014 start-time=01:00:00

add interval=1d name=Disable_Users_For_Active_Connections_at_01:00:01 on-event=Disable_Users_For_Active_Connections policy=\
    ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api \
    start-date=Mar/18/2014 start-time=01:00:01

add interval=1d name=Userman-Backup-Export-FTP_at_01:00:02 on-event=Userman-Backup-Export-FTP policy=\
    ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api \
    start-date=Mar/18/2014 start-time=01:00:02

add interval=1d name=Enable_Users_For_Userman_at_01:04:00 on-event=Enable_Users_For_Userman policy=\
    ftp,reboot,read,write,policy,test,winbox,password,sniff,sensitive,api \
    start-date=Mar/18/2014 start-time=01:04:00

If you can enhance this scripts please do so, you can modify it as you like.
Please give karma if the post helps you, Thx.

Why disable / re-enable, etc. and closing the connection of the users??? :imp:
Are you happy when your provider stop internet? :angry:
Also, in this way, manually disabled users are re-enabled :exclamation:

Thx, for your comments.

Why disable / re-enable, etc. and closing the connection of the users??? > :imp:
Are you happy when your provider stop internet? > :angry:
Also, in this way, manually disabled users are re-enabled > :exclamation:

Please keep in mind this is Maintenance Script and It’s to prevent any connection to the database, for you about to make a backup of the Database you don’t want any database connection while you doing an backup.
No, then again ISP do have Maintenance slot to run backup’s. The user will be down for max of 5 min and not a couple of hours if you have a crashed or corrupted database to restore it lol

After running Userman on a RouterOS PC for a log time and having problem when there was a power failure to recover the database.I decided to build a Maintenance Script to backup Userman’s database and make a export file and a system backup and then to FTP the files to my ftp server.