Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
en:linux:backup:full-rsync [2012/06/14 02:06] alex |
en:linux:backup:full-rsync [2020/04/29 08:04] (current) alex [backup-system] |
||
|---|---|---|---|
| Line 7: | Line 7: | ||
| * ''target'' : system to back up, must be set to output of ''uname -n''. Used as a sanity check. | * ''target'' : system to back up, must be set to output of ''uname -n''. Used as a sanity check. | ||
| * ''source_dir'' : source directory. Set to ''/'' for full system backup. | * ''source_dir'' : source directory. Set to ''/'' for full system backup. | ||
| - | * ''target_dir'' : target directory. Set to the mountpoint of an external hard drive. Note: make sure that ''target_dir'' or a parent is included in ''exclude_dir'' otherwise rsync will recurse and your hard drive will get filled up completely. | + | * ''target_dir'' : target directory. Set to the mountpoint of an external hard drive or a remote server. Note: make sure that ''target_dir'' or a parent is included in ''exclude_dir'' otherwise rsync will recurse and your hard drive will get filled up completely. |
| * ''exclude_dir'' : excluded directories. Set these to temporary directories and other things you don't want backed up. | * ''exclude_dir'' : excluded directories. Set these to temporary directories and other things you don't want backed up. | ||
| * ''include_dir'' : included directories. Subfolders of excluded directories to include. | * ''include_dir'' : included directories. Subfolders of excluded directories to include. | ||
| - | If you want to use this script with a remote host, change flags to | + | If you want to use this script with a remote host where the target user is not root, change flags to |
| <code> | <code> | ||
| - | flags="-avPse ssh --delete --rsync-path=\"rsync --fake-super\"" | + | flags=(-avPse ssh --delete --ignore-errors --rsync-path="rsync --fake-super") |
| </code> | </code> | ||
| Line 20: | Line 20: | ||
| ====== backup-system ====== | ====== backup-system ====== | ||
| - | <code sh> | + | <code bash> |
| #!/bin/bash | #!/bin/bash | ||
| set -f | set -f | ||
| - | hostname=$(uname -n) | + | hostname=$(hostname -s) |
| target="my-hostname" | target="my-hostname" | ||
| source_dir="/" | source_dir="/" | ||
| target_dir="/media/external-hard-drive/$target/" | target_dir="/media/external-hard-drive/$target/" | ||
| - | exclude_dir="*/.gvfs /media /run/media /sys /dev /proc /mnt /tmp pagefile.sys hiberfil.sys /home/*/.cache .mozilla/firefox/*/Cache .Trash-1000" | + | exclude_dir=(*/.gvfs /media /run/media /sys /dev /proc /mnt /tmp pagefile.sys hiberfil.sys /home/*/.cache .mozilla/firefox/*/Cache .Trash-1000) |
| - | include_dir="/mnt/linux-data/opt /mnt/data" | + | include_dir=(/mnt/linux-data/opt /mnt/data) |
| - | flags="-avP --delete" | + | flags=(-avPAX --delete --ignore-errors) |
| if [ -n "$target" ] ; then | if [ -n "$target" ] ; then | ||
| Line 40: | Line 40: | ||
| fi | fi | ||
| - | include="" | + | include=() |
| # process include directories | # process include directories | ||
| - | for d in $include_dir | + | for d in "${include_dir[@]}" |
| do | do | ||
| found=0 | found=0 | ||
| dir="" | dir="" | ||
| - | exc_dir="" | + | shortest_parent="/" |
| - | shortest_parent="" | + | |
| - | + | echo include_dir $d | |
| # Need to deal with rsync idiosyncracies | # Need to deal with rsync idiosyncracies | ||
| # by including excluded directories | # by including excluded directories | ||
| Line 55: | Line 56: | ||
| # Removes matches to the include directory | # Removes matches to the include directory | ||
| # and finds the shortest parent path | # and finds the shortest parent path | ||
| - | for d2 in $exclude_dir | + | for d2 in "${exclude_dir[@]}" |
| do | do | ||
| + | echo exclude_dir $d2 | ||
| + | | ||
| if [[ $d = $d2* ]]; then | if [[ $d = $d2* ]]; then | ||
| found=1 | found=1 | ||
| - | | + | |
| - | if [[ -z "$shortest_parent" ]]; then | + | if [[ "$shortest_parent" == "/" ]]; then |
| shortest_parent=$d2 | shortest_parent=$d2 | ||
| else | else | ||
| Line 67: | Line 70: | ||
| fi | fi | ||
| fi | fi | ||
| - | | ||
| - | else | ||
| - | exc_dir="$exc_dir $d2" | ||
| fi | fi | ||
| + | echo shortest_parent $shortest_parent | ||
| done | done | ||
| - | | + | |
| if [[ $found ]]; then | if [[ $found ]]; then | ||
| - | exclude_dir=$exc_dir | + | path=$(dirname "$d") |
| - | path=`dirname "$d"` | + | |
| while [[ "$path" != "/" ]]; do | while [[ "$path" != "/" ]]; do | ||
| - | exclude_dir="$exclude_dir $path/*" | + | exclude_dir+=("$path/*") |
| - | #include_dir="$include_dir $path" | + | include+=("--include=$path") |
| - | include="$include --include $path" | + | if [[ "$path" == "$shortest_parent" ]]; then |
| - | path=`dirname "$path"` | + | break |
| + | fi | ||
| + | path=$(dirname "$path") | ||
| done | done | ||
| fi | fi | ||
| - | | + | |
| - | include="$include --include $d" | + | include+=("--include=$d") |
| done | done | ||
| - | exclude="" | + | exclude=() |
| - | for d in $exclude_dir | + | for d in "${exclude_dir[@]}" |
| do | do | ||
| - | exclude="$exclude --exclude $d" | + | exclude+=("--exclude=$d") |
| done | done | ||
| - | sudo -E rsync $flags $include $exclude "$source_dir" "$target_dir" | + | sudo -E rsync "${flags[@]}" "${include[@]}" "${exclude[@]}" "$source_dir" "$target_dir" |
| </code> | </code> | ||