Mar 15

Handy backup scripts

Making backups isn’t really a priority for most of us. But if you ever had a crash you know the horror of not having one. Here’s some basic scripts for various systems that preform automatic backups. I have added scripts for Cisco devices, MySQL backups for Linux and Synctoy file synchronisation (automated).

For easy editing I have marked the options that need changing in red.

Cisco routers and switches

Cisco has a built-in archive option. Problem is, it doesn’t overwrite the old backups, which makes you end up with 365 backups at the end of the year of basically the same configuration. Here’s an automated kron job that overwrites the same file on a ftp-server every day.

ip ftp username username
ip ftp password password

kron occurrence daily-backup at 0:30 recurring
policy-list backup

kron policy-list backup
cli show run | redirect ftp://ftp_server_path/name.txt

Backing up a MySQL server

Using an online backup application is a good way to securely store your data. I myself often use the IASO online backup client which also comes in Linux flavors. It cannot however preform database backups for MySQL server. Here’s a script I use (via a scheduled cron job) that also optimizes all tables before dumping it up to a gzip file, online backup then takes care of the actual backup.

Put this is a file (like backup_mysql.sh) and give it execute permissions (chmod +x backup_mysql.sh). You can then add this to a cron job.

### MySQL Server Login Info ###
MYSQL=”$(which mysql)”
MYSQLDUMP=”$(which mysqldump)”
GZIP=”$(which gzip)”
NOW=$(date +”%d-%m-%Y”)

### See comments below ###
### [ ! -d $BAK ] && mkdir -p $BAK || /bin/rm -f $BAK/* ###
[ ! -d "$BAK" ] && mkdir -p “$BAK”

DBS=”$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse ‘show databases’)”
for db in $DBS
mysqlcheck -o -u$MUSER -p$MPASS $db
FILE=$BAK/$db.$NOW-$(date +”%T”).gz


Automated Synctoy backup

Synctoy is an easy, and free, way to keep files in sync between a NAS station or some other device. Depending on the setup you can also use it to sync stuff (over a VPN for security obviously) to another server/PC/NAS station. To automate this, just setup some sync jobs with the synctoy application itself. Do a one time manual sync of all the jobs and you can put the script below into a batch file and schedule it via WIndows Task Manager. Make sure though that there is a user logged into the server, you can lock the desktop, but not logoff or it wont work. This script uses the DOS applications “Stopwatch.exe” and “VMailer.exe” to keep track of sync time and mail you the logfile that is created, you can get these applications from the downloads section. Just put them in the same directory as the batch file and your good to go. This script will now automatically sync all the jobs on the scheduled time.

@echo off
SET email=send_report_to@domain.com
SET from=noreply_from@domain.com
SET emailserver=smtp.server.domain.com


stopwatch start > timestamp.txt

echo To: %email% >mail.log
echo From: %from% >>mail.log
echo Subject: Synctoy sync %date% >>mail.log
echo. >>mail.log

echo Synchonisatie started: %date% %time% >> mail.log
“C:\Program Files\SyncToy 2.1\SyncToyCmd.exe” -R >> mail.log
stopwatch stop < timestamp.txt > elapsed.txt
for /F %%a in (‘type elapsed.txt’) do set /a elapsed_time=%%a
echo Synchronisatie completed in: %elapsed_time% seconden >> mail.log
echo. >> sync.log
del timestamp.txt
del elapsed.txt
type mail.log >> sync.log

VMailer.exe mail.log %emailserver% %email% %from% >>sync.log
del mail.log

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>