Difference between revisions of "Cloud Backup"

From Funtoo Linux
Jump to: navigation, search
(Configure etckeeper)
(Compile the kernel)
 
(35 intermediate revisions by one user not shown)
Line 6: Line 6:
  
 
= Use Case =
 
= Use Case =
A backup machine currently provides network drives on a home LAN to allow clients on the LAN to backup to, using apps such as Time Machine (Mac) and Genie Timeline (Windows). As this machine ''is'' the backup machine it doesn't have anywhere to backup itself to. In this situation a backup solution is provided by backing up to somewhere online - dropbox. If a restore from the backup is required, the client machine's backups would be trashed, and the backup machine restored.
+
A backup machine currently provides network drives on a home LAN to allow clients on the LAN to backup to, using apps such as Time Machine (Mac) and Genie Timeline (Windows). As this machine ''is'' the backup machine it doesn't have anywhere to backup to itself. In this situation a backup solution is provided by backing up to somewhere online - dropbox. If a restore from the backup is required, the client machine's backups would be trashed, and the backup machine restored.
  
 
= Automatic Backup Archives With Etckeeper =
 
= Automatic Backup Archives With Etckeeper =
Line 65: Line 65:
 
Note: because funtoo uses boot-update, this means /boot/grub/grub.cfg does not need to be archived.
 
Note: because funtoo uses boot-update, this means /boot/grub/grub.cfg does not need to be archived.
  
To ensure you kernel config is archived, make the following link:
+
To ensure your portage world file is archived, make the following link:
 
<pre>
 
<pre>
 
# ln /var/lib/portage/world /etc/world
 
# ln /var/lib/portage/world /etc/world
Line 78: Line 78:
 
If you don't already have cron installed, do it now.
 
If you don't already have cron installed, do it now.
 
<pre>
 
<pre>
emerge vixie-cron
+
# emerge vixie-cron
 
</pre>
 
</pre>
 
And write the cron job to save an hourly version of /etc.
 
And write the cron job to save an hourly version of /etc.
Line 152: Line 152:
 
</pre>
 
</pre>
  
After starting the dropbox deamon, it will provide a http link. You will need to visit this site just once to associate this computer with your dropbox account.
+
After starting the dropbox daemon, it will provide a http link. You will need to visit this site just once to associate your computer with your dropbox account.
  
 
Write the cron job to make the backup archive and move it online.
 
Write the cron job to make the backup archive and move it online.
Line 184: Line 184:
 
mv -v -f router.gpg /home/dropbox/Dropbox/Private/
 
mv -v -f router.gpg /home/dropbox/Dropbox/Private/
 
</pre>
 
</pre>
 +
Remember to change "encryption_password"
  
 
{{fancywarning|If you forget this password the backup will be unusable. Lose the password and you lose the backup.}}
 
{{fancywarning|If you forget this password the backup will be unusable. Lose the password and you lose the backup.}}
Line 193: Line 194:
  
 
==== Private/Public key Encryption ====
 
==== Private/Public key Encryption ====
Make a private/public key pair.
+
Make a private/public encryption/decryptions key pair. The public key will be used to encrypt and the private key to decrypt.
 
<pre>
 
<pre>
 
# gpg --gen-key
 
# gpg --gen-key
 
</pre>
 
</pre>
The public key is used to create the encrypted back and needs to live on this machine. A copy of the private key needs to be made and stored securely in another place. If this machine becomes unbootable, and this is the only place the private key lives, the backup dies with it.
+
The public key is used to create the encrypted backup and needs to live on the computer being backed up. A copy of the private key needs to be made and stored securely in another place. If this machine becomes unbootable, and this is the only place the private key lives, the backup dies with it.
 
The private key should not be kept:
 
The private key should not be kept:
 
# In the same place as the back up
 
# In the same place as the back up
 
# On the machine being backed up
 
# On the machine being backed up
{{fancywarning|The private key is the only key that will decrypt the backup. Lose this key and/or it's password you lose the backup.}}
+
{{fancywarning|The private key is the only key that will decrypt the backup. Lose this key and/or it's password and you lose the backup.}}
 
List the private keys.
 
List the private keys.
 
<pre>
 
<pre>
Line 218: Line 219:
 
To create a key file.
 
To create a key file.
 
<pre>
 
<pre>
# gpg -o private_encryption.gpgkey --export-secret-key 0EF13559
+
# gpg -o private_decryption.gpgkey --export-secret-key 0EF13559
 
</pre>
 
</pre>
 
Now store this key somewhere secure. The backup is only as secure as the private key.
 
Now store this key somewhere secure. The backup is only as secure as the private key.
 +
 
Modify the cron job. /etc/cron.daily/backup
 
Modify the cron job. /etc/cron.daily/backup
 
<pre>
 
<pre>
Line 234: Line 236:
  
 
==== Sign Backups ====
 
==== Sign Backups ====
Create a 2nd private/public (signing) key pair.
+
Create a 2nd private/public (signing) key pair. The private key is used to sign and the public key is used to check the authenticity/integrity.
 
<pre>
 
<pre>
 
# gpg --gen-key
 
# gpg --gen-key
Line 241: Line 243:
 
In this case the private key is required to sign the backup and the public key is used to check the integrity of the backup.
 
In this case the private key is required to sign the backup and the public key is used to check the integrity of the backup.
 
Follow a similar process as above to copy the public key to to another computer/storage media.
 
Follow a similar process as above to copy the public key to to another computer/storage media.
 +
 
List the private keys
 
List the private keys
 
<pre>
 
<pre>
Line 246: Line 249:
 
</pre>
 
</pre>
 
Note: -K lists private keys while -k lists public keys.
 
Note: -K lists private keys while -k lists public keys.
Then export this public key.
+
 
 +
Then export this public key via cut and paste.
 
<pre>
 
<pre>
# gpg -a --export <key name/number>
+
# gpg -a --export <key name or number>
 
</pre>
 
</pre>
To create a key file
+
Or to create a key file.
 
<pre>
 
<pre>
# gpg -o public_signing.gpgkey --export <key name/number>
+
# gpg -o public_signing.gpgkey --export <key name or number>
 
</pre>
 
</pre>
 
Now store this key somewhere secure.
 
Now store this key somewhere secure.
 +
 
Modify the backup cron job.
 
Modify the backup cron job.
 
/etc/cron.daily/backup  
 
/etc/cron.daily/backup  
Line 274: Line 279:
 
This restore will assume your are starting with a new blank disk.
 
This restore will assume your are starting with a new blank disk.
 
Start by performing a stage 3 install, upto and including section 5 "Chroot into your new system." http://www.funtoo.org/wiki/Funtoo_Linux_Installation
 
Start by performing a stage 3 install, upto and including section 5 "Chroot into your new system." http://www.funtoo.org/wiki/Funtoo_Linux_Installation
 +
 +
Then the restore process is:
 
# Download backup from dropbox
 
# Download backup from dropbox
 
# Decrypt
 
# Decrypt
Line 284: Line 291:
  
 
== Download backup from dropbox ==
 
== Download backup from dropbox ==
Log into your dropbox account and find you backup file. Move it to a public area if it isn't already in one. Then right click on it and click "copy public link."
+
Log into your dropbox account and find your backup file. Move it to a public area if it isn't already in one. Then right click on it and click "copy public link."
 
Now on the computer to be restored, delete the contents of the /etc folder and download the backup file.
 
Now on the computer to be restored, delete the contents of the /etc folder and download the backup file.
  
Line 296: Line 303:
 
(Again http is all 1 word, but was blocked by the spam filter).
 
(Again http is all 1 word, but was blocked by the spam filter).
  
Note: If you have to copy this from another computer and therefore can not cut and paste it, there is a "shorten link" option
+
Note: If you have to copy the link from another computer and therefore can not cut and paste it, there is a "shorten link" option
  
 
== Decrypt ==
 
== Decrypt ==
Line 302: Line 309:
  
 
Note:
 
Note:
# The decryption key is the private key of the encryption key pair - private_encryption.gpgkey
+
# The decryption key is the private key of the encryption key pair - private_decryption.gpgkey
 
# The signing key is the public key of the signing key pair - public_signing.gpgkey
 
# The signing key is the public key of the signing key pair - public_signing.gpgkey
  
Line 314: Line 321:
 
To import the keys by file.
 
To import the keys by file.
 
<pre>
 
<pre>
# gpg --import private_encryption.gpgkey
+
# gpg --import private_decryption.gpgkey
 
# gpg --import public_signing.gpgkey
 
# gpg --import public_signing.gpgkey
 
</pre>
 
</pre>
Line 321: Line 328:
 
<pre>
 
<pre>
 
# gpg -d backup.gpg > backup.bundle
 
# gpg -d backup.gpg > backup.bundle
 +
</pre>
 +
 +
If the backup was signed and you have correctly imported the signing public key you should see a message similar to:
 +
<pre>
 +
gpg: Good signature from "my_signing_key <noone@example.com>"
 
</pre>
 
</pre>
  
 
== Clone ==
 
== Clone ==
 
<pre>
 
<pre>
git clone /tmp/backup.bundle /etc
+
git clone /tmp/backup.bundle /etc/
 
</pre>
 
</pre>
  
Line 340: Line 352:
 
</pre>
 
</pre>
  
== Compile the kernel ==
+
== Compile the kernel (genkernel)==
 +
If you have genkernel set to save config files (the default).
 +
<pre>
 +
# cp /etc/kernels/kernel-config-x86_64-<latest version>-gentoo /usr/src/linux/.config
 +
</pre>
 +
Otherwise use the currently loaded kernel's config
 +
<pre>
 +
# zcat /proc/config.gz > /usr/src/linux/.config
 +
</pre>
 +
Then compile the kernel
 
<pre>
 
<pre>
# zcat /proc/config.gz > .config 
+
# genkernel --oldconfig --no-mrproper all  
# genkernel --oldconfig --no-mrproper all  
+
 
</pre>
 
</pre>
  
Line 351: Line 371:
 
# boot-update
 
# boot-update
 
</pre>
 
</pre>
 +
Adjust the device as required if installing to another location.
  
 
== Reboot ==
 
== Reboot ==

Latest revision as of 23:42, 21 January 2012


This howto will describe a method for automatically backing up your funtoo install to the internet, in this case dropbox, but any online storage will do. Gentoo describes a method of creating a stage 4 archive. The problem with a stage 4 is that it is large and it archives a lot of unnecessary files. Such as applications that can be reinstalled from an emerge world. Instead, this method will aim for more of a stage 3.5.

Note: this method does not attempt to backup everything. The intention is only to backup the system. Optionally you can also archive and copy your /home folder if you have enough online storage.

Contents

[edit] Use Case

A backup machine currently provides network drives on a home LAN to allow clients on the LAN to backup to, using apps such as Time Machine (Mac) and Genie Timeline (Windows). As this machine is the backup machine it doesn't have anywhere to backup to itself. In this situation a backup solution is provided by backing up to somewhere online - dropbox. If a restore from the backup is required, the client machine's backups would be trashed, and the backup machine restored.

[edit] Automatic Backup Archives With Etckeeper

Etckeeper is a tool that is used to save versions of /etc, including meta-data in a version control repository such as git. As etckeeper is not in the funtoo portage tree, layman is used to provide an overlay.

[edit] Install etckeeper via layman

Before you install layman it is worth mentioning that you probably want USE="git subversion" in /etc/make.conf. After adjusting use flags, to install layman you run:

# emerge layman

In order to backup the layman configuration, but not the portage overlay trees, make the following modifications to the default install. Tell Portage about layman-fetched repositories by adding the following line to /etc/make.conf

source /etc/layman/make.conf

Modify the following lines in /etc/layman/layman.cfg

storage   : /var/lib/layman
installed : /etc/layman/installed.xml
make_conf : /etc/layman/make.conf

Add the bgo-overlay. As described on their web page, bgo.zugaina.org.

# layman -o   h t t p://gpo.zugaina.org/lst/gpo-repositories.xml -L
# layman -a bgo-overlay -o   h t t p://gpo.zugaina.org/lst/gpo-repositories.xml

(http is all one word but was blocked by the spam filter)

More information about layman can be found here: http://www.gentoo.org/proj/en/overlays/userguide.xml

Then unmask and install etckeeper.

# emerge etckeeper --autounmask-write
# emerge etckeeper 

Note: To update layman overlays do:

# layman -S

At the time of writing, there was a problem with the etckeeper (sys-apps/etckeeper-0.58-r2) ebuild which needed fixed by doing the following. If you see the following error then apply this fix:

# emerge etckeeper
Calculating dependencies... done!
>>> Verifying ebuild manifests
!!! A file is not listed in the Manifest: '/var/lib/layman/bgo-overlay/sys-apps/etckeeper/files/etckeeper-gentoo-0.58.patch'

# cd /var/lib/layman/bgo-overlay/sys-apps/etckeeper
# ebuild etckeeper-0.58-r2.ebuild manifest
# emerge etckeeper

[edit] Configure etckeeper

Move any config files that do not live in /etc. i.e. Check /root for any files to be archive, such as iptables scripts and move them to /etc.

Note: because funtoo uses boot-update, this means /boot/grub/grub.cfg does not need to be archived.

To ensure your portage world file is archived, make the following link:

# ln /var/lib/portage/world /etc/world

Initialise the git repository.

# etckeeper init
Initialized empty Git repository in /etc/.git/
# etckeeper commit "Initial commit."

If you don't already have cron installed, do it now.

# emerge vixie-cron

And write the cron job to save an hourly version of /etc. Note: git will only create a new version (commit) if there are changes from the previous one.

/etc/cron.hourly/etckeeper

#! /bin/bash
etckeeper commit "Hourly auto-commit"

[edit] Encrypt and copy backups online

[edit] Copy To Dropbox

# emerge dropbox

Add a dropbox user

# useradd dropbox

Write the dropbox init files.

/etc/conf.d/dropbox

DROPBOX_USERS="dropbox" 

/etc/init.d/dropbox

#!/sbin/runscript 
# Copyright 1999-2004 Gentoo Foundation 
# Distributed under the terms of the GNU General Public License, v2 or later 
# $Header: /var/cvsroot/gentoo-x86/sys-fs/dropbox/files/dropbox.init-1.0,v 1.4 2007/04/04 13:35:25 cardoe Exp $ 

NICENESS=5 

depend() { 
    need localmount net 
    after bootmisc 
} 

start() { 
    ebegin "Starting dropbox..." 
    for dbuser in $DROPBOX_USERS; do 
        start-stop-daemon -S -b -m --pidfile /var/run/dropbox-$dbuser.pid  -N $NICENESS -u $dbuser -v -e HOME="/home/$dbuser" -x /opt/dropbox/dropboxd 
    done 
    eend $? 
} 

stop() { 
    ebegin "Stopping dropbox..." 
    for dbuser in $DROPBOX_USERS; do 
        start-stop-daemon --stop --pidfile /var/run/dropbox-$dbuser.pid 
    done 
    eend $? 
} 

status() { 
    for dbuser in $DROPBOX_USERS; do 
        if [ -e /var/run/dropbox-$dbuser.pid ] ; then 
            echo "dropboxd for USER $dbuser: running." 
        else 
            echo "dropboxd for USER $dbuser: not running." 
        fi 
    done 
    eend $? 
}

Start dropbox now and at boot time.

# chmod 0755 /etc/init.d/dropbox 
# /etc/init.d/dropbox start
# rc-update add dropbox default

After starting the dropbox daemon, it will provide a http link. You will need to visit this site just once to associate your computer with your dropbox account.

Write the cron job to make the backup archive and move it online. /etc/cron.daily/backup

#! /bin/bash
cd /etc
git bundle create /tmp/backup.bundle --all
cd /tmp
mv -v -f backup.bundle /home/dropbox/Dropbox/Private/

Make the script executable.

# chmod +x /etc/cron.daily/backup 

[edit] Encrypt Backups

It is a good idea to encrypt your backup before moving it online. This can be done with gpg, using a symmetric (password only) or public/private key encryption. Additionally you can chose to sign the backup to check its integrity before restoring.

# emerge gpg

[edit] Symmetric Encryption

There is no preparation required to use a symmetric key as all that is required is simply a passphrase. Just modify the cron job. /etc/cron.daily/backup

#! /bin/bash
cd /etc
git bundle create /tmp/backup.bundle --all
cd /tmp
echo 'encryption_password' | gpg -o backup.gpg --batch --homedir /root/.gnupg -vvv  --passphrase-fd 0 --yes -c backup.bundle
mv -v -f router.gpg /home/dropbox/Dropbox/Private/

Remember to change "encryption_password"

If you forget this password the backup will be unusable. Lose the password and you lose the backup.


As there is now sensitive information in this file you might want to remove read permission.

# chmod og-r /etc/cron.daily/backup 

[edit] Private/Public key Encryption

Make a private/public encryption/decryptions key pair. The public key will be used to encrypt and the private key to decrypt.

# gpg --gen-key

The public key is used to create the encrypted backup and needs to live on the computer being backed up. A copy of the private key needs to be made and stored securely in another place. If this machine becomes unbootable, and this is the only place the private key lives, the backup dies with it. The private key should not be kept:

  1. In the same place as the back up
  2. On the machine being backed up

The private key is the only key that will decrypt the backup. Lose this key and/or it's password and you lose the backup.


List the private keys.

# gpg -K
/root/.gnupg/secring.gpg
------------------------
sec   2048R/0EF13559 2012-01-21
uid                  my_key <noone@example.com>
ssb   2048R/67417FEB 2012-01-21

The private key can be exported using either the key name or key number. In this case "my_key" or "0EF13559". To cut and paste the key. Ie, if logging in remotely.

# gpg -a --export-secret-key 0EF13559

To create a key file.

# gpg -o private_decryption.gpgkey --export-secret-key 0EF13559

Now store this key somewhere secure. The backup is only as secure as the private key.

Modify the cron job. /etc/cron.daily/backup

#! /bin/bash
cd /etc
git bundle create /tmp/backup.bundle --all
cd /tmp
gpg -o backup.gpg -r 'my-key' --batch --homedir /root/.gnupg -vvv  --passphrase-fd 0 --yes -e backup.bundle
mv -v -f backup.gpg /home/dropbox/Dropbox/Private/

Replace "my-key" with the appropriate name from the key list. Also note the change from -c for symmetric encryption to -e for private/public key encryption

[edit] Sign Backups

Create a 2nd private/public (signing) key pair. The private key is used to sign and the public key is used to check the authenticity/integrity.

# gpg --gen-key

Note: The password for this key will be required in the script below. In this case the private key is required to sign the backup and the public key is used to check the integrity of the backup. Follow a similar process as above to copy the public key to to another computer/storage media.

List the private keys

# gpg -k

Note: -K lists private keys while -k lists public keys.

Then export this public key via cut and paste.

# gpg -a --export <key name or number>

Or to create a key file.

# gpg -o public_signing.gpgkey --export <key name or number>

Now store this key somewhere secure.

Modify the backup cron job. /etc/cron.daily/backup

#! /bin/bash
cd /etc
git bundle create /tmp/backup.bundle --all
cd /tmp
echo 'signing_key_password' | gpg -s -o backup.gpg -r 'my-encryption-key' --batch --homedir /root/.gnupg -vvv  --passphrase-fd 0 --yes -e backup.bundle 
mv -v -f backup.gpg /home/dropbox/Dropbox/Private/

Note: the script will require the password for your private (signing) key to sign the backup. Replace "password" with the password for your signing private key. And as there is sensitive information in this file don't forget to remove read permission.

# chmod og-r /etc/cron.daily/backup 

[edit] To Restore From A Backup

This restore will assume your are starting with a new blank disk. Start by performing a stage 3 install, upto and including section 5 "Chroot into your new system." http://www.funtoo.org/wiki/Funtoo_Linux_Installation

Then the restore process is:

  1. Download backup from dropbox
  2. Decrypt
  3. Clone
  4. Link world file
  5. Emerge world
  6. Compile the kernel
  7. Restore grub bootloader
  8. Reboot

[edit] Download backup from dropbox

Log into your dropbox account and find your backup file. Move it to a public area if it isn't already in one. Then right click on it and click "copy public link." Now on the computer to be restored, delete the contents of the /etc folder and download the backup file.

(Need to check if this needs done before chrooting into the new install).

# cd /etc
# rm -rf *
# cd /tmp
# wget h t t p://dl.dropbox.com/link-to-backup-file/backup.gpg

(Again http is all 1 word, but was blocked by the spam filter).

Note: If you have to copy the link from another computer and therefore can not cut and paste it, there is a "shorten link" option

[edit] Decrypt

If you used a public/private key to encrypt, and optionally signed the backup, import the decryption and signing keys.

Note:

  1. The decryption key is the private key of the encryption key pair - private_decryption.gpgkey
  2. The signing key is the public key of the signing key pair - public_signing.gpgkey

To import the keys by cut and paste.

# gpg --import <<EOF

Note: The last line after pasting the key should be "EOF" Repeat for both keys.

To import the keys by file.

# gpg --import private_decryption.gpgkey
# gpg --import public_signing.gpgkey

Decrypt the backup.

# gpg -d backup.gpg > backup.bundle

If the backup was signed and you have correctly imported the signing public key you should see a message similar to:

gpg: Good signature from "my_signing_key <noone@example.com>"

[edit] Clone

git clone /tmp/backup.bundle /etc/

[edit] Link world file

# ln /etc/world /var/lib/portage/world

[edit] Emerge world

# emerge --sync
# layman -S
# emerge -uDaNv world

[edit] Compile the kernel (genkernel)

If you have genkernel set to save config files (the default).

# cp /etc/kernels/kernel-config-x86_64-<latest version>-gentoo /usr/src/linux/.config 

Otherwise use the currently loaded kernel's config

# zcat /proc/config.gz > /usr/src/linux/.config 

Then compile the kernel

# genkernel --oldconfig --no-mrproper all 

[edit] Restore grub bootloader

# grub-install --no-floppy /dev/sda
# boot-update

Adjust the device as required if installing to another location.

[edit] Reboot

# reboot
Personal tools
Namespaces

Variants
Actions
Categories
Toolbox
Stuff