-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrm2-backup.sh
executable file
·147 lines (133 loc) · 4.25 KB
/
rm2-backup.sh
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/sh
# Current rm2-backup.sh
version="1.0.0"
current_directory=`dirname $0`
# default values for arguments
user="root"
remarkable_ip="10.11.99.1" # remarkable IP address
backup_dir="$current_directory/backup"
sync=false
backup=false
restore_update_existing=false
restore_ignore_existing=false
restore_source=
usage()
{
echo "Usage: $0 options
echo "Options:
echo " -v or --version :"
echo " -u, --user 'username': change default username (${user})"
echo " -H, --host 'host or ip: change remarkable ip/hostname (${remarkable_ip})"
echo " -s, --sync: sync remarkable to $backup_dir/xochitl"
echo " -b, --backup : backup to automatically created $backup_dir/YYYY/MM/DD/HHMMSS directory"
echo " -l, --list : list backup directories"
echo " -r 'backup_dir' # restore from 'backup_dir' directory"
echo " -R 'backup_dir' # clean destination and restore from 'backup_dir' directory"
echo "Examples:"
echo " $0 # show help"
echo " $0 -h # show help"
echo " $0 -s # sync remarkable to $backup_dir/xochitl"
echo " $0 -b # backup to automatically created $backup_dir/YYYY/MM/DD/HHMMSS directory"
echo " $0 -r $backup_dir/2024/01/28/091723 # restore from given backup directory, ignore non existing"
echo " $0 -r $backup_dir/xochitl # restore from given backup directory, ignore non existing"
echo " $0 -R $backup_dir/2024/01/28/091723 # restore from given backup directory, delete non existing"
exit 1
}
if [ $# -eq 0 ]
then
usage
fi
# loop through arguments and process them
while [ $# -gt 0 ]; do
case "$1" in
-v | --version)
echo "$0 version: v$version"
exit
;;
-l | --list)
echo "Available backups:"
echo $backup_dir/xochitl
find $backup_dir -mindepth 4 -maxdepth 4 -type d
exit
;;
-u | --user)
user="$2"
shift
shift
;;
-H | --host)
remarkable_ip="$2"
shift
shift
;;
-s | --sync)
sync=true
shift
;;
-b | --backup)
backup=true
shift
;;
-r | --restore-update-existing)
restore_update_existing=true
restore_source="$2"
shift
shift
;;
-R | --restore-ignore-existing)
restore_ignore_existing=true
restore_source="$2"
shift
shift
;;
-h | --help | *)
usage
;;
esac
done
echo "reMarkable: ${user}@${remarkable_ip}"
output=""
ret=""
ssh_cmd()
{
output=`ssh -o ConnectTimeout=2 -o ServerAliveInterval=2 -ServerAliveCountMax=2 ${user}@${remarkable_ip} $@ 2>&1`
retval=$?
return $retval
}
if ! ssh_cmd "hostname"
then
echo "$remarkable_ip is down/unreachable: $output"
exit 1
fi
if $sync ;
then
echo "syncing from $remarkable_ip to $backup_dir/xochitl"
mkdir -p $backup_dir
rsync -e "ssh -o ConnectTimeout=2 -o ServerAliveInterval=2 -ServerAliveCountMax=2" --timeout 30 -azuv ${user}@${remarkable_ip}:~/.local/share/remarkable/xochitl $backup_dir
exit $?
fi
if $backup ;
then
year="`date +"%Y"`"
month="`date +"%m"`"
day="`date +"%d"`"
current_time="`date +"%H"`_`date +"%M"`_`date +"%S"`"
current_backup_dir="${backup_dir}/${year}/${month}/${day}/${current_time}"
mkdir -p $current_backup_dir
echo "backuping from $remarkable_ip to $current_backup_dir"
rsync -e "ssh -o ConnectTimeout=2 -o ServerAliveInterval=2 -ServerAliveCountMax=2" --timeout 30 -azuv ${user}@${remarkable_ip}:~/.local/share/remarkable/xochitl/ $current_backup_dir/
exit $?
fi
if $restore ;
then
echo "restore from $restore_source/ to ${user}@${remarkable_ip}:~/.local/share/remarkable/xochitl/"
rsync -azuvn $restore_source/ ${user}@${remarkable_ip}:~/.local/share/remarkable/xochitl/
exit $?
fi
if $restore_ignore_existing ;
then
echo "restore from $restore_source/ to ${user}@${remarkable_ip}:~/.local/share/remarkable/xochitl/"
rsync -azuvn --delete --ignore-existing $restore_source/ ${user}@${remarkable_ip}:~/.local/share/remarkable/xochitl/
exit $?
fi
exit 0