summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xinu.at>2015-09-13 10:04:36 +0200
committerFlorian Pritz <bluewind@xinu.at>2015-09-13 10:04:36 +0200
commitf4a93f70044c213b2e3d97d61af972ab1bba19a2 (patch)
tree3f7663f9501fd9c9f444072c0a2aa7dee8d7562e
parent09d7c955d907bf09c5a63c85d2f0c6d7e47ad6ea (diff)
downloadbin-f4a93f70044c213b2e3d97d61af972ab1bba19a2.tar.gz
bin-f4a93f70044c213b2e3d97d61af972ab1bba19a2.tar.xz
backup.sh: Rwrite the script
Signed-off-by: Florian Pritz <bluewind@xinu.at>
-rwxr-xr-xbackup.sh131
1 files changed, 115 insertions, 16 deletions
diff --git a/backup.sh b/backup.sh
index d32d982..74d855d 100755
--- a/backup.sh
+++ b/backup.sh
@@ -5,30 +5,76 @@
#
# Important steps:
# - define a host "backup" in root's .ssh/config
-# - create the globbing filters or remove the argument to backup()
# - read the script and adjust to your needs
-if [[ $UID != 0 ]]; then
- echo "Error: need root, restarting with sudo" >&2
- exec sudo "$0" "$@"
-fi
+set -e
-export HOME=/root
+main() {
+ if [[ $UID != 0 ]]; then
+ exec sudo "$0" "$@"
+ fi
-# if you want to encrypt the backups remove --no-encryption in the duplicity call
-# and uncomment the lines that contain PASSPHRASE
-#PASSPHRASE="randomstringhere"
+ TMPDIR="$(mktemp -d "/tmp/${0##*/}.XXXXXX")"
+ trap "rm -rf '${TMPDIR}'" EXIT TERM
+
+ # ensure duplicity keeps its cache at a central location
+ export HOME=/root
+
+ # if you want to encrypt the backups remove --no-encryption in the duplicity call
+ # and uncomment the lines that contain PASSPHRASE
+ #PASSPHRASE="randomstringhere"
+
+ # these mountpoints will be excluded
+ excludeMountpoints=(
+ /tmp/
+ /sys/
+ /dev/
+ /proc/
+ /run/
+ /mnt/levant/nfs/
+ /media/
+ )
+
+ # first line that matches wins
+ IFS='' read -r -d '' excludeList <<EOF || true
++ /home/flo/.local/share/Steam/steamapps/common/Counter-Strike Global Offensive/csgo/cfg
+- /home/*/.local/share/Steam/steamapps/common/*/*
+- /home/*/.cache/*
+- /home/*/.claws-mail/*
+- /root/.cache/*
+- /var/cache/pacman/pkg/*
+EOF
+
+ exclude_mountpoints
+ #echo "$excludeList"
+
+ # save some data that's useful for restores
+ backupDataDir=/root/backup-data/
+ mkdir -p "$backupDataDir"
+ fdisk -l > "$backupDataDir/fdisk"
+ vgdisplay > "$backupDataDir/vgdisplay"
+ pvdisplay > "$backupDataDir/pvdisplay"
+ lvdisplay > "$backupDataDir/lvdisplay"
+ lvdisplay > "$backupDataDir/lvdisplay"
+ df -a > "$backupDataDir/df"
+ findmnt -l > "$backupDataDir/findmnt"
+
+ # this does not ignore /proc and network mounts so it's not that useful :(
+ #find / | gzip > /root/full-file-list.txt.gz
+
+ backup / sftp://backup/$HOSTNAME-backup/full-backup/ --exclude-filelist <(echo "$excludeList")
+}
backup() {
- src=$1
- dest=$2
+ local src=$1
+ local dest=$2
shift 2
#export PASSPHRASE
duplicity \
- --exclude-other-filesystems \
-v2 \
- --volsize 50 \
+ --numeric-owner \
+ --volsize 150 \
--allow-source-mismatch \
--asynchronous-upload \
--full-if-older-than 30D \
@@ -39,6 +85,59 @@ backup() {
#export PASSPHRASE=""
}
-backup / pexpect+sftp://backup/$HOSTNAME-backup/root/ --exclude-globbing-filelist /root/duplicity_root_filter
-backup /home/ pexpect+sftp://backup/$HOSTNAME-backup/home/ --exclude-globbing-filelist /root/duplicity_home_filter
-backup /boot/ pexpect+sftp://backup/$HOSTNAME-backup/boot/
+### support functions below ###
+
+##
+# usage : in_array( $needle, $haystack )
+# return : 0 - found
+# 1 - not found
+##
+in_array() {
+ local needle=$1; shift
+ local item
+ for item in "$@"; do
+ [[ $item = "$needle" ]] && return 0 # Found
+ done
+ return 1 # Not Found
+}
+
+# same as in_array except 0 is returned if any item in haystack starts with needle
+in_array_startswith() {
+ local needle=$1; shift
+ local item
+ for item in "$@"; do
+ [[ "$needle" == "$item"* ]] && return 0 # Found
+ done
+ return 1 # Not Found
+}
+
+exclude_mountpoints() {
+ local error=0
+
+ for fs in "${excludeMountpoints[@]}"; do
+ if [[ $fs != */ ]]; then
+ error=1
+ echo "Error: excludeMountpoints entry doesn't end with /: $fs" >&2
+ fi
+ excludeList+="- $fs*"$'\n'
+ done
+
+ while read line; do
+ local mountpoint=$(echo "$line" | cut -d\ -f2 | sed 's#\040# #g;')
+ type=$(echo "$line" | cut -d\ -f3)
+
+ if in_array $type fuse.sshfs tmpfs cifs nfs fuseblk; then
+ if ! in_array_startswith "$mountpoint/" "${excludeMountpoints[@]}"; then
+ error=1
+ echo "Warning: mountpoint not excluded: $mountpoint" >&2
+ fi
+ fi
+ done </etc/mtab
+
+ if ((error)); then
+ exit 1
+ fi
+}
+
+main "$@"
+