-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVpasssync
executable file
·100 lines (80 loc) · 2.36 KB
/
Vpasssync
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
#!/bin/zsh
function Mergedb()
{
local res;
if [ "$1" = "new" ]; then
echo "changing password";
keepassxc-cli merge "$2" "$3";
res=$?;
elif [ "$1" = "same" ]; then
echo "same password";
keepassxc-cli merge -s "$2" "$3";
res=$?;
else
res=1;
fi
return $res;
}
. Vglobals
backupdir="$PATH_TO_CACHE_FOLDER/keepass";
backup="$backupdir/Database_backup.kdbx";
networkfile="Database_net.kdbx";
networkdir="$HOME/Downloads";
if ! [ -e "$KEEPASS_DATABASE_FILE" ]; then
echo "database $KEEPASS_DATABASE_FILE doesn't exist";
exit 1;
fi
if ! [ -d "$backupdir" ]; then
mkdir -p "$backupdir";
if ! [ -d "$backupdir" ]; then
echo "couldn't create backup directory";
exit 1;
fi
fi
flags="same"
if [ "$#" -gt 0 ] && [ "$1" = "newpass" ]; then
echo "password in local database was changed";
flags="new";
fi
# keep old backup backed up
cp "$backup" "$backup.old";
echo "backup backed up)";
cp "$KEEPASS_DATABASE_FILE" "$backup";
echo "currently used file is backed up";
# obtain file from network
rclone copy "$CLOUD_BACKUP_DIR/$networkfile" "$networkdir" && echo "loaded copy from cloud";
if ! [ -e "$networkdir/$networkfile" ]; then
echo "network file wasn't downloaded";
else
echo "merging network file to local backup...";
cp "$networkdir/$networkfile" "$backupdir/$networkfile.old";
if ! Mergedb "$flags" "$backup" "$networkdir/$networkfile"; then
echo "maybe the password you've provided is wrong, try again:";
if ! Mergedb "$flags" "$backup" "$networkdir/$networkfile"; then
echo "or maybe you don't know the password((";
rm "$networkdir/$networkfile";
exit 1
fi
fi
# now backup file has all changes
cp "$backup" "$KEEPASS_DATABASE_FILE";
cp "$backup" "$networkdir/$networkfile";
# send network file back to online storage
rclone copyto "$networkdir/$networkfile" "$CLOUD_BACKUP_DIR/$networkfile.new";
if rclone ls "$CLOUD_BACKUP_DIR" | grep -q "$networkfile.new"; then
echo "backup successfully copied to cloud";
rclone moveto "$CLOUD_BACKUP_DIR/$networkfile.new" "$CLOUD_BACKUP_DIR/$networkfile";
if rclone ls "$CLOUD_BACKUP_DIR" | grep -q "$networkfile.new"; then
echo "couldn't replace old file with a new one on remote";
exit 1;
else
echo "file is replaced on remote";
rm "$networkdir/$networkfile";
echo "local redundant copy deleted";
fi
else
echo "backup wasn't copied to cloud";
exit 1;
fi
fi
echo "done";