SnipeIt? What is that? SnipeIt is an open-source asset management system using which you can manage your organization’s assets. You can get more detail on that https://snipeitapp.com/.
We came across a scenario where we need to backup the asset management (which uses SnipeIt) daily and upload it to the S3 bucket.

I can’t do this task daily, so yeah let’s get this done using a simple shell script.
- Initialize a log file
LOGFILE="/var/log/snipeit-backup.log" echo "**************START*************************" >> $LOGFILE
-
Navigate to the asset management directory and initialize the snipeit backup command
php artisan snipeit:backup
cd /var/www/snipeit/ php artisan snipeit:backup >> $LOGFILE
-
Now we need to figure out which is the latest backup in the backup folder
Here is the command to find the latest modified file in the specific folder.
cd /var/www/snipeit/storage/app/backups Filename=$(ls -Art | tail -n 1)
-
Upload the latest backup file to S3 bucket
echo 'Backup uploading to S3 bucket' >> $LOGFILE /snap/bin/aws s3 cp $Filename s3://BUCKET_NAME/FOLDER_NAME/ >> $LOGFILE
-
Delete all the backups older than 3 days
echo 'Deleting backup older than 3 days' >> $LOGFILE find /var/www/snipeit/storage/app/backups -type f -name '*.zip' -mtime +3 -exec rm {} \; -
Now lets notify that everything went well using email
SENDGRID_API_KEY="INSERT_YOUR_KEY_HERE" BAK_DATETIME=`date +%F-%H:%M` SUBJECT="Asset Mangement Backup was succcessful at: ${BAK_DATETIME}" REQUEST_DATA='{"personalizations": [{ "to": [{ "email": "foo@foo.com" }], "subject": "'"$SUBJECT"'" }], "from": { "email": "foo@foo.com", "name": "foo.com" }, "content": [{ "type": "text/plain", "value": "This is an automated message to inform that Asset backup ('"$Filename"') completed successfully to S3 bucket." }] }'; curl -X "POST" "https://api.sendgrid.com/v3/mail/send" \ -H "Authorization: Bearer $SENDGRID_API_KEY" \ -H "Content-Type: application/json" \ -d "$REQUEST_DATA" echo "Sent email notification via sendgrid" >> $LOGFILE echo "***************END***************************" >> $LOGFILE -
Now for the final step set the cron at 12 A.M. daily to run this shell script.
Here is the cron location/etc/crontab. Add this code to the crontab:0 0 * * * root /var/www/snipeit/snipeit-backup.sh
Here is the full code snippet for reference:
| LOGFILE="/var/log/snipeit-backup.log" | |
| echo "**************START*************************" >> $LOGFILE | |
| #---------1. Asset Management backup starting--------- | |
| echo "Start Asset Management backup process" >> $LOGFILE | |
| #---------2. Navigate to Asset Management directory --------- | |
| cd /var/www/snipeit/ | |
| #---------3. Backup Asset Management --------- | |
| php artisan snipeit:backup >> $LOGFILE | |
| #---------4. Get the latest backup file from the directory --------- | |
| cd /var/www/snipeit/storage/app/backups | |
| Filename=$(ls -Art | tail -n 1) | |
| #---------5. Upload the backup file to S3 bucket --------- | |
| echo 'Backup uploading to S3 bucket' >> $LOGFILE | |
| /snap/bin/aws s3 cp $Filename s3://BUCKET_NAME/FOLDER_NAME/ >> $LOGFILE | |
| #---------6. Remove all backups older than 3 days --------- | |
| echo 'Deleting backup older than 3 days' >> $LOGFILE | |
| find /var/www/snipeit/storage/app/backups -type f -name '*.zip' -mtime +3 -exec rm {} \; | |
| #---------7. Send email to me/group to inform the backup is complete --------- | |
| SENDGRID_API_KEY="INSERT_YOUR_KEY_HERE" | |
| BAK_DATETIME=`date +%F-%H:%M` | |
| SUBJECT="Asset Mangement Backup was succcessful at: ${BAK_DATETIME}" | |
| REQUEST_DATA='{"personalizations": [{ | |
| "to": [{ "email": "foo@foo.com" }], | |
| "subject": "'"$SUBJECT"'" | |
| }], | |
| "from": { | |
| "email": "foo@foo.com", | |
| "name": "foo.com" | |
| }, | |
| "content": [{ | |
| "type": "text/plain", | |
| "value": "This is an automated message to inform that Asset backup ('"$Filename"') completed successfully to S3 bucket." | |
| }] | |
| }'; | |
| curl -X "POST" "https://api.sendgrid.com/v3/mail/send" \ | |
| -H "Authorization: Bearer $SENDGRID_API_KEY" \ | |
| -H "Content-Type: application/json" \ | |
| -d "$REQUEST_DATA" | |
| echo "Sent email notification via sendgrid" >> $LOGFILE | |
| echo "***************END***************************" >> $LOGFILE |
Hope this will be useful to you. 🙂 Feel free to comment here if you have any questions.
