1

I am currently working on a project where I have to build a Linux network on a virtual machine using Ubuntu 18.04. I am currently trying to figure out how to write a script that backs up the printed documents every 6 hours. My question is this: How should I go about writing this script? My professor has decided for us to find this information on our own, and it has been very difficult to find some guidance on the internet.. Thank you in advanced for anyone who can help!!

Edit:

I understand how to make the script run every 6 hours using the cron utility. But my question is how do I write this script? I am completely lost on the writing the script itself. So if a document were to be printed to a printer, how would I be able to write a script that backs whatever is printed off the printer?

Here was my train of thought: I wanted to maybe create a script so that every time something is printed, it goes into a folder where I can take that folder and back it up. Does this sound about right? Or are there log files that automatically do this and all I need to do is somehow get it from the log file?

1

Have a look at lpq and lpstat:

  • lpq shows queued jobs on the default printer and lpq -l does it more verbosely, see man lpq
  • lpstat -o prints the queued jobs, use lpstat -Wcompleted -o for completed jobs, see man lpstat
0

Summarizing the helpful comments of @qwr, @GeorgeUdosen and @perlduck, here is what you need to do:

  • write a script that backs up the files using one of the various backup command line utilities
  • test that script until it works flawlessly
  • look into cron about how to execute commands at a certain time.
  • 1
    I can do all the cron job stuff, its just writing the script and knowing what to write is the difficult part. Any other suggestions? – Nate Chung Nov 27 '18 at 19:32
0

The /var/spool/ is traditionally used for machine-local data being spooled to or from UNIX subsystems.

The example I will give is for Ubuntu 18.04, which I'm on

All the printing jobs are stored in the /var/spool/cups directory (CUPS is the standards-based, open source printing system developed by Apple Inc.), you will need sudo privileges to access the directory.

On other Linux systems, printing jobs will be on /var/spool/lpd (Printer spool directory)

Below is very simple script to display all the files in the directory

#!/bin/bash

print_file_location="/var/spool/cups/*"

    for file in $print_file_location
    do 
        #this will print all the files in directory
        echo "$file"
    done

Hopefully this will help in your investigation.

edited: To copy the files to new location you could:

#!/bin/bash

print_file_location="/var/spool/cups/*"

for file in $print_file_location
do 
    #this will copy the files to a different directohory
    cp $file  "/home/print_backup/ $(basename $file).bak"
done

The backed up files will have the same permissions as the original

  • Can you write a script where it copies all the print jobs in the /var/spool/cups directory and just copy it into a different directory called backups? – Nate Chung Nov 28 '18 at 18:16
  • Nate I left you that script, to try and figure out the rest, what you can is change the echo command to cp and the copy the files to another location. – Aaron Obeng Nov 29 '18 at 8:42
  • I've edited my answer to include a script to copy the files to another directory, you just need to edit the script to suit your needs. I hope this helps. – Aaron Obeng Nov 29 '18 at 10:32
  • Do I replace $(basename $file).bak with something? I keep executing this script and it keeps saying that "var/spool/cups is a directory". – Nate Chung Nov 29 '18 at 18:51
  • You need to check if have files in the directory, that's done by doing sudo ls /var/spool/cups/ . If there are no files, then there is nothing to copy. This directory should contain the files you have sent to the printer. – Aaron Obeng Nov 29 '18 at 19:42

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.