From 60751a5f53c4905ee269f9dc16cdf2b23b7a245a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Sun, 28 Apr 2013 08:15:02 +0200 Subject: [PATCH] new post: automated backups with chef --- ...automated-backups-for-chef-server.markdown | 90 +++++++++++++++++++ .../downloads/code/chef-backup/backup-chef.sh | 16 ++++ .../code/chef-backup/restore-chef.sh | 16 ++++ 3 files changed, 122 insertions(+) create mode 100644 source/_posts/2013-04-27-automated-backups-for-chef-server.markdown create mode 100644 source/downloads/code/chef-backup/backup-chef.sh create mode 100644 source/downloads/code/chef-backup/restore-chef.sh diff --git a/source/_posts/2013-04-27-automated-backups-for-chef-server.markdown b/source/_posts/2013-04-27-automated-backups-for-chef-server.markdown new file mode 100644 index 0000000..18ad9a1 --- /dev/null +++ b/source/_posts/2013-04-27-automated-backups-for-chef-server.markdown @@ -0,0 +1,90 @@ +--- +layout: post +title: "Automated backups for Chef Server 11" +date: 2013-04-27 21:34 +comments: true +categories: +- chef +- opscode +- backup +- cron +- knife-backup +description: "Automated backups for Chef Server 11 using cron" +--- + +In this article I will share my setup, I use to backup chef server. +In the best case, you have a dedicated machine, which has network access to your chef +server. Otherwise you will have to additionally use a different backup program +like [rsnapshot](http://www.rsnapshot.org/) or +[duplicity](http://duplicity.nongnu.org/) to backup the created export +directory. In my case I use a raspberry pie with a +[hdd docking station](http://www.amazon.de/dp/B0017J4IAQ?tag=gitblo-21) and a +[power saving harddrive](http://www.amazon.de/dp/B004VFJ9MK?tag=gitblo-21) + +To get started you will need ruby on the backup machine. I prefer using rvm for +this job. Feel free to choose your preferred way: + +```bash +$ curl -L https://get.rvm.io | bash -s stable --autolibs=enabled +``` + +To create the backup, I use the great [knife-backup gem](https://github.com/mdxp/knife-backup) of [Marius Ducea](http://www.ducea.com/): + +```bash +$ gem install knife-backup +``` + +Then add these scripts to your system: + +```bash +$ mkdir -p ~/bin && cd ~/bin +$ wget http://blog.higgsboson.tk/downloads/code/chef-backup/backup-chef.sh +$ wget http://blog.higgsboson.tk/downloads/code/chef-backup/restore-chef.sh +$ chmod +x {backup,restore}-chef.sh +``` + +{% include_code restore script lang:bash chef-backup/backup-chef.sh %} + +{% include_code backup script lang:bash chef-backup/restore-chef.sh %} + +Modify BACKUP variable to match your backup destination. +Next you will need a knife.rb to get access to your server. +I suggest to create a new client: + +```bash +$ mkdir -p ~/.chef +$ knife client create backup --admin --file "$HOME/.chef/backup.pem" +$ cat <<'__EOF__' >> ~/.chef/knife-backup.rb +log_level :info +log_location STDOUT +node_name 'backup' +client_key "#{ENV["HOME"]}/.chef/backup.pem" +chef_server_url 'https://chef.yourdomain.tld' # EDIT HERE +syntax_check_cache_path "#{ENV["HOME"]}.chef/syntax_check_cache" +__EOF__ +$ knife role list # test authentication +``` + +Now test the whole setup, by running the `backup-chef.sh` script: + +```bash +$ ~/bin/backup-chef.sh +``` + +It should create a tar file in the backup directory. + +If everything works, you can add a cronjob to automate this. + +```bash +$ crontab -e +``` + + @daily $HOME/bin/backup-chef.sh + +To restore a backup simply run (where `DATE` is the date of the backup) + +```bash +$ ~/bin/restore-chef.sh /path/to/backup/DATE.tar.bz2 +``` + +That's all folks! diff --git a/source/downloads/code/chef-backup/backup-chef.sh b/source/downloads/code/chef-backup/backup-chef.sh new file mode 100644 index 0000000..47c14d3 --- /dev/null +++ b/source/downloads/code/chef-backup/backup-chef.sh @@ -0,0 +1,16 @@ +#!/bin/bash +# optional: load rvm +source "$HOME/.rvm/scripts/rvm" || source "/usr/local/rvm/scripts/rvm" + +cd /tmp + +BACKUP=/path/to/your/backup #<--- EDIT THIS LINE +TMPDIR=/tmp/$(mktemp -d chef-backup-XXXX) +MAX_BACKUPS=8 + +cd $TMPDIR +trap "rm -rf '$TMPDIR'" INT QUIT TERM EXIT +knife --config $HOME/.chef/knife-backup.rb backup export -D . >/dev/null +tar -cjf "$BACKUP/$(date +%m.%d.%Y).tar.bz2" . +# keep the last X backups +ls -t "$BACKUP" | tail -n+$MAX_BACKUPS | xargs rm -f diff --git a/source/downloads/code/chef-backup/restore-chef.sh b/source/downloads/code/chef-backup/restore-chef.sh new file mode 100644 index 0000000..854a5f3 --- /dev/null +++ b/source/downloads/code/chef-backup/restore-chef.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +if [ "$#" -eq 0 ]; then + echo "USAGE: $0 /path/to/backup" + exit 1 +fi + +source "$HOME/.rvm/scripts/rvm" || source "/usr/local/rvm/scripts/rvm" + +cd /tmp +TMPDIR=/tmp/$(mktemp -d chef-restore-XXXX) + +cd "$TMPDIR" +trap "rm -rf '$TMPDIR'" INT QUIT TERM EXIT +tar xf $1 +knife --config $HOME/.chef/knife-backup.rb backup restore -D .