I chose not to edit the previous answer, as suggested by the prompt when I clicked to add this one so that the comments on that answer are not invalidated and because I feel the previous answer and comments are valuable in the learning experience they reflect.
I got excited an posted the previous answer before even powering down my machines to see that there was some issue with something still running. I have not figured out exactly what that issue was, I believe it was due to my script not being LSB compliant and being in /etc/init.d.
I have actually solved the problem now and the solution has been working great for a couple of days, I have tested it multiple times in that period.
What I did:
I found this post while working on getting this same script to run at boot on my Arch installation, which is on a separate drive in this desktop: https://unix.stackexchange.com/questions/138281/arch-linux-run-script-a-minute-after-boot and then I revisited this page that I have not looked in a while: https://wiki.archlinux.org/index.php/Systemd the following solution worked on my Arch machine and, realizing that Ubuntu also runs Systemd, I decided to try it on Ubuntu as well ...success:)
Here is the now working solution:
#!/bin/bash
if [[ $(($(date +%A-%B-%d-%Y) != $(cat /home/user/Log/backup.log | tail -n1 | cut -d " " -f7))) \
-eq 1 ]]
then
while [[ $(sudo -u user dropbox status) != "Up to date" ]]
do
sleep 10s
[[ $(uptime | cut -d " " -f4) -ge 30 ]] && echo "Dropbox took to long on $(date)" >> \
/home/user/Log/backup.log && exit 0
done
shopt -s dotglob globstar
# Change to BACKUPNUM=$(($(date +%d) % 10)) when get separate drive to
# backup to some day
BACKNUM=$(($(date +%d) % 2))
rsync -aAX --delete /home/user/ /backup/daily${BACKNUM}/user/ >> \
/home/user/Log/backup.log 2>&1
[[ $? -eq 0 ]] && rsync -aAX --delete /etc/ /backup/daily${BACKNUM}/etc/ >> \
/home/user/Log/backup.log 2>&1
[[ $? -eq 0 ]] && rm -rf /backup/daily${BACKNUM}/user/.dropbox >> \
/home/user/Log/backup.log 2>&1
[[ $? -eq 0 ]] && rm -rf /backup/daily${BACKNUM}/user/Dropbox/.dropbox >> \
/home/user/Log/backup.log 2>&1
[[ $? -eq 0 ]] && rm -rf /backup/daily${BACKNUM}/user/Dropbox/.dropbox.cache >> \
/home/user/Log/backup.log 2>&1
[[ $? -eq 0 ]] && \
echo "successful backup of /home/user and /etc: $(date +%A-%B-%d-%Y) $(date +%l:%M%P)" >> \
/home/user/Log/backup.log
exit 0
else
exit 0
fi
So I added the following two files /etc/systemd/system/dailyBackup.service:
[Unit]
Description=dailyBackup
[Service]
Type=simple
ExecStart=/usr/local/bin/dailyBackup
and /etc/systemd/system/dailyBackup.timer:
[Unit]
Desciption=Runs dailyBackup one minute after boot
[Timer]
#how long to wait before executing
OnBootSec=1min
Unit=dailyBackup.service
[Install]
WantedBy=multi-user.target
I then ran:
sudo systemctl enable dailyBackup.timer
and shutdown the system. Upon powering back up, the script executed successfully and there are no shutdown or reboot issues. This solution, as earlier stated, has been working for a couple of days now. As can be seen in the script, I am using rsync now which was suggested by bacOn in their comment to my last answer. I have tried rsync a few times in the past and have never really fully figured out why there are still differences when I run diff -r, but I think I have realized that these are due to system .files that have changed slightly since the run of rsync and also because I seem to have some broken symlinks in the ~/snap/gnome-system-monitor/current/.local/share/icons/hicolor on my desktop and there is not even a snap directory on my laptop Ubuntu. This must be due to something I have previously installed and removed. That is for another question though as is the issue that the --delete rsync flag seems to cancel the --exclude flag, even when I add the --delete-excluded flag. Which is why my script still has the rm statements after the rsync. I hope this is helpful to someone, I have learned from this and to me that is invaluable. Now I need to do some actual school work instead of playing with and learning about Linux, which is one of my favorite things to do:)
update:
So I figured out that the patterns in the rsync flag --exclude={} have to be relative paths, relative to the source directory being synced from and that is why the excludes were being ignored entirely. I was putting absolute paths in. so the rm -rf statements are no longer needed. I have also added some conditionals to the while loop now and decreased the sleep to one second( I know this is close to polling the command ) so that if dropbox is taking to long to update because I have added some huge video files or something the script will exit 0 if I shutdown or reboot while the script is still running that while loop. So until I figure out a better way to stop the script in the case of a shutdown or reboot and a better way to have the script wait until dropbox is up to date, perhaps something in dailyBackup.timer to not even run it until dropbox is up to date, here is the current best working version of the script:
#!/bin/bash
if [[ $((10#$(date +%d) != 10#$(cat /home/user/Log/backup.log | tail -n1 | cut -d " " -f7 | \
cut -d "-" -f3))) -eq 1 ]]
then
while [[ $(sudo -u user dropbox status) != "Up to date" ]]
do
[[ $(runlevel | cut -d " " -f2) -eq 0 ]] && exit 0
[[ $(runlevel | cut -d " " -f2) -eq 6 ]] && exit 0
[[ $(uptime | cut -d " " -f4) -ge 30 ]] && echo "Dropbox took to long on $(date)" >> \
/home/user/Log/backup.err && exit 0
sleep 1s
done
shopt -s dotglob globstar
# Change to BACKUPNUM=$(($(date +%d) % 10)) when get separate drive to backup to someday
BACKNUM=$((10#$(date +%d) % 2))
rsync -aAX --delete-excluded \
--exclude={".dropbox","Dropbox/.dropbox",".dropbox-dist","Dropbox/.dropbox.cache"} \
/home/user/ /backup/daily${BACKNUM}/user/ >> /home/user/Log/backup.err 2>&1
[[ $? -eq 0 ]] && rsync -aAX --delete /etc/ /backup/daily${BACKNUM}/etc/ >> \
/home/user/Log/backup.err 2>&1
[[ $? -eq 0 ]] && \
echo "successful backup of /home/user and /etc: $(date +%A-%B-%d-%Y) $(date +%l:%M%P)" >> \
/home/user/Log/backup.log
exit 0
else
exit 0
fi
I have not stated it, however to use this I created a /backup directory and then a /backup/daily0 and /backup/daily1 and "sudo chown me:myGroup" both of them and then created a user and an etc directory inside both daily0 and daily1.
Also on my laptop I changed dailyBackup.timer to start the script 3 minutes after boot since it takes longer for wifi to get going than a wired connection and on my desktop I changed it to 2 minutes just to help it not loop as long in the case of a long dropbox update time.
Also when the date hit 08 I found that since the numbers in the first conditional if are being interpreted as octal I had to prepend 10# so that it uses base 10 numbers everywhere which prompted me to just add an additional cut -d to use only the actual day of the month instead of comparing the entire date string. The issue on this website is that the pound signs are interpreted in the code block as the start of a comment so it makes it look like the last of those lines are just comments, they are not. I have also found that it is better to send errors to one file and successful logs to another.
the first line of code after the chebang and the line where the variable BACKNUM is instantiated are the lines I am talking about.
which dropbox
an use the output in your script.