Friday, December 11, 2015

AutoMySQLBackup daily rotation only keep one day or latest backup day SOLVED

I had problems with my AutoMySQLBackup script on my ubuntu vps server, using the latest (3.0_rc6) available version of the script. The problem I had was that the daily rotation was not working as expected. The script kept yesterdays backup and did not remove it. It seemed to be happening randomly but nothing is ever random.

So today I took some of my precious time to investigate the problem. I found out that the problem was on line 803:

find "${CONFIG_backup_dir}/${subfolder}${subsubfolder}" -mtime +"${rotation}" -type f -exec rm {} \;
The rotation-parameter was configured to be "0". No problems with that as we only want to keep the latest backup file that was created today. But the mtime function with find is the problem where the argument to mtime are the number of whole days. While some backup-files wasn't created (or modified) a whole day from the current time.
-mtime n
       The primary shall evaluate as true if the file modification time subtracted from the initialization time, divided by 86400 (with any remainder discarded), is n.
So this is the problem. Now the solution can be fixed in various of ways. But I just needed a quick fix to this as I always want to only keep the latest mysql database backup files. So I modified that line (in particularly and some others just on case I need something else than 0 in rotation configuration) to:
find "${CONFIG_backup_dir}/${subfolder}${subsubfolder}" -mmin +$((60*23)) -type f -exec rm {} \;
As you can see from above, I'm looking for files that is 23 hours or older. So this covers my gap of backup file creation time.

I'm happy if this helps you out. AutoMySQLBackup is a great script but not really maintained the best.

Thursday, May 28, 2015

Exclude directories and files with Tar in Ubuntu Linux

I had problems with figuring out how to exclude directories with files and sub-directories with tar. I've tried several of ways to accomplish this but all of my effort ended with the exclude being ignored somehow.

But finally I found out how to do this. So my system is Linux Ubuntu 14.04 with Tar version 1.27.1.

tar --exclude=var/www -cvpjf /var/backups/vps/vpsBackup_$(date +"%F").tar.bz2 *
So what the above does: excludes the directory www in var with all it's files and sub directories. So no trailing slash after equal sign and no after in the end of path.

The whole manual for tar can be found here. Also with the command tar --help will show you a list of possible actions.

I hope this will help out somebody.

Tuesday, October 14, 2014

Solution to Knockd won't work / open port in iptables

I had a struggle to get Portknocking with knockd to work on my Ubuntu 14.04 VPS. I've read and followed a lot of instructions, Ubuntus instruction among these. But nothing seemed to help me out here.

I did check my knockd log located to /var/log/knockd.log and the configuration for activating the knockd commands seemed to work. But I always ended up with "command returned non-zero status code (a number)"

So what I figured out that it had to do something with the start_command and stop_command that didn't do the job correctly. Everywhere I could read that you were "supposed to" control the IP tables by having e.g.
start_command = /sbin/iptables -A INPUT -s %IP% -p tcp --dport 22 -j ACCEPT
If it was say to open up the SSH-default port 22. But that didn't work for me.
The first I did was to check if /sbin/iptables even existed and it didn't. No wonder why nothing happen with my iptables configuration...

So one solution to this was for me to create two shell script to configure the iptables for me.
I created a knock-open.sh containing this.
#!/bin/sh 
iptables -I INPUT 1 -s $1 -p tcp -m tcp --dport 22 -j ACCEPT
Then I created a knock-close.sh:
#!/bin/sh
iptables -D INPUT -s $1 -p tcp -m tcp --dport 22 -j ACCEPT
And my knockd.conf-file (/etc/knockd.conf), I configured it like this:
[options] 
logfile = /var/log/knockd.log 
[SSH] 
sequence      = 1212:udp,3861:tcp,8721:udp 
seq_timeout   = 5 
tcpflags      = syn 
start_command = sh /var/scripts/knock-open.sh %IP% 
cmd_timeout   = 20 
stop_command  = sh /var/scripts/knock-close.sh %IP%
So this did the trick for me. After restarting the daemon (service knockd restart) and knocking the sequence ports, iptables was now configured correctly and working with knockd.

I hope this solution helps someone out there who's struggling with knockd and iptables.