Automating backup process using ssh-keys:

Links to difrent topics on this page:

Examples on how to use cs-rsync-backup:

Example usage and explenation of the script:

user@machine:~$ cs-rsync-backup

  • running the script without options will make a backup copy of all your folders and files to your homedir except for the NoCsRsync subdir(s)

If you want to use the script in combination with a ssh key and only backup certain files:

user@machine:~$ cs-rsync-backup -v -k /paht/to/your/.ssh/key.priv --nohome --files-from=filetorsync . -r

  • -v activates verbose mode
  • -k wil indicate that you want to use a ssh key with the path to the key behind it, see using ssh key with cs-rsync-backup
  • --nohome indicates that you do not want to copy all the files on your homedir
  • -files-from=filetorsync . is a rscync option to read from a file which files you want to back up
    • for easy usage the file should be in the directory you are in
    • the paths in the file start from the location where the script is executed
    • the point almost at the end is important
    • -r to recurse into directories, this is important if you have directories listed in the filetorsync

user@machine:~$ cs-rsync-backup -v -k /paht/to/your/.ssh/key.priv --nohome --files-from=filetorsync . -r --exclude filetoexclude1 --exclude filetoexclude2

  • a lot of the same options as in the example above just to show that you can still exclude certain files while using a filetorsync document
  • --exclude filename will let you exclude certain files

Automatic mail when backup is too old:

If you want to recieve a mail in case your backup is too old consider to make a crontab on your departemental homedir server.

  • make a crontab using: crontab -e
  • put the following line in the file:
    25 15 * * 1-5 /usr/local/bin/cs-check-backups 7 | mail -e -s 'check backups' firstname.lastname@cs.kuleuven.be
  • you can check with: crontab -l

This crontab will do a check every workday at 15:25 and send you an email if your backup is older then 7 days.

Restoring a file

If you accidently deleted a file or want last week's version or ..., you can restore this by ssh'ing to ssh.cs.kuleuven.be. Go to the directory /home/USERNAME/BACKUP/ and look for the file you need. Once found you can restore this by issuing the commands:

cd /home/USERNAME/BACKUP/somefolder/whereyourfile/is/located/
rsync -avz filename yourmachinename.dyn.cs.kuleuven.be:/home/USERNAME/somefolder/whereyourfile/islocated/filename-backup

This will copy the file to the specified directory on your machine.

If you can't find te file or you want a specific version of the file please lookt at the Tivoli documentation.

Further automating cs-rsync-backup

This guide provides an example of how you can make a ssh-key without password so you are able to use backup whithout the need for a password. You are then able to use it in an automated script, this is an example but we do not recomend using it. When putting the backup in a script you have less control of when it is activated. This can cause problems.

If you follow these steps your backup will be placed in your departemental homedir under: /home/USERNAME/BACKUP/. The homedir can by reached by connecting to (almost) any machine managed by the systemgroup.

  • First thing to do is create ssh keys without a password
  • person@pc:/proc$ ssh-keygen
    Generating public/private rsa key pair.
    Enter file in which to save the key (/home/person/.ssh/id_rsa):
    /home/person/.ssh/backupkey <== choose the filename (otherwise you will overwrite your default ssh-key!)
    Enter passphrase (empty for no passphrase): <== leave empty
    Enter same passphrase again: <== leave empty
    Your identification has been saved in /home/person/.ssh/backupkey.
    Your public key has been saved in /home/person/.ssh/backupkey.pub.

  • Next step is to allow this key to access your homedir on the departemental systems:
    • For this you need to add the generated public key to your authorized_keys file (this file is located on your departemental homedir in /home/username/. ssh/authorized_keys)
    • Important! This is currently an insecure situation. To correct this you need to add the following line infront of the key you just added to the authorized_keys file (on the same line!):
    •  command="$HOME/bin/validate-cs-rsync.sh",no-pty,no-port-forwarding,no-X11-forwarding,no-agent-forwarding

       This would create for example the following line in your authorize_keys file:

       command="$HOME/bin/validate-cs-rsync.sh",no-pty,no-port-forwarding,no-X11-forwarding,no-agent-forwarding ssh-rsa AAAAB3Nza
       C1yc2EAAAABIwAAAQEAo6NN5AxjGRk0c6lh8pRIp5e/v2T5ALeZotQyo0BfZA9Ny74pHg/Gi/OWMF7zTDh/+YdMYDP2kh3wfpzL
       NX3WaQID+NWe0T9T5z3PIevNvH9JzcCOSeYaW9fjXtEXewjucZKuDJZIa5rAgi79ZSGLRFlYqzksHmEFOIs7NFlbX+D2kqMNYR9
       cSAww0U946XU7wjX69wBP98kochup/OIavbmOe4vZyejh4o8MoKnN4cnv6tGj337TSkEHC4ohdLlBKGAYR9NFUE6Qk3gbcbngRY
       aOGf0f68QwBV5Bx4lTEfZ0vhUthWTveXq3FyvfNcZi68QSVYu9vfI46T68cPM+4Q== user@hostname

    • The $HOME/bin/validate-cs-rsync.sh part depends on where you put the file in the following step

  • After this you will need to make a link to /usr/local/cs-adm/bin/validate-cs-rsync.sh. This will deny the execution of all commands apart from the ones we allow/need. As in the previous step this will have to be done somewhere in your departemental homedir!
            cd $HOME/bin
            ln -s /usr/local/cs-adm/bin/validate-cs-rsync.sh validate-cs-rsync.sh
            chown user:group validate-cs-rsync.sh
          

    If the command "cs-check-backups" is sent, "/usr/local/bin/cs-check-backups 7" will be executed on the server. "/usr/local/bin/cs-check-backups 7" is to allow the key to also check when the latest backup was took. The "7" means "give output if the latest backup was taken more then 7 days ago". Change it if needed. This will be used later on to check if a backup should be taken.

To summarize the automation process

  • What you need on your departemental homedir:
    • Your passwordless public key with command="$HOME/bin/validate-cs-rsync.sh",no-pty,no-port-forwarding,no-X11-forwarding,no-agent-forwarding in your authorized_keys file
    • The validate-cs-rsync.sh file
  • On your laptop/desktop:
    • The passwordless ssh-keys
    • You can now use this key in combination with the cs-rsync-backup script using the -k | --key option.
      example: cs-rsync-backup -k /home/user/.ssh/yourprivatekey
    • You might be tempted using the script in a startup script but we advise against this. This could cause a lot of trouble.