This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

Cron Jobs For Ubuntu
#1
Cron Jobs For Ubuntu
This is my Monthly backup cron.

What this does is optimize all the images in the public_html folder, zips it then sends a copy of public_html to Dropbox.

What you need to do for the script to fully work.

First thing is to install jpegoptim & OptiPNG.

You do this by running:

Code:
sudo apt-get install jpegoptim optipng

Next thing to do is to set up Dropbox.

Code:
wget -O dropbox_uploader.sh https://raw.githubusercontent.com/andreafabrizi/Dropbox-Uploader/master/dropbox_uploader.sh
chmod a+rx dropbox_uploader.sh
sudo mv dropbox_uploader.sh /usr/local/bin/dropbox_uploader
sudo chown root:root /usr/local/bin/dropbox_uploader
dropbox_uploader

Follow the on prompt commands because you will need to create a Dropbox API.

Code:
nano /etc/cron.monthly/backup

Code:
#!/bin/bash
cd /var/sentora/hostdata/zadmin/public_html/
find -iregex '.*\.\(png\)$' -print0 | xargs -0 optipng -o7 -preserve
find -iregex '.*\.\(jpg\|jpeg\)$' -print0 | xargs -0 jpegoptim --max=90 --strip-all –all-progressive --preserve --totals
find -iregex '.*\.\(jpg\|png\|jpeg\)$' -exec chmod 644 {} \;
find -iregex '.*\.\(jpg\|png\|jpeg\)$' -exec chown www-data:www-data {} \;
rm -rf /var/sentora/hostdata/zadmin/backup.zip
cd /var/sentora/hostdata/zadmin/
zip -r backup.zip public_html
dropbox_uploader delete /backup.zip
dropbox_uploader upload /var/sentora/hostdata/zadmin/backup.zip /backup.zip
exit 0

F3 will save the file and Ctrl+X will exit nano.

Next set the correct permissions.
Code:
sudo chmod +x '/etc/cron.monthly/backup'
sudo chmod 755 '/etc/cron.monthly/backup'

This is my Weekly updater cron.

What this does is run the system updater and installs any new updates. If an update requires a reboot, the script will reboot the server.

The script will also backup the logs and run a fsck to repair the disk if the system gets rebooted.

What you need to do for the script to fully work.

Edit fsck so it forces the fix.

Code:
nano /etc/default/rcS

Change the line at the end of that file to:

Code:
FSCKFIX=yes

from:

Code:
#FSCKFIX=no

F3 will save the file and Ctrl+X will exit nano.

Code:
nano /etc/cron.weekly/updater

Code:
#!/bin/bash
/usr/bin/dpkg --configure -a
/usr/bin/apt-get update
/usr/bin/apt-get -qy dist-upgrade
/usr/bin/apt-get install -f
/usr/bin/apt-get clean
/usr/bin/apt-get -qy autoremove

#Backup Logs
rm -rf /var/sentora/hostdata/zadmin/logs.zip
cd /var/
zip -r /var/sentora/hostdata/zadmin/logs.zip log
dropbox_uploader delete /logs.zip
dropbox_uploader upload /var/sentora/hostdata/zadmin/logs.zip /logs.zip

#Clean Logs
find /var/log -type f -delete
touch /var/log/dovecot.log
touch /var/log/dovecot-info.log
touch /var/log/dovecot-debug.log
chown vmail.mail /var/log/dovecot*
chown mysql:mysql /var/log/mysql*
service dovecot restart

if [ -f /var/run/reboot-required ]; then
sudo touch /forcefsck
sudo reboot
else
dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge
exit 0
fi

F3 will save the file and Ctrl+X will exit nano.

Next set the correct permissions.

Code:
sudo chmod +x '/etc/cron.weekly/updater'
sudo chmod 755 '/etc/cron.weekly/updater'

This is my Daily database cron.

What this does is optimize all the the databases and backs them up to Dropbox.

Code:
nano /etc/cron.daily/database

Code:
#!/bin/bash
mkdir -p /var/sentora/hostdata/zadmin/database
mysql -u root -p*PASSWORD* -e "FLUSH TABLES WITH READ LOCK;"
mysqlcheck --auto-repair -Aos -u root -p*PASSWORD*
mysqldump -u *USER* -p*PASSWORD* *DATABASE_NAME* > /var/sentora/hostdata/zadmin/database/*DATABASE_SAVE*.sql
*Repeat the above line for each database*
mysql -u root -p*PASSWORD* -e "UNLOCK TABLES;"
rm -rf /var/sentora/hostdata/zadmin/database.zip
cd /var/sentora/hostdata/zadmin/
zip -r database.zip database
rm -rf /var/sentora/hostdata/zadmin/database
dropbox_uploader delete /database.zip
dropbox_uploader upload /var/sentora/hostdata/zadmin/database.zip /database.zip
exit 0

Key:

*PASSWORD* = DB/MySQL Root Password.
*USER* = DB user.
*DATABASE_NAME* = Name of the DB.
*DATABASE_SAVE* = What you want to call the saved DB.

F3 will save the file and Ctrl+X will exit nano.

Next set the correct permissions.

Code:
sudo chmod +x '/etc/cron.daily/database'
sudo chmod 755 '/etc/cron.daily/database'

If you have any handy cron jobs please share.
Reply
Thanks given by:


Possibly Related Threads...
Thread Author Replies Views Last Post
Upgrade to Ubuntu 16.04 from 14.04 HogensHero 8 13 322 07-18-2020, 03:13 AM
Last Post: dycott78
Ubuntu: How to Enable tls/ssl in proftpd Diablo925 2 6 867 04-21-2020, 03:35 AM
Last Post: iraqiboy90
Ubuntu 18.04 or 20.04 Kangarooo 1 1 061 03-20-2020, 11:39 AM
Last Post: Jettaman

Forum Jump:


Users browsing this thread: 1 Guest(s)