Elasticbeanstalk - Cronjobs - Env Variables - amazon-elastic-beanstalk

Trying to work my cronjobs but env variables are not working in it.
my .platform/hooks/postdeploy/create_cron_files.sh is like below:
#!/bin/sh
# Create CRON files
# After the deployment finishes, set up a Crontab for
# the php artisan schedule:run command
echo "* * * * * webapp bash -c "\"" . <(sed -E -n 's/[^#]+/export &/ p' /opt/elasticbeanstalk/deployment/envvars) && php /var/app/current/artisan schedule:run 1>> /dev/null 2>&1"\"" " |
sudo tee /etc/cron.d/artisan_scheduler
# In some cases, Laravel logs a lot of data in the storage/logs/laravel.log and it sometimes
# might turn out into massive files that will restrict the filesystem.
# Uncomment the following lines to enable a CRON that deletes the laravel.log file
# every now and often.
echo "0 0 * * */7 root rm -rf /var/app/current/storage/logs/laravel.log 1>> /dev/null 2>&1" \
| sudo tee /etc/cron.d/log_deleter
Also I tried this one, too:
echo "* * * * * root . /opt/elasticbeanstalk/deployment/env && /usr/bin/php /var/app/current/artisan schedule:run 1>> /dev/null 2>&1" \
| sudo tee /etc/cron.d/artisan_scheduler
But the system does not see env variables inside elasticbeanstalk.
Whats wrong with it?

Related

how to run cron in dockerfile mySQL

I am new to dockerfile and basically I am trying to build a mysql image that runs a store proc every minute using cron and dockerFile content is shown below:
FROM mysql:5.7.16
ENV TZ Asia/Muscat
ENV http_proxy=http://000.000.00.1:8786/
ENV https_proxy=http://000.000.00.1:8786/
COPY I_Dump.sql /docker-entrypoint-initdb.d/
#Install Cron
RUN apt-get update
RUN apt-get -y install cron
# Add the cron job
RUN crontab -l | { cat; echo "* * * * * docker exec -t mysql mysql -uroot -proot -e "SELECT dummy.updatePublisherStatus()"; } | crontab -
# Run the command on container startup
CMD cron
When I run the command docker build -t mysqlinfocron . I get the following error:
/bin/sh: 1: Syntax error: "(" unexpected (expecting "}")
The command '/bin/sh -c crontab -l | { cat; echo "* * * * * docker exec -t mysql mysql -uroot -proot -e "SELECT dummy.updatePublisherStatus()"; } | crontab -' returned a non-zero code: 2
I know my syntax is wrong but I don't know how to fix it, any feedback is most welcome?

suspend then wake: cron of rtcwake, Beaglebone Black

I'm trying to configure the beaglebone black [wireless version - 4.9.82-ti-r102 #1 SMP PREEMPT] -- running debian 9.3 stretch.
This command works fine in a bash terminal:
sudo /usr/sbin/rtcwake -m mem -u -t $(date +%s -d "+2 minutes")
I've setup a cron job via
sudo crontab -e
In it, I have the following line:
10,40 * * * * /usr/sbin/rtcwake -m mem -u -t $(date +%s -d "20 minutes")
However, the device is not suspending.
What am I doing wrong? How do I debug this?
Thanks for your help
M
The % char is the problem in the crontab approach. That char has a special meaning in crontab (newline...start of stdin, see manual).
Instead of -t $(date +%s -d "1 minutes") you can simply use -s 60 for sleeping 60 seconds and not having to use % at all. Or you might escape it with \% or use '+%s' or "+%s".
OK,
In case there is anyone else trying to solve this. I don't know why this works, however it does...
1. make a script with the rtcwake comand in it....:
#!/bin/bash
# suspend then wake from a bash script
echo attempting to suspend!
/usr/sbin/rtcwake -m mem -u -t $(date +%s -d "1 minutes")
I called this wakesleep, and placed it in /usr/local/bin (on the PATH), make it executable...
sudo chmod +x /usr/local/bin/sleepwake
Then added a line in crontab, the su version:
sudo crontab -e
The added line in the root crontab is of this sort of format:
10 * * * * /usr/local/bin/sleepwake
... and it works. This is a mystery to me, why the previous approach did not work, but it works. Very keen to hear from someone what the difference between the two approaches is.

Bash script does not execute in CRON (might be to do with Sudo commands?)

I have a bash script that uses ssh tunneling to get a PSQL dump remotely then puts the dump into a local MySQL database.
I can't seem to get CRON to run it.
I have performed the following steps:
1) Ran:
sudo crontab -e
2) Input the following to get the script to execute every 10 minutes. Script also takes about 2-3 minutes to execute fully.
*/10 * * * * /home/ubuntu/psqlimport 2>&1 /home/ubuntu/cronlog.log
3) Ran:
sudo chmod +x /home/ubuntu/psqlimport
4) Restarted CRON:
sudo /etc/init.d/cron restart
I check my database to see if there were any updates and there are none, as if the script never executed. There is also no /home/ubuntu/cronlog.log file created. When I run:
bash psqlimport
The script executes as expected.
Why doesn't the CRON job execute the script?
Filename: psqlimport
#!bin/bash
###
#
# PostgreSQL to MySQL Dump program
#
# Filename: psqlimport
# Description: This program dumps the 3 tables from a
# remote PostgreSQL database, converts the dump file
# into MySQL translatable INSERT INTO queries,
# and inputs the data into the local
# MySQL Inbevdash6 database.
#
# Script can be copied to the
# /etc/cron.daily folder for daily database updates.
#
#
# Script written by ramabrahma#stackoverflow
# Date: December 15, 2015
# Version: 01
#
#Set the dump file and temp file
DUMPFILE='psql.dump.sql' || (echo "assign var failed" 1>&2; exit 1)
TMPFILE=`mktemp` || (echo "mktemp failed" 1>&2; exit 1)
#Tables to dump: table1, table2, table3
#Dump as INSERT queries statements
sudo -E sshpass -p "<remote host password>" \
ssh username#remote_host \
pg_dump -U database_username -t table1 \
-t table2 -t table3 \
--column-inserts --data-only psqldatabase \
| awk '/^INSERT/ {i=1} /^SELECT/ {i=0} i' \
> "$TMPFILE" \
|| (echo "pg_dump failed" 1>&2; exit 1)
#Adding transaction blocks to the dump file
(echo "start transaction; truncate table table1; "; \
echo "truncate table table2; "; \
echo "truncate table table3; "; \
cat "$TMPFILE"; echo 'commit;' ) \
> "$DUMPFILE" \
|| (echo "parsing dump file failed" 1>&2; exit 1)
#Inputting the dump file to the MySQL database
mysql --defaults-file="/home/ubuntu/.mysql.db.cfg" < "$DUMPFILE" \
|| (echo "mysql failed" 1>&2; exit 1)
#Remove the temp file created
rm "$TMPFILE"
rm "$DUMPFILE"
You have an error in your shebang:
#!bin/bash
It should be
#!/bin/bash
bin/bash probably isn't a path to anything unless you're executing out of /. This wasn't a problem when you executed the script manually as bash psqlimport explicitly invokes bash to evaluate the script. You'll be able to see the problem in action if you try to run ./psqlimport to run the script.

Script to check if an entry present on crontab, if not create new one

I want to make an entry in crontab that will optimize mysql database, so I am writing a script that will check if it is already present, else it will be created.
The problem I'm facing is when I check for entry it is returning many unwanted entries, to be specific all directories, files as output, present in the directory where I am executing the script.
My overall script looks like:
check=`crontab -l | grep -i "ABC"`
if [ -z "$check" ];
then
#echo " Adding opt to crontab "
crontab -l > ./tmpfile
echo "0 * * * * /bin/bash /usr/bin/mysqlcheck --all-databases -o --user=<user> --password=<pwd> 2>&1 >> ./tmpfile
crontab ./tmpfile
rm tmpfile
echo "ABC has been successfully added to crontab"
exit 1
else
echo "ABC already exists in crontab "
exit 1
fi

Linux shell script for database backup

I tried many scripts for database backup but I couldn't make it. I want to backup my database every hour.
I added files to "/etc/cron.hourly/" folder, changed its chmod to 755, but it didn't run.
At least I write my pseudo code.
I would be happy if you can write a script for this operation and tell me what should I do more ?
After adding this script file to /etc/cron.hourly/ folder.
Get current date and create a variable, date=date(d_m_y_H_M_S)
Create a variable for the file name, filename="$date".gz
Get the dump of my database like this mysqldump --user=my_user --password=my_pass --default-character-set=utf8 my_database | gzip > "/var/www/vhosts/system/example.com/httpdocs/backups/$("filename")
Delete all files in the folder /var/www/vhosts/system/example.com/httpdocs/backups/ that are older than 8 days
To the file "/var/www/vhosts/system/example.com/httpdocs/backup_log.txt", this text will be written: Backup is created at $("date")
Change the file owners (chown) from root to "my_user". Because I want to open the backup and log files from the "my_user" FTP account.
I don't want an email after each cron. >/dev/null 2>&1 will be added.
After hours and hours work, I created a solution like the below. I copy paste for other people that can benefit.
First create a script file and give this file executable permission.
# cd /etc/cron.daily/
# touch /etc/cron.daily/dbbackup-daily.sh
# chmod 755 /etc/cron.daily/dbbackup-daily.sh
# vi /etc/cron.daily/dbbackup-daily.sh
Then copy following lines into file with Shift+Ins
#!/bin/sh
now="$(date +'%d_%m_%Y_%H_%M_%S')"
filename="db_backup_$now".gz
backupfolder="/var/www/vhosts/example.com/httpdocs/backups"
fullpathbackupfile="$backupfolder/$filename"
logfile="$backupfolder/"backup_log_"$(date +'%Y_%m')".txt
echo "mysqldump started at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"
mysqldump --user=mydbuser --password=mypass --default-character-set=utf8 mydatabase | gzip > "$fullpathbackupfile"
echo "mysqldump finished at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"
chown myuser "$fullpathbackupfile"
chown myuser "$logfile"
echo "file permission changed" >> "$logfile"
find "$backupfolder" -name db_backup_* -mtime +8 -exec rm {} \;
echo "old files deleted" >> "$logfile"
echo "operation finished at $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"
echo "*****************" >> "$logfile"
exit 0
Edit:
If you use InnoDB and backup takes too much time, you can add "single-transaction" argument to prevent locking. So mysqldump line will be like this:
mysqldump --user=mydbuser --password=mypass --default-character-set=utf8
--single-transaction mydatabase | gzip > "$fullpathbackupfile"
Create a script similar to this:
#!/bin/sh -e
location=~/`date +%Y%m%d_%H%M%S`.db
mysqldump -u root --password=<your password> database_name > $location
gzip $location
Then you can edit the crontab of the user that the script is going to run as:
$> crontab -e
And append the entry
01 * * * * ~/script_path.sh
This will make it run on the first minute of every hour every day.
Then you just have to add in your rolls and other functionality and you are good to go.
I got the same issue.
But I manage to write a script.
Hope this would help.
#!/bin/bash
# Database credentials
user="username"
password="password"
host="localhost"
db_name="dbname"
# Other options
backup_path="/DB/DB_Backup"
date=$(date +"%d-%b-%Y")
# Set default file permissions
umask 177
# Dump database into SQL file
mysqldump --user=$user --password=$password --host=$host $db_name >$backup_path/$db_name-$date.sql
# Delete files older than 30 days
find $backup_path/* -mtime +30 -exec rm {} \;
#DB backup log
echo -e "$(date +'%d-%b-%y %r '):ALERT:Database has been Backuped" >>/var/log/DB_Backup.log
#!/bin/sh
#Procedures = For DB Backup
#Scheduled at : Every Day 22:00
v_path=/etc/database_jobs/db_backup
logfile_path=/etc/database_jobs
v_file_name=DB_Production
v_cnt=0
MAILTO="abc#as.in"
touch "$logfile_path/kaka_db_log.log"
#DB Backup
mysqldump -uusername -ppassword -h111.111.111.111 ddbname > $v_path/$v_file_name`date +%Y-%m-%d`.sql
if [ "$?" -eq 0 ]
then
v_cnt=`expr $v_cnt + 1`
mail -s "DB Backup has been done successfully" $MAILTO < $logfile_path/db_log.log
else
mail -s "Alert : kaka DB Backup has been failed" $MAILTO < $logfile_path/db_log.log
exit
fi
Here is my mysql backup script for ubuntu in case it helps someone.
#Mysql back up script
start_time="$(date -u +%s)"
now(){
date +%d-%B-%Y_%H-%M-%S
}
ip(){
/sbin/ifconfig eth0 2>/dev/null|awk '/inet addr:/ {print $2}'|sed 's/addr://'
}
filename="`now`".zip
backupfolder=/path/to/any/folder
fullpathbackupfile=$backupfolder/$filename
db_user=xxx
db_password=xxx
db_name=xxx
printf "\n\n"
printf "******************************\n"
printf "Started Automatic Mysql Backup\n"
printf "******************************\n"
printf "TIME: `now`\n"
printf "IP_ADDRESS: `ip` \n"
printf "DB_SERVER_NAME: DB-SERVER-1\n"
printf "%sBACKUP_FILE_PATH $fullpathbackupfile\n"
printf "Starting Mysql Dump \n"
mysqldump -u $db_user -p$db_password $db_name| pv | zip > $fullpathbackupfile
end_time="$(date -u +%s)"
elapsed=$(($end_time-$start_time))
printf "%sMysql Dump Completed In $elapsed seconds\n"
printf "******************************\n"
PS: Rememember to install pv and zip in your ubuntu
sudo apt install pv
sudo apt install zip
Here is how I set crontab by using crontab -e in ubuntu to run every 6 hours
0 */6 * * * sh /path/to/shfile/backup-mysql.sh >> /path/to/logs/backup-mysql.log 2>&1
Cool thing is it will create a zip file which is easier to unzip from anywhere
Now, copy the following content in a script file (like: /backup/mysql-backup.sh) and save on your Linux system.
#!/bin/bash
export PATH=/bin:/usr/bin:/usr/local/bin
TODAY=`date +"%d%b%Y"`
DB_BACKUP_PATH='/backup/dbbackup'
MYSQL_HOST='localhost'
MYSQL_PORT='3306'
MYSQL_USER='root'
MYSQL_PASSWORD='mysecret'
DATABASE_NAME='mydb'
BACKUP_RETAIN_DAYS=30
mkdir -p ${DB_BACKUP_PATH}/${TODAY}
echo "Backup started for database - ${DATABASE_NAME}"
mysqldump -h ${MYSQL_HOST} \
-P ${MYSQL_PORT} \
-u ${MYSQL_USER} \
-p${MYSQL_PASSWORD} \
${DATABASE_NAME} | gzip > ${DB_BACKUP_PATH}/${TODAY}/${DATABASE_NAME}-${TODAY}.sql.gz
if [ $? -eq 0 ]; then
echo "Database backup successfully completed"
else
echo "Error found during backup"
exit 1
fi
##### Remove backups older than {BACKUP_RETAIN_DAYS} days #####
DBDELDATE=`date +"%d%b%Y" --date="${BACKUP_RETAIN_DAYS} days ago"`
if [ ! -z ${DB_BACKUP_PATH} ]; then
cd ${DB_BACKUP_PATH}
if [ ! -z ${DBDELDATE} ] && [ -d ${DBDELDATE} ]; then
rm -rf ${DBDELDATE}
fi
fi
After creating or downloading script make sure to set execute permission to run properly.
$ chmod +x /backup/mysql-backup.sh
Edit crontab on your system with crontab -e command. Add following settings to enable backup at 3 in the morning.
0 3 * * * root /backup/mysql-backup.sh
Add the following code to your shell script file. Replace dbname, dbuser and dbpass with your database name, username and password respectively.
#!/bin/sh
echo "starting db backup"
day="$(date +"%m-%d-%y")"
db_backup="mydb_${day}.sql"
sudo mysqldump -udbuser -pdbpass --no-tablespaces dbname >/home/${db_backup}
echo " backup complete"
If you want to compress the above backup data, just
Replace with the following code.
db_backup="mydb_${day}.gz"
sudo mysqldump -udbuser -pdbpass --no-tablespaces dbname | gzip -c >/home/${db_backup}
If you want to delete files older than 14 days in a folders,
use following code.
#!/bin/bash
fpath1=/home/ubuntu/mysql/*
fpath2=/home/ubuntu/postgsql/*
file_path=($fpath1 $fpath2)
for i in ${file_path[#]};
do
find $i -type d -mtime +13 -exec rm -Rf {} +
done
#!/bin/bash
# Add your backup dir location, password, mysql location and mysqldump location
DATE=$(date +%d-%m-%Y)
BACKUP_DIR="/var/www/back"
MYSQL_USER="root"
MYSQL_PASSWORD=""
MYSQL='/usr/bin/mysql'
MYSQLDUMP='/usr/bin/mysqldump'
DB='demo'
#to empty the backup directory and delete all previous backups
rm -r $BACKUP_DIR/*
mysqldump -u root -p'' demo | gzip -9 > $BACKUP_DIR/demo$date_format.sql.$DATE.gz
#changing permissions of directory
chmod -R 777 $BACKUP_DIR
You might consider this Open Source tool, matiri, https://github.com/AAFC-MBB/matiri which is a concurrent mysql backup script with metadata in Sqlite3. Features:
Multi-Server: Multiple MySQL servers are supported whether they are co-located on the same or separate physical servers.
Parallel: Each database on the server to be backed up is done separately, in parallel (concurrency settable: default: 3)
Compressed: Each database backup compressed
Checksummed: SHA256 of each compressed backup file stored and the archive of all files
Archived: All database backups tar'ed together into single file
Recorded: Backup information stored in Sqlite3 database
Full disclosure: original matiri author.
As a DBA, You must schedule the backup of MySQL Database in case of any issues so that you can recover your databases from the current backup.
Here, we are using mysqldump to take the backup of mysql databases and the same you can put into the script.
[orahow#oradbdb DB_Backup]$ cat .backup_script.sh
#!/bin/bash
# Database credentials
user="root"
password="1Loginxx"
db_name="orahowdb"
v_cnt=0
logfile_path=/DB_Backup
touch "$logfile_path/orahowdb_backup.log"
# Other options
backup_path="/DB_Backup"
date=$(date +"%d-%b-%Y-%H-%M-%p")
# Set default file permissions
Continue Reading ....
MySQL Backup
I have prepared a Shell Script to create a Backup of MYSQL database.
You can use it so that we have backup of our database(s).
#!/bin/bash
export PATH=/bin:/usr/bin:/usr/local/bin
TODAY=`date +"%d%b%Y_%I:%M:%S%p"`
################################################################
################## Update below values ########################
DB_BACKUP_PATH='/backup/dbbackup'
MYSQL_HOST='localhost'
MYSQL_PORT='3306'
MYSQL_USER='auriga'
MYSQL_PASSWORD='auriga#123'
DATABASE_NAME=( Project_O2 o2)
BACKUP_RETAIN_DAYS=30 ## Number of days to keep local backup copy; Enable script code in end of th script
#################################################################
{ mkdir -p ${DB_BACKUP_PATH}/${TODAY}
echo "
${TODAY}" >> ${DB_BACKUP_PATH}/Backup-Report.txt
} || {
echo "Can not make Directry"
echo "Possibly Path is wrong"
}
{ if ! mysql -u ${MYSQL_USER} -p${MYSQL_PASSWORD} -e 'exit'; then
echo 'Failed! You may have Incorrect PASSWORD/USER ' >> ${DB_BACKUP_PATH}/Backup-Report.txt
exit 1
fi
for DB in "${DATABASE_NAME[#]}"; do
if ! mysql -u ${MYSQL_USER} -p${MYSQL_PASSWORD} -e "use "${DB}; then
echo "Failed! Database ${DB} Not Found on ${TODAY}" >> ${DB_BACKUP_PATH}/Backup-Report.txt
else
# echo "Backup started for database - ${DB}"
# mysqldump -h localhost -P 3306 -u auriga -pauriga#123 Project_O2 # use gzip..
mysqldump -h ${MYSQL_HOST} -P ${MYSQL_PORT} -u ${MYSQL_USER} -p${MYSQL_PASSWORD} \
--databases ${DB} | gzip > ${DB_BACKUP_PATH}/${TODAY}/${DB}-${TODAY}.sql.gz
if [ $? -eq 0 ]; then
touch ${DB_BACKUP_PATH}/Backup-Report.txt
echo "successfully backed-up of ${DB} on ${TODAY}" >> ${DB_BACKUP_PATH}/Backup-Report.txt
# echo "Database backup successfully completed"
else
touch ${DB_BACKUP_PATH}/Backup-Report.txt
echo "Failed to backup of ${DB} on ${TODAY}" >> ${DB_BACKUP_PATH}/Backup-Report.txt
# echo "Error found during backup"
exit 1
fi
fi
done
} || {
echo "Failed during backup"
echo "Failed to backup on ${TODAY}" >> ${DB_BACKUP_PATH}/Backup-Report.txt
# ./myshellsc.sh 2> ${DB_BACKUP_PATH}/Backup-Report.txt
}
##### Remove backups older than {BACKUP_RETAIN_DAYS} days #####
# DBDELDATE=`date +"%d%b%Y" --date="${BACKUP_RETAIN_DAYS} days ago"`
# if [ ! -z ${DB_BACKUP_PATH} ]; then
# cd ${DB_BACKUP_PATH}
# if [ ! -z ${DBDELDATE} ] && [ -d ${DBDELDATE} ]; then
# rm -rf ${DBDELDATE}
# fi
# fi
### End of script ####
In the script we just need to give our Username, Password, Name of Database(or Databases if more than one) also Port number if Different.
To Run the script use Command as:
sudo ./script.sc
I also Suggest that if You want to see the Result in a file Like:
Failure Occurs or Successful in backing-up,
then Use the Command as Below:
sudo ./myshellsc.sh 2>> Backup-Report.log
Thank You.