4

I am trying to set up a shell script under Debian GNU/Linux to backup a directory and subdirectories to a remote NAS running Linux over SSH. This connection has to be encrypted.

I have tried a couple different approaches involving rsync rsh and such but without luck. I know how to set up the crontab, but I need a script that will run unattended (i.e., it won't ask for password)

Could anyone help me?

EDIT: I can successfully login without a password now.

The problem now is to get rsync to copy the files in /backups/ to [remote system]:/backups/

It only copies one folder from /backups/ to [remote system]:/backups/.

EDIT: Seems local system has one /backups and one /backup... I have tried with /backups when I should have used /backup...

Final command: rsync -avzr -e ssh /backup/ admin@[IP]:/backups/

3 Answers 3

2

rsync since 2.6.x defaults to using ssh for transfers. So it's already taken care of as long as your remote NAS rsync is supported.

As to password less unattended transfers, setup ssh key based authentication for the user you are running the cron as and that should be done. Here is how you set it up -- http://www.cyberciti.biz/tips/ssh-public-key-based-authentication-how-to.html

24
  • Ok, would you care explaining this step-by-step? I tried something called keychain without luck... Aug 24, 2012 at 17:41
  • updated my answer :)
    – Chida
    Aug 24, 2012 at 17:44
  • Ok thanks, now logging in works without a password. I'll just google how to use rsync and report back if I can't get it to work! Aug 24, 2012 at 17:48
  • how should the rsync command look like? It does copy recursive directories but not files... Aug 24, 2012 at 18:15
  • 3
    I think it's time to edit both the question and answer to contain some of the info in this comment thread -- You guys are tripping automatic alarms for too many comments. If the issue is really that complex it may not be a good fit for a Q&A site...
    – voretaq7
    Aug 24, 2012 at 19:31
2

You can also pass this flag to rsync to use a file for the password:

--password-file

Can easily chmod the file to 400 so no one but your user can read that file (and root of course)

2

Ssh Key syncing you said you had this working but i thought i would still post the steps

ssh in to your NAS

To make the Key

ssh-keygen -t rsa on local machine 

Then sync to the computer you will be backing up from

cat ~/.ssh/id_rsa.pub | ssh user@hostname    ‘cat>>.ssh/authorized_keys’

Rync Backup script -This will make a backup folder for today ,then sycn yesterdays files todays this will reduce the load on the network, then it will rsync todays to be backed up.

#!/bin/sh

mkdir -p /storage/backups/`date +\%Y-\%m-\%d`-`date +\%A`/$host/$username

rsync -avz /storage/backups/`date --date=yesterday +\%Y-\%m-\%d`-`date--date=yesterday    +\%A`/$host/$username/ /storage/backups/`date +\%Y-\%m-\%d`-`date +\%A`/$host/$username/

rsync -avz -e ssh  /home/username/ /storage/backups/`date +\%Y-\%m-\%d`-`date +\%A`/$host/$username/

You will also need a script which will clean up the backups: this will keep a week backup ,I can confirm that this works on Netgear's ready nas

#!/bin/sh

# Definitions
sevendaysago=$(date --date='6 days ago' +%Y-%m-%d-%A)

# Delete backups from 7 days ago
rm -rf /storage/backups/$sevendaysago

you will need to run this to make scripts runable

chmod u+x script.sh

in cron you will just need to add via crontab -e

@daily sh backupscript
1 17 * * * sh dailycleanup
2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .