-
Notifications
You must be signed in to change notification settings - Fork 6
/
make-backup
executable file
·109 lines (91 loc) · 3.01 KB
/
make-backup
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
set -e
set_dir () { DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"; }; set_dir
safe_source () { source $1; set_dir; }
if [[ $(id -u) > 0 ]]; then sudo $0 "$@"; exit; fi
safe_source $DIR/aktos-bash-lib/basic-functions.sh
safe_source $DIR/aktos-bash-lib/fs-functions.sh
safe_source $DIR/aktos-bash-lib/btrfs-functions.sh
show_help () {
cat <<HELP
Usage:
$(basename $0) ...options...
Options:
--source : source directory for backup
--backups : backup container directory
--method : method for backup. One of:
* hardlinks
* btrfs
HELP
}
die () {
errcho "ERROR: "
errcho "ERROR: $@"
errcho "ERROR: "
show_help
exit 255
}
source_dir=
backup_dir=
method=
while :; do
case $1 in
-h|-\?|--help)
show_help # Display a usage synopsis.
exit
;;
--source) # Takes an option argument; ensure it has been specified.
if [ "$2" ]; then
source_dir=$(realpath $2)
shift
else
die '"--source" requires a non-empty option argument.'
fi
;;
--backups) # Takes an option argument; ensure it has been specified.
if [ "$2" ]; then
backup_dir=$(realpath $2)
shift
else
die '"--backups" requires a non-empty option argument.'
fi
;;
--method) # Takes an option argument; ensure it has been specified.
if [ "$2" ]; then
method=$2
shift
else
die '"--method" requires a non-empty option argument.'
fi
;;
-?*)
printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
;;
*) # Default case: No more options, so break out of the loop.
break
esac
shift
done
[[ "$method" == "hardlinks" ]] || [[ "$method" == "btrfs" ]] || die "method is required."
[[ -d $backup_dir ]] || die "backups should be a directory."
[[ -d $source_dir ]] || die "source should be a directory."
read -p "Enter Comment : " comment
[[ $comment ]] || echo_err "Comment is required."
comment=$(echo $comment | sed -e 's/ /-/g')
prefix=$(get_timestamp)
backup_name="$prefix-$comment"
tmp_name="work-in-progress-$backup_name"
start_timer
if [[ "$method" == "hardlinks" ]]; then
if ls $backup_dir/work-in-progress* 1> /dev/null 2>&1; then
echo_err "work in progress directories found. Remove them first."
fi
echo "Creating backup with the following name: $backup_name"
echo "Using hardlinks method"
cp -al $source_dir $backup_dir/$tmp_name
mv $backup_dir/$tmp_name $backup_dir/$backup_name
elif [[ "$method" == "btrfs" ]]; then
is_btrfs_subvolume $source_dir || die "Source should be BTRFS subvolume."
btrfs sub snap -r "$source_dir" "$backup_dir/$backup_name"
fi
show_timer "Backup completed in:"