aboutsummaryrefslogtreecommitdiff
path: root/share/hydractl/sync-backups
blob: 9cc79def95f26695f55ff08329c73d97ba9faf26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
#
# sync a removable volume with system backups.
#

# Parameters.
BASENAME="`basename $0`"
VOLUME="$1"
MEDIA="/media/$VOLUME"
BWLIMIT=${BWLIMIT:=0}
IMAGES="/var/data/crypt/"
CP="ionice -c 3 nice -n 19 cp"
WHOAMI="`whoami`"

# Set bandwidth limit
if [ "$BWLIMIT" != "0" ]; then
  BWLIMIT="--bwlimit=$BWLIMIT"
else
  BWLIMIT=""
fi

# Build rsync command line
RSYNC="ionice -c 3 nice -n 19 rsync -avH --delete $BWLIMIT"

# Sync backups for a node.
function sync_backups_node {
  if [ ! -z "$NODE" ]; then
    # Get full node hostname.
    NODE_HOSTNAME="`$SUDO cat /var/vservers/$NODE/etc/hostname`"
    if [ -z "$NODE_HOSTNAME" ]; then
      NODE_HOSTNAME="$NODE"
    fi

    # Sync local encrypted backup.
    echo "Syncing /var/vservers/$NODE/var/backups/duplicity/..."
    $SUDO mkdir -p /$MEDIA/$NODE_HOSTNAME/duplicity
    $SUDO $RSYNC /var/vservers/$NODE/var/backups/duplicity/ /$MEDIA/$NODE_HOSTNAME/duplicity/

    # Sync remote backups.
    for node in `$SUDO ls /var/vservers/$NODE/var/backups/remote/`; do
      echo "Syncing /var/vservers/$NODE/var/backups/remote/$node/..."
      $SUDO mkdir -p /$MEDIA/$NODE_HOSTNAME/remote/$node
      $SUDO $RSYNC /var/vservers/$NODE/var/backups/remote/$node/ /$MEDIA/$NODE_HOSTNAME/remote/$node/
    done
  else
    # Sync local encrypted backup.
    echo "Syncing /var/backups/duplicity/..."
    $SUDO mkdir -p /$MEDIA/$HOSTNAME/duplicity
    $SUDO $RSYNC /var/backups/duplicity/ /$MEDIA/$HOSTNAME/duplicity/

    # Sync remote backups.
    for node in `$SUDO ls /var/backups/remote/`; do
      echo "Syncing /var/backups/remote/$node/..."
      $SUDO mkdir -p /$MEDIA/$HOSTNAME/remote/$node/
      $SUDO $RSYNC /var/backups/remote/$node/ /$MEDIA/$HOSTNAME/remote/$node/
    done

    # Copy encrypted images.
    if [ -d "$IMAGES" ]; then
      for image in `find $IMAGES -name '*.img' -type f`; do
        echo "Copying image to /$MEDIA/$HOSTNAME/images/`dirname $image`"
        $SUDO mkdir -p /$MEDIA/$HOSTNAME/images/`dirname $image`
        $SUDO $CP $image /$MEDIA/$HOSTNAME/images/`dirname $image`
      done
    fi
  fi
}

# Parsing
if [ -z "$VOLUME" ]; then
  echo "usage: $BASENAME <media> [nodes]"
  exit 1
#elif [ "$WHOAMI" != 'root' ]; then
#  echo "Please run this command as the root user"
#  exit 1
else
  shift
fi

# Set sudo config
if [ "$WHOAMI" != 'root' ]; then
  SUDO="sudo"
fi

# Check volume name
if [ "$VOLUME" == "`hostname -f`" ]; then
  echo "volume is the hostname, cannot sync to myself"
  exit 1
fi

# Check if it is mounted
if ! mount | grep -q $MEDIA; then
  echo "volume $MEDIA is not mounted"
  exit 1
fi

# Detect if removable media is a system or a regular storage volume
if [ -d "$MEDIA/var/backups/remote" ]; then
  MEDIA="$MEDIA/var/backups/remote"
fi

# Sync backups for each node.
if [ ! -z "$*" ]; then
  for NODE in $*; do
    sync_backups_node
  done
else
  sync_backups_node
fi