What I used for automatic backups for my DHCP servers. It could be modified for your use. This is a write up that I did for a few guys here at work.
First check if ssh/scp port is open(only needed if you’re at the machine locally)
[Local host]$ netstat –l | grep ssh (could also use netstat –a |grep ssh) (lists all active ssh sessions)
[Local host]$ ~]# netstat -l | grep ssh
tcp 0 0 *:ssh *:* LISTEN
tcp 0 0 *:ssh *:* LISTEN
Once the port is confirmed open, you will need to generate either an RSA or DSA key pair. The “-t” denotes the type of key being generated.
[Local host]$ ssh-keygen –t rsa (could also be ssh-keygen –t dsa)
This will prompt you for the dir you want to save it to. Just use the default dir, which is /root/.ssh/
This will also prompt you for a passphrase to use…simply hit enter to leave it blank. When asked to confirm password hit enter again.
This will create two different keys: id_rsa and id_rsa.pub
Once the files are created you will need to cat the contents of the key into /root/.ssh/authorized_keys. If this file does not exist you will need to create it using the touch command.
[Local host]$ touch authorized_keys
The private key(id_rsa) contents will need to be transferred to the “authorized_keys” file on the local machine.
[Local host]$ cat id_rsa >> authorized_keys
This will take the contents of the key and paste it into the file. (Confirm by using “cat /root/.ssh/authorized_keys”)
You will now need to transfer the public key (id_rsa.pub) to the remote machine that you will be backing up the information to.
[Local host]$ scp /root/.ssh/id_rsa.pub
root@192.168.51.XXX:/root/.ssh
Once the file transfer is complete you will need copy the contents of the key into “authorized_keys” using “cat id_rsa.pub >> authorized_keys” And again if this file does not exist use “touch authorized_keys” to create it.
Test these newly created keys by starting an ssh session from the local machine to the remote machine using for example “ssh 192.168.51.218” It should login you in using the keys and without being prompt for a password.
Once you have all of that done you will need to create a cron job to run your daily, hourly, month…etc transfer or backup of the data.
This cron job can be created in one of two ways…you can set it up so the local machine uploads the data to the remote machine or you can set it up to where the remote machine pulls it down from the local machine. Either way is fine and you can build the cron on either machine…purely preference. The example I am giving below is building the cronjob on the local machine and uploading the files to be backed up to the remote machine. Again this could be done in reverse essentially but this is the way I chose to do it.
One other thing to note is that there are a few different ways to enable a cron job. You could place the cronjob in “cronjob.daily” to make it run everyday or maybe create a “cronjob.monthly” to make it run once a month. I will be using cronjob editor (crontab –e) because I require my cron to run multiple times a day and at specific hours of the day.
First, check and see if there are currently any cronjobs that are already built into the server. This is done with the following…
[Local host]$ crontab –l
This will list all the cronjob’s in the server currently.
To create a new cron that will run at specific times multiple times per day you will need to use the command
[Local host]$ crontab –e
This will open a VI editor window that will allow you to create the new cron job.
The cron that I am using is utilizing “scp” or secure copy.
00 0,12 * * * scp /var/lib/dhcpd/dhcpd.leases root@192.168.51.218:/var/lib/dhcpd
Once this line has been added you will need to write the file and you’re done.
So how that cron job reads is that it starts at the top of the hour”00” it runs at 12am and 12pm “0,12” the first “*” is the day of the week, the second “*” is the month of the year, the last “*” is the week of the month. So “* * *” would indicate that this cron will run every day, of every month, and every day of every month
These values can be supplemented for numbers representing corresponding days, weeks and months. Days being “0-7” months being “1-12” and every day of every month being “1-31”. This can also be broke down as far as hours, minutes and seconds.