Community discussions

 
slech
Long time Member
Long time Member
Topic Author
Posts: 533
Joined: Thu Feb 14, 2008 4:03 pm
Location: Moldova, Chisinau

Backup Mikrotik config to Subversion/SVN repository via SSH

Thu Jan 12, 2012 4:25 pm

Russian variant - Резервное копирование Mikrotik в Subversion посредством SSH/SFTP

Inspired by: Using SSH for system backup

Related forum posts
remote creating backup-file
Difference between backup and export - how to expect changes
Backup and Restore Certificates
Mikrotik SSH Backup - my solution

Realization
Requirements:
1. ROS 5.15
2. SVN Repository
3. Linux with ssh, sftp, sshpass and svn

1. Import ROS public-key into your user known_hosts file
ssh-keyscan -v -p 22 -t dsa 192.168.0.1 >> ~/.ssh/known_hosts
2. Create user for backups on ROS
export = ssh, ftp,read, sniff
backup = ssh, test, policy
get export via sftp = ssh, ftp
get backup via sftp = ssh, ftp, sensitive
/user group add name=backup policy=ssh,ftp,read,sniff,test,policy,sensitive
/user add name=backuper password="password" group=backup address=192.168.0.2
3. Create in SVN folder for device:
svn mkdir --parents https://svn.domain.com/svn/admin/trunk/usingw01 --no-auth-cache --username user --password '*****' --message "Created empty directory for usingw01 - `date +"%Y-%m-%d %H:%M:%S"`"
4. Create folder for SVN working copy:
mkdir -p /root/backup/trunk/usingw01/
5. Create SVN working directory
cd /root/backup/trunk/usingw01
svn checkout https://svn.domain.com/svn/admin/trunk/usingw01 . --trust-server-cert --non-interactive --no-auth-cache --username usingw01 --password 'svnpassword'
6. Create folder for script:
mkdir /root/backup_scripts
7. Create script:
vi /root/backup_scripts/backup_usingw01_to_svn.sh
#!/bin/sh
#
routername="usingw01"
sshhost="192.168.0.1"
sshport="22"
sshuser="backuper"
sshpassword="password"
svnlocalpath="/root/backup/trunk/$routername"
svnusername="usingw01"
svnpassword="svnpassword"
current_export_name="$routername-config-export-current.rsc"
precedent_export_name="$routername-config-export-precedent.rsc"
current_backup_name="$routername-config-backup-current.backup"
#
#
sshpass -p $sshpassword ssh $sshuser@$sshhost -p $sshport export >$current_export_name
diff -I "by Router" $current_export_name $svnlocalpath/$precedent_export_name
#
if [ "$?" -ne "0" ]; then
sshpass -p $sshpassword ssh $sshuser@$sshhost -p $sshport export file=$current_export_name
sshpass -p $sshpassword ssh $sshuser@$sshhost -p $sshport system backup save name=$current_backup_name
sshpass -p $sshpassword sftp -oPort=$sshport $sshuser@$sshhost:$current_backup_name
#
mv -f $current_export_name $svnlocalpath/
mv -f $current_backup_name $svnlocalpath/
rm -f $svnlocalpath/$precedent_export_name
svn add --force $svnlocalpath/$current_export_name
svn add --force $svnlocalpath/$current_backup_name
svn commit $svnlocalpath --trust-server-cert --non-interactive --no-auth-cache --username $svnusername --password $svnpassword --message "Automated commit of $routername at `date +"%Y-%m-%d %H:%M:%S"`"
#
mv -f $svnlocalpath/$current_export_name $svnlocalpath/$precedent_export_name
exit 1
#
#
fi
mv -f $current_export_name $svnlocalpath/$precedent_export_name
exit 0
#
8. Create cron job:
crontab -e
00 04 * * * sh /root/backup_scripts/backup_usingw01_to_svn.sh
The logic of the script:
1. make first export
2. diff current export with precedent export
3. if diff exist make a backup
4. commit export and backup to svn

Be careful with passwords of users, if your password contain $, ), !, ` or other special bash symbols, you must shield their with \:
\$, \), \`, "'!'"

Also you can setup post-commit hook on svn and you will receive email notifications with diff of export file. This is useful to view last changes in config:
RouterOS-Backup-Mikrotik-RouserOS-config-to-Subversion-repository-via-SSH-01.png
Hopefully this will be useful for someone.
You do not have the required permissions to view the files attached to this post.
Last edited by slech on Thu Jul 06, 2017 10:42 pm, edited 7 times in total.
sorry for my english
 
borko
just joined
Posts: 1
Joined: Thu May 02, 2013 9:47 am

Re: Backup Mikrotik config to Subversion/SVN repository via

Thu May 02, 2013 9:51 am

It is useful. Thank you very much!
 
savage
Forum Guru
Forum Guru
Posts: 1196
Joined: Mon Oct 18, 2004 12:07 am
Location: Cape Town, South Africa
Contact:

Re: Backup Mikrotik config to Subversion/SVN repository via

Thu May 02, 2013 10:19 am

Or just use Rancid

There are modules for Mikrotik available.
Regards,
Chris
 
slech
Long time Member
Long time Member
Topic Author
Posts: 533
Joined: Thu Feb 14, 2008 4:03 pm
Location: Moldova, Chisinau

Re: Backup Mikrotik config to Subversion/SVN repository via

Mon Aug 26, 2013 3:03 pm

sorry for my english

Who is online

Users browsing this forum: Google [Bot] and 82 guests