Some more work on soft deletion

This commit is contained in:
deajan 2015-03-30 19:51:31 +02:00
parent e7af81de98
commit 41781aa0da
1 changed files with 56 additions and 30 deletions

View File

@ -4,7 +4,7 @@ PROGRAM="Osync" # Rsync based two way sync engine with fault tolerance
AUTHOR="(L) 2013-2015 by Orsiris \"Ozy\" de Jong" AUTHOR="(L) 2013-2015 by Orsiris \"Ozy\" de Jong"
CONTACT="http://www.netpower.fr/osync - ozy@netpower.fr" CONTACT="http://www.netpower.fr/osync - ozy@netpower.fr"
PROGRAM_VERSION=1.00pre PROGRAM_VERSION=1.00pre
PROGRAM_BUILD=2603201504 PROGRAM_BUILD=3003201502
## type doesn't work on platforms other than linux (bash). If if doesn't work, always assume output is not a zero exitcode ## type doesn't work on platforms other than linux (bash). If if doesn't work, always assume output is not a zero exitcode
if ! type -p "$BASH" > /dev/null if ! type -p "$BASH" > /dev/null
@ -17,7 +17,7 @@ fi
if [ ! "$DEBUG" == "yes" ] if [ ! "$DEBUG" == "yes" ]
then then
DEBUG=no DEBUG=no
SLEEP_TIME=1 SLEEP_TIME=.1
else else
SLEEP_TIME=3 SLEEP_TIME=3
fi fi
@ -59,8 +59,7 @@ ALERT_LOG_FILE=$RUN_DIR/osync_lastlog
function Dummy function Dummy
{ {
sleep 1 sleep .1
exit 0
} }
function Log function Log
@ -1504,16 +1503,17 @@ function SoftDelete
if [ $verbose -eq 1 ] if [ $verbose -eq 1 ]
then then
# Cannot launch log function from xargs, ugly hack # Cannot launch log function from xargs, ugly hack
$FIND_CMD "$MASTER_SYNC_DIR$MASTER_BACKUP_DIR/" -type f -ctime +$CONFLICT_BACKUP_DAYS -print0 | xargs -0 -I {} echo "Deleting file {}" > $RUN_DIR/osync_conflict_backup_$SCRIPT_PID $FIND_CMD "$MASTER_SYNC_DIR$MASTER_BACKUP_DIR/" -type f -ctime +$CONFLICT_BACKUP_DAYS -print0 | xargs -0 -I {} echo "Will delete file {}" > $RUN_DIR/osync_conflict_backup_master_$SCRIPT_PID
Log "Command output:\n$(cat $RUN_DIR/osync_conflict_backup_$SCRIPT_PID)" Log "Command output:\n$(cat $RUN_DIR/osync_conflict_backup_master_$SCRIPT_PID)"
$FIND_CMD "$MASTER_SYNC_DIR$MASTER_BACKUP_DIR/" -type d -empty -ctime +$CONFLICT_BACKUP_DAYS -print0 | xargs -0 -I {} echo "Deleting directory {}" >> $RUN_DIR/osync_conflict_backup_$SCRIPT_PID $FIND_CMD "$MASTER_SYNC_DIR$MASTER_BACKUP_DIR/" -type d -empty -ctime +$CONFLICT_BACKUP_DAYS -print0 | xargs -0 -I {} echo "Will delete directory {}" > $RUN_DIR/osync_conflict_backup_master_$SCRIPT_PID
Log "Command output:\n$(cat $RUN_DIR/osync_conflict_backup_$SCRIPT_PID)" Log "Command output:\n$(cat $RUN_DIR/osync_conflict_backup_master_$SCRIPT_PID)"
Dummy &
fi fi
if [ $dryrun -ne 1 ] if [ $dryrun -ne 1 ]
then then
$FIND_CMD "$MASTER_SYNC_DIR$MASTER_BACKUP_DIR/" -type f -ctime +$CONFLICT_BACKUP_DAYS -print0 | xargs -0 -I {} rm -f "{}" && $FIND_CMD "$MASTER_SYNC_DIR$MASTER_BACKUP_DIR/" -type d -empty -ctime +$CONFLICT_BACKUP_DAYS -print0 | xargs -0 -I {} rm -rf "{}" > $RUN_DIR/osync_conflict_backup_$SCRIPT_PID & $FIND_CMD "$MASTER_SYNC_DIR$MASTER_BACKUP_DIR/" -type f -ctime +$CONFLICT_BACKUP_DAYS -print0 | xargs -0 -I {} rm -f "{}" && $FIND_CMD "$MASTER_SYNC_DIR$MASTER_BACKUP_DIR/" -type d -empty -ctime +$CONFLICT_BACKUP_DAYS -print0 | xargs -0 -I {} rm -rf "{}" > $RUN_DIR/osync_conflict_backup_master_$SCRIPT_PID &
else
Dummy &
fi fi
child_pid=$! child_pid=$!
WaitForCompletion $child_pid $SOFT_MAX_EXEC_TIME $HARD_MAX_EXEC_TIME WaitForCompletion $child_pid $SOFT_MAX_EXEC_TIME $HARD_MAX_EXEC_TIME
@ -1521,15 +1521,11 @@ function SoftDelete
if [ $retval -ne 0 ] if [ $retval -ne 0 ]
then then
LogError "Error while executing conflict backup cleanup on master replica." LogError "Error while executing conflict backup cleanup on master replica."
Log "Command output:\n$(cat $RUN_DIR/osync_conflict_backup_master_$SCRIPT_PID)"
else else
Log "Conflict backup cleanup complete on master replica." Log "Conflict backup cleanup complete on master replica."
fi fi
if [ $verbose -eq 1 ]
then
Log "Command output:\n$(cat $RUN_DIR/osync_conflict_backup_$SCRIPT_PID)"
fi
elif [ -d "$MASTER_SYNC_DIR$MASTER_BACKUP_DIR" ] && ! [ -w "$MASTER_SYNC_DIR$MASTER_BACKUP_DIR" ] elif [ -d "$MASTER_SYNC_DIR$MASTER_BACKUP_DIR" ] && ! [ -w "$MASTER_SYNC_DIR$MASTER_BACKUP_DIR" ]
then then
LogError "Warning: Master replica conflict backup dir [$MASTER_SYNC_DIR$MASTER_BACKUP_DIR] isn't writable. Cannot clean old files." LogError "Warning: Master replica conflict backup dir [$MASTER_SYNC_DIR$MASTER_BACKUP_DIR] isn't writable. Cannot clean old files."
@ -1539,13 +1535,26 @@ function SoftDelete
then then
CheckConnectivity3rdPartyHosts CheckConnectivity3rdPartyHosts
CheckConnectivityRemoteHost CheckConnectivityRemoteHost
if [ $dryrun -eq 1 ] if [ $dryrun -eq 1 ]
then then
Log "Listing backups older than $CONFLICT_BACKUP_DAYS days on slave replica. Won't remove anything." Log "Listing backups older than $CONFLICT_BACKUP_DAYS days on slave replica. Won't remove anything."
eval "$SSH_CMD \"if [ -w \\\"$SLAVE_SYNC_DIR$SLAVE_BACKUP_DIR\\\" ]; then $COMMAND_SUDO $REMOTE_FIND_CMD \\\"$SLAVE_SYNC_DIR$SLAVE_BACKUP_DIR/\\\" -ctime +$CONFLICT_BACKUP_DAYS; fi\"" &
else else
Log "Removing backups older than $CONFLICT_BACKUP_DAYS days on remote slave replica." Log "Removing backups older than $CONFLICT_BACKUP_DAYS days on slave replica."
eval "$SSH_CMD \"if [ -w \\\"$SLAVE_SYNC_DIR$SLAVE_BACKUP_DIR\\\" ]; then $COMMAND_SUDO $REMOTE_FIND_CMD \\\"$SLAVE_SYNC_DIR$SLAVE_BACKUP_DIR/\\\" -ctime +$CONFLICT_BACKUP_DAYS -exec rm -rf '{}' \;; fi\"" & fi
if [ $verbose -eq 1 ]
then
# Cannot launch log function from xargs, ugly hack
eval "$SSH_CMD \"if [ -w \\\"$SLAVE_SYNC_DIR$SLAVE_BACKUP_DIR\\\" ]; then $COMMAND_SUDO $REMOTE_FIND_CMD \\\"$SLAVE_SYNC_DIR$SLAVE_BACKUP_DIR/\\\" -type f -ctime +$CONFLICT_BACKUP_DAYS -print0 | xargs -0 -I {} echo Will delete file {} && $REMOTE_FIND_CMD \\\"$SLAVE_SYNC_DIR$SLAVE_BACKUP_DIR/\\\" -type d -empty -ctime $CONFLICT_BACKUP_DAYS -print0 | xargs -0 -I {} echo Will delete directory {}; fi\"" > $RUN_DIR/osync_conflict_backup_slave_$SCRIPT_PID
Log "Command output:\n$(cat $RUN_DIR/osync_conflict_backup_slave_$SCRIPT_PID)"
fi
if [ $dryrun -ne 1 ]
then
eval "$SSH_CMD \"if [ -w \\\"$SLAVE_SYNC_DIR$SLAVE_BACKUP_DIR\\\" ]; then $COMMAND_SUDO $REMOTE_FIND_CMD \\\"$SLAVE_SYNC_DIR$SLAVE_BACKUP_DIR/\\\" -type f -ctime +$CONFLICT_BACKUP_DAYS -print0 | xargs -0 -I {} rm -f \\\"{}\\\" && $REMOTE_FIND_CMD \\\"$SLAVE_SYNC_DIR$SLAVE_BACKUP_DIR/\\\" -type d -empty -ctime $CONFLICT_BACKUP_DAYS -print0 | xargs -0 -I {} rm -rf \\\"{}\\\"; fi\"" > $RUN_DIR/osync_conflict_backup_slave_$SCRIPT_PID &
else
Dummy &
fi fi
child_pid=$! child_pid=$!
WaitForCompletion $child_pid $SOFT_MAX_EXEC_TIME $HARD_MAX_EXEC_TIME WaitForCompletion $child_pid $SOFT_MAX_EXEC_TIME $HARD_MAX_EXEC_TIME
@ -1553,6 +1562,8 @@ function SoftDelete
if [ $retval -ne 0 ] if [ $retval -ne 0 ]
then then
LogError "Error while executing conflict backup cleanup on slave replica." LogError "Error while executing conflict backup cleanup on slave replica."
Log "Command output:\n$(cat $RUN_DIR/osync_conflict_backup_slave_$SCRIPT_PID)"
else else
Log "Conflict backup cleanup complete on slave replica." Log "Conflict backup cleanup complete on slave replica."
fi fi
@ -1562,10 +1573,23 @@ function SoftDelete
if [ $dryrun -eq 1 ] if [ $dryrun -eq 1 ]
then then
Log "Listing backups older than $CONFLICT_BACKUP_DAYS days on slave replica. Won't remove anything." Log "Listing backups older than $CONFLICT_BACKUP_DAYS days on slave replica. Won't remove anything."
$FIND_CMD "$SLAVE_SYNC_DIR$SLAVE_BACKUP_DIR/" -ctime +$CONFLICT_BACKUP_DAYS &
else else
Log "Removing backups older than $CONFLICT_BACKUP_DAYS days on slave replica." Log "Removing backups older than $CONFLICT_BACKUP_DAYS days on slave replica."
$FIND_CMD "$SLAVE_SYNC_DIR$SLAVE_BACKUP_DIR/" -ctime +$CONFLICT_BACKUP_DAYS -exec rm -rf '{}' \; & fi
if [ $verbose -eq 1 ]
then
# Cannot launch log function from xargs, ugly hack
$FIND_CMD "$SLAVE_SYNC_DIR$SLAVE_BACKUP_DIR/" -type f -ctime +$CONFLICT_BACKUP_DAYS -print0 | xargs -0 -I {} echo "Will delete file {}" > $RUN_DIR/osync_conflict_backup_slave_$SCRIPT_PID
Log "Command output:\n$(cat $RUN_DIR/osync_conflict_backup_slave_$SCRIPT_PID)"
$FIND_CMD "$SLAVE_SYNC_DIR$SLAVE_BACKUP_DIR/" -type d -empty -ctime +$CONFLICT_BACKUP_DAYS -print0 | xargs -0 -I {} echo "Will delete directory {}" > $RUN_DIR/osync_conflict_backup_slave_$SCRIPT_PID
Log "Command output:\n$(cat $RUN_DIR/osync_conflict_backup_slave_$SCRIPT_PID)"
Dummy &
fi
if [ $dryrun -ne 1 ]
then
$FIND_CMD "$SLAVE_SYNC_DIR$SLAVE_BACKUP_DIR/" -type f -ctime +$CONFLICT_BACKUP_DAYS -print0 | xargs -0 -I {} rm -f "{}" && $FIND_CMD "$SLAVE_SYNC_DIR$SLAVE_BACKUP_DIR/" -type d -empty -ctime +$CONFLICT_BACKUP_DAYS -print0 | xargs -0 -I {} rm -rf "{}" > $RUN_DIR/osync_conflict_backup_slave_$SCRIPT_PID &
fi fi
child_pid=$! child_pid=$!
WaitForCompletion $child_pid $SOFT_MAX_EXEC_TIME $HARD_MAX_EXEC_TIME WaitForCompletion $child_pid $SOFT_MAX_EXEC_TIME $HARD_MAX_EXEC_TIME
@ -1573,9 +1597,11 @@ function SoftDelete
if [ $retval -ne 0 ] if [ $retval -ne 0 ]
then then
LogError "Error while executing conflict backup cleanup on slave replica." LogError "Error while executing conflict backup cleanup on slave replica."
Log "Command output:\n$(cat $RUN_DIR/osync_conflict_backup_slave_$SCRIPT_PID)"
else else
Log "Conflict backup cleanup complete on slave replica." Log "Conflict backup cleanup complete on slave replica."
fi fi
elif [ -d "$SLAVE_SYNC_DIR$SLAVE_BACKUP_DIR" ] && ! [ -w "$SLAVE_SYNC_DIR$SLAVE_BACKUP_DIR" ] elif [ -d "$SLAVE_SYNC_DIR$SLAVE_BACKUP_DIR" ] && ! [ -w "$SLAVE_SYNC_DIR$SLAVE_BACKUP_DIR" ]
then then
LogError "Warning: Slave replica conflict backup dir [$SLAVE_SYNC_DIR$SLAVE_BACKUP_DIR] isn't writable. Cannot clean old files." LogError "Warning: Slave replica conflict backup dir [$SLAVE_SYNC_DIR$SLAVE_BACKUP_DIR] isn't writable. Cannot clean old files."
@ -2183,10 +2209,10 @@ then
GetLocalOS GetLocalOS
InitLocalOSSettings InitLocalOSSettings
Init Init
SoftDelete
exit 0
GetRemoteOS GetRemoteOS
InitRemoteOSSettings InitRemoteOSSettings
SoftDelete
exit $?
if [ $sync_on_changes -eq 1 ] if [ $sync_on_changes -eq 1 ]
then then
SyncOnChanges SyncOnChanges