This wiki has been archived and made read-only.
For up-to-date information about TkkrLab and it's projects please visit our main website at tkkrlab.nl.

Difference between revisions of "Laptop Backup"

From

Jump to: navigation, search
Line 12: Line 12:
 
* Backup to a NFS share at 192.168.12.75
 
* Backup to a NFS share at 192.168.12.75
 
* The NFS share is mounted at /mnt/nas/michiel/
 
* The NFS share is mounted at /mnt/nas/michiel/
* Backup only when eth0 or eth1 is connected and the laptop can ping to the NFS share.
+
* First backup: Laptop -> Nas (Laptop runs the backup script)
 +
* Second backup: Nas -> stack (External server runs the backup script)
  
== Before running... ==
+
== Laptop to Nas backup ==
 +
=== Information ===
 +
* Laptop is Debian based.
 +
* Backup to a NFS share at 192.168.12.75
 +
* The NFS share is mounted at /mnt/nas/michiel/
 +
* First backup: Laptop -> Nas
 +
* Laptop runs the backup script
 +
 
 +
=== Before running... ===
 
* Make sure the directory "backup" exist on the NFS share.
 
* Make sure the directory "backup" exist on the NFS share.
* Make sure screen is installed
+
* Make sure screen is installed on your laptop.
  
 
Run the following command at the root of the NFS share.
 
Run the following command at the root of the NFS share.
 
<syntaxhighlight lang="Bash">
 
<syntaxhighlight lang="Bash">
printf "Welcome01" | shasum -a 512 > .password
+
printf "Welcome01" > .password
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== Backup ==
+
=== Backup Script ===
/etc/network/interfaces
+
/opt/laptopbackup ("chmod +x /opt/laptopbackup" after creating file)
<syntaxhighlight lang="Bash">
+
# interfaces(5) file used by ifup(8) and ifdown(8)
+
auto lo
+
iface lo inet loopback
+
 
+
auto eth0
+
iface eth0 inet dhcp
+
post-up /etc/network/backup
+
 
+
auto eth1
+
iface eth1 inet dhcp
+
post-up /etc/network/backup
+
</syntaxhighlight>
+
 
+
/etc/network/backup (chmod +x /etc/network/backup after creating file)
+
<syntaxhighlight lang="Bash">
+
#!/bin/sh
+
: Check if script is not started before last boot.
+
FLAGFILE=/var/run/backup-is-started
+
 
+
case "$IFACE" in
+
    lo)
+
        # The loopback interface does not count.
+
        # only run when some other interface comes up
+
        exit 0
+
        ;;
+
    *)
+
        ;;
+
esac
+
 
+
if [ -e $FLAGFILE ]; then
+
    exit 0
+
else
+
    touch $FLAGFILE
+
fi
+
 
+
: Start backup
+
screen -S backup '/etc/network/backupscreen'
+
</syntaxhighlight>
+
 
+
/etc/network/backupscreen  (chmod +x /etc/network/backupscreen after creating file)
+
 
<syntaxhighlight lang="Bash">
 
<syntaxhighlight lang="Bash">
 
#!/bin/bash
 
#!/bin/bash
Line 82: Line 50:
 
   
 
   
 
: Check password in /mnt/nas/michiel/.password to make sure you are connected to the right NFS share.
 
: Check password in /mnt/nas/michiel/.password to make sure you are connected to the right NFS share.
if [ "$(printf "Welcome01" | shasum -a 512)" = "$(cat "/mnt/nas/michiel/.password")" ] ; then
+
if [ "$(cat "/mnt/nas/michiel/.password" | shasum -a 512)" = "eb6689158408aee35adb6b7ed148d5458073554439a184ad9b787d834c7eaa1df7103ddda2b31ff608de184d7d4f4d08f82bc86884a6ed0fa93e30d31e6bf055  -" ] ; then
 
   echo "password accepted"
 
   echo "password accepted"
  exit
 
 
else
 
else
 
   echo "Backup script: Wrong password at NFS share"
 
   echo "Backup script: Wrong password at NFS share"
Line 93: Line 60:
 
: Rsync the whole system except some directories.
 
: Rsync the whole system except some directories.
 
echo "Backup script: Rsync is running"
 
echo "Backup script: Rsync is running"
 +
rsync -aAXH --delete --info=progress2 /home/michiel /mnt/nas/michiel/backup/laptop/home/michiel
 +
echo "Backup script: Yeey, backup is finished"
 +
</syntaxhighlight>
 +
 +
To except some directories, replace rsync with the following command
 +
<syntaxhighlight lang="Bash">
 
rsync -aAXH --delete --info=progress2 --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found","/home/*/.thumbnails/*","/home/*/.cache/*","/home/*/.local/share/Trash/*","/home/*/.gvfs","/home/*/Downloads"} / /mnt/nas/michiel/backup
 
rsync -aAXH --delete --info=progress2 --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found","/home/*/.thumbnails/*","/home/*/.cache/*","/home/*/.local/share/Trash/*","/home/*/.gvfs","/home/*/Downloads"} / /mnt/nas/michiel/backup
 +
</syntaxhighlight>
  
echo "Backup script: Yeey, backup is finished"
+
===Run the script===
 +
To run the script, use the following command.
 +
<syntaxhighlight lang="Bash">
 +
sudo bash /opt/laptopbackup
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
Use sudo to make sure the permissions don't change. Also use bash instead op "./" or "sh" because otherwise you can get some bugs with the exclude parameters.
  
 
== Resources ==
 
== Resources ==

Revision as of 19:41, 20 December 2016

Project: Laptop Backup
360x360px
Name Laptop Backup
Initiator fridgefire
Status In progress, not working yet
Skills Bash magic
Summary A way to backup a laptop


Information

  • Laptop is Debian based.
  • Backup to a NFS share at 192.168.12.75
  • The NFS share is mounted at /mnt/nas/michiel/
  • First backup: Laptop -> Nas (Laptop runs the backup script)
  • Second backup: Nas -> stack (External server runs the backup script)

Laptop to Nas backup

Information

  • Laptop is Debian based.
  • Backup to a NFS share at 192.168.12.75
  • The NFS share is mounted at /mnt/nas/michiel/
  • First backup: Laptop -> Nas
  • Laptop runs the backup script

Before running...

  • Make sure the directory "backup" exist on the NFS share.
  • Make sure screen is installed on your laptop.

Run the following command at the root of the NFS share.

printf "Welcome01" > .password

Backup Script

/opt/laptopbackup ("chmod +x /opt/laptopbackup" after creating file)

#!/bin/bash
echo "start backup"
: check if we can ping 192.168.12.75
ping -q -c5 192.168.12.75 > /dev/null
if [ $? -eq 0 ] ; then
    echo "ping is succeeded"
else
    echo "Can not ping to server"
    exit 0
fi
 
: Mount NFS share at /mnt/nas/michiel
mount 192.168.12.75:/shares/michiel /mnt/nas/michiel
 
: Check password in /mnt/nas/michiel/.password to make sure you are connected to the right NFS share.
if [ "$(cat "/mnt/nas/michiel/.password" | shasum -a 512)" = "eb6689158408aee35adb6b7ed148d5458073554439a184ad9b787d834c7eaa1df7103ddda2b31ff608de184d7d4f4d08f82bc86884a6ed0fa93e30d31e6bf055  -" ] ; then
  echo "password accepted"
else
  echo "Backup script: Wrong password at NFS share"
  exit 0
fi
 
 
: Rsync the whole system except some directories.
echo "Backup script: Rsync is running"
rsync -aAXH --delete --info=progress2 /home/michiel /mnt/nas/michiel/backup/laptop/home/michiel
echo "Backup script: Yeey, backup is finished"

To except some directories, replace rsync with the following command

rsync -aAXH --delete --info=progress2 --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found","/home/*/.thumbnails/*","/home/*/.cache/*","/home/*/.local/share/Trash/*","/home/*/.gvfs","/home/*/Downloads"} / /mnt/nas/michiel/backup

Run the script

To run the script, use the following command.

sudo bash /opt/laptopbackup

Use sudo to make sure the permissions don't change. Also use bash instead op "./" or "sh" because otherwise you can get some bugs with the exclude parameters.

Resources