Cron Jobs For Ubuntu
03-04-2016, 01:42 PM
(This post was last modified: 03-05-2016, 09:29 AM by pinguy.)
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:
Next thing to do is to set up Dropbox.
Follow the on prompt commands because you will need to create a Dropbox API.
F3 will save the file and Ctrl+X will exit nano.
Next set the correct permissions.
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.
Change the line at the end of that file to:
from:
F3 will save the file and Ctrl+X will exit nano.
F3 will save the file and Ctrl+X will exit nano.
Next set the correct permissions.
This is my Daily database cron.
What this does is optimize all the the databases and backs them up to Dropbox.
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.
If you have any handy cron jobs please share.
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
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
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.