156 lines
No EOL
3.4 KiB
Markdown
156 lines
No EOL
3.4 KiB
Markdown
Bidirectional sync
|
|
https://sentrytwo.com/6bb78a2539
|
|
|
|
sync_directories1.sh
|
|
```bash
|
|
#!/bin/bash
|
|
|
|
|
|
# SOURCE="/home/hpes/Videos/impnew/notes/keepaas_offline"
|
|
# DESTINATION=" /home/hpes/Dropbox_keepaas/Apps/Keepaas2Android"
|
|
|
|
SOURCE=$1
|
|
DESTINATION=$2
|
|
|
|
db_name= "main.kdbx"
|
|
|
|
SOURCE_FILE="$SOURCE/${db_name}"
|
|
DESTINATION_FILE= "$DESTINATION/${db_name}"
|
|
|
|
# Function to sync directories
|
|
sync_directories() {
|
|
# rsync -avu --ignore-existing "$SOURCE/${db_name}" "$DESTINATION/${db_name}"
|
|
rsync -avu "$SOURCE/${db_name}" "$DESTINATION/${db_name}"
|
|
}
|
|
|
|
|
|
# Ensure initial sync
|
|
sync_directories
|
|
|
|
|
|
|
|
# Monitor the source directory for any activity, and sync upon changes
|
|
inotifywait -mre close_write,create,delete,move "$SOURCE" |
|
|
while read -r directory events filename; do
|
|
sync_directories
|
|
done
|
|
|
|
|
|
|
|
```
|
|
|
|
sync_directories.sh
|
|
```bash
|
|
#!/bin/bash
|
|
|
|
|
|
# SOURCE="/home/hpes/Videos/impnew/notes/keepaas_offline"
|
|
# DESTINATION=" /home/hpes/Dropbox_keepaas/Apps/Keepaas2Android"
|
|
|
|
SOURCE=$1
|
|
DESTINATION=$2
|
|
|
|
db_name= "main.kdbx"
|
|
|
|
SOURCE_FILE="$SOURCE/${db_name}"
|
|
DESTINATION_FILE= "$DESTINATION/${db_name}"
|
|
|
|
# Function to sync directories
|
|
sync_directories2() {
|
|
rsync -avu "$DESTINATION/${db_name}" "$SOURCE/${db_name}"
|
|
}
|
|
|
|
# Ensure initial sync
|
|
sync_directories2
|
|
|
|
|
|
|
|
# Monitor the source directory for any activity, and sync upon changes
|
|
inotifywait -mre close_write,create,delete,move "$SOURCE" |
|
|
while read -r directory events filename; do
|
|
sync_directories2
|
|
done
|
|
|
|
|
|
|
|
```
|
|
sync1.sh
|
|
```bash
|
|
/home/hpes/Videos/imp/scripts/sync_directories/sync_directories1.sh /home/hpes/Videos/impnew/notes/keepaas_offline /home/hpes/Dropbox_keepaas/Apps/Keepaas2Android
|
|
|
|
```
|
|
|
|
sync2.sh
|
|
```bash
|
|
/home/hpes/Videos/imp/scripts/sync_directories/sync_directories2.sh /home/hpes/Videos/impnew/notes/keepaas_offline /home/hpes/Dropbox_keepaas/Apps/Keepaas2Android
|
|
|
|
```
|
|
|
|
|
|
sync_bi.sh
|
|
```bash
|
|
#!/bin/bash
|
|
|
|
SCREEN_NAME1="sync1"
|
|
SCREEN_NAME1_SCRIPT_PATH="/home/hpes/Videos/imp/scripts/sync_directories/sync1.sh"
|
|
SCREEN_NAME2="sync2"
|
|
SCREEN_NAME2_SCRIPT_PATH="/home/hpes/Videos/imp/scripts/sync_directories/sync2.sh"
|
|
|
|
start_script() {
|
|
# Check if the screen session already exists
|
|
if screen -list | grep -q "$SCREEN_NAME1"; then
|
|
echo "Session already exists. Please stop it first or choose another name."
|
|
exit 1
|
|
fi
|
|
|
|
# Start the screen session detached mode
|
|
screen -dmS $SCREEN_NAME1 "$SCREEN_NAME1_SCRIPT_PATH"
|
|
screen -dmS $SCREEN_NAME2 "$SCREEN_NAME2_SCRIPT_PATH"
|
|
|
|
echo "Script started in background under screen session '$SCREEN_NAME1' and '$SCREEN_NAME2'."
|
|
}
|
|
|
|
stop_script() {
|
|
# Kill the screen session
|
|
screen -S $SCREEN_NAME1 -X quit
|
|
screen -S $SCREEN_NAME2 -X quit
|
|
echo "Screen session '$SCREEN_NAME1' and '$SCREEN_NAME2' stopped."
|
|
}
|
|
|
|
# Check argument passed
|
|
if [ "$#" -ne 1 ]; then
|
|
echo "Usage: $0 {start|stop}"
|
|
exit 1
|
|
fi
|
|
|
|
case "$1" in
|
|
start)
|
|
start_script
|
|
;;
|
|
stop)
|
|
stop_script
|
|
;;
|
|
*)
|
|
echo "Invalid option: $1"
|
|
echo "Usage: $0 {start|stop}"
|
|
exit 1
|
|
;;
|
|
esac
|
|
|
|
# screen -dmS sync1 ./sync1.sh
|
|
# screen -dmS sync2 ./sync2.sh
|
|
|
|
|
|
```
|
|
|
|
Finally in `.bashrc` add
|
|
```bash
|
|
# creating alias for starting sync
|
|
alias startsync="/home/hpes/Videos/imp/scripts/sync_directories/screen/sync_bi.sh start"
|
|
|
|
# run command for starting sync
|
|
startsync
|
|
|
|
# creating alias for stopping sync
|
|
alias closesync="/home/hpes/Videos/imp/scripts/sync_directories/screen/sync_bi.sh stop"
|
|
``` |